而循环不工作终止问题

While Loop not Working termination issue

本文关键字:终止 问题 工作 循环      更新时间:2023-10-16

所以基本上用户输入木材类型可以是P F C M O 如果用户输入"T",循环应该终止并输出我到目前为止拥有的代码总数,但它不起作用。在 cout 中,我没有编写代码来输出总数,但它只是一个测试,看看我是否得到输出,我在 void 函数中我从用户那里获得输入,例如可能是 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;
}  

试试这个:

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 语句完成之前等待您输入所有五个值。