我尝试使用我询问过的 do/while 循环,并修复了我在 int main
中的一个函数,以允许在用户需要时重新运行整个程序,但它正在重新运行程序无需等待用户输入。
int main()
{
int spoolnumber = 0; // Number of spools to be ordered
float subtotalspool = 0; // Spool sub total
float shippingcost = 0; // Shipping cost
float totalcost = 0; // Total cost
char type = 'n';
do {
instruct(); // Print instructions to user
spoolnumber = spoolnum(); // calculate and store number of spools
subtotalspool = stotalspool(spoolnumber); // Calculate subtotal
shippingcost = shipcost(subtotalspool); // Calculate subtotal
totalcost = tcost(subtotalspool, shippingcost); // Calculate final total
// Print final output
results(spoolnumber, subtotalspool, shippingcost, totalcost);
cout << "\n" << " Would you like to run the program again? [y/n]";
}
while (type != 'y');
return 0;
}
最佳答案
您还没有添加任何代码来接受用户输入。在循环的底部,尝试将字符从 cin
读入 type
。
此外,在接受来自 cin
的用户输入之前,您可能需要先刷新 cout
的输出。
关于c++ - 如何在 do/while 循环中获取用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7795182/