而循环终止问题

  • 本文关键字:问题 终止 循环 c++
  • 更新时间 :
  • 英文 :


以下循环应该在输入 T 作为我的以下代码类型时终止。当我输入 T 以检查循环是否确定它是否终止时,我只得到一个空行,我该如何解决这个问题?

#include <iostream>
#include <iomanip>
using namespace std;
void get_user_input(char&, int&, int&, int&, int&);
float compute_item_cost(char, int, int, int, int);
const float pine_cost = 0.89;
const float fir_cost = 1.09;
const float cedar_cost = 2.26;
const float maple_cost = 4.50;
const float oak_cost = 3.10;
int main()
{
    int quanity, height, width, length;
    string name_of_wood;
    char type;//declare variables
    get_user_input(type, quanity, height, width, length);
    do
    {
        float cost = compute_item_cost(type, quanity, height, width, length);
        if (type == 'P') {
            cout << "Pine" << cost;
            cout << "n";
            get_user_input(type, quanity, height, width, length);
        }
    }
    while (type != 'T');
    cout << "bad input";
}
void get_user_input(char& type, int& quanity, int& height, int& width, int& length)
{
    cout << "Enter the wood type";
    cin >> type >> quanity >> height >> width >> length;
}
float compute_item_cost(char type, int quanity, int height, int width, int length)
{
    float compute_cost;
    float price;
    if (type == 'P') {
        compute_cost = (height*width*length) / 12.0;
        return compute_cost*quanity*pine_cost;
    }
    //compute_cost = (height*width*length) / 12.0;
    //return compute_cost*quanity*
    compute_cost = (height*width*length) / 12.0;
    return compute_cost*4.50*quanity;

我在想,如果你输入T,你不应该输入其余的东西来继续(并终止程序(。 所以也许可以这样改变get_user_input

void get_user_input(char& type, int& quanity, int& height, int& width, int& length)
{
    cout << "Enter the wood type";
    cin >> type;
    if (type != 'T')
    {
        cin >> quanity >> height >> width >> length;
    }
}

不要忘记使用std::endlstd::cout上刷新和插入换行符,甚至使用std::flush

std::cout << "Some text" << std::endl;

最新更新