而循环不工作终止问题

时间:2017-03-26 20:24:16

标签: c++

所以基本上用户输入的木材类型可以是PFCMO如果用户输入'T',循环应该终止并输出我到目前为止应该具有的代码总数但它不起作用。在cout中我没有编写输出总数的代码,但它只是一个测试,看看我是否得到输出,我在虚函数我得到用户的输入,例如P 10 10 10 10,如果它只是T环应该终止和cout我所拥有的。

#include <iostream>
#include <iomanip>
#include <string>
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
    {
        get_user_input(type, quanity, height, width, length);


    } while (type!='T');


    cout << "33333333333333333333333333333333333e";
    return 0;

}
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;

}  

1 个答案:

答案 0 :(得分:2)

试试这个:

void get_user_input(char& type, int& quanity, int& height, int& width, int& length)
{
    cin >> type;
    if (type == 'T') return;
    cin >> quanity >> height >> width >> length;
}

它无法正常工作的原因是因为它在cin语句结束前等你输入所有五个值。