c++ - 如何在 do/while 循环中获取用户输入?

标签 c++ user-input

我尝试使用我询问过的 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/

相关文章:

c++ 与 linux : Qtversion is invalid 上的 Qt 创建者

c++ - 为什么 std::getline(cin, Number) 给出 "No matching function for call"错误?

c++ - 递归比较字符串的函数 - C++

javascript - JS - 'function' 不是函数异常

c++ - 奇怪的结果 (`\210` ) 打印字符数组的末尾时

c++ - 委派模板成员功能的良好设计

ios - 通过插入可见空格格式化安全文本输入

C: 如何在不记录 ENTER (\n) 的情况下获取用户输入?

user-input - 如何在 AutoHotkey 中读取多行用户输入?

user-input - 如何在 fish shell 中获得用户确认?