gpt4 book ai didi

c++ - While 循环终止问题?

转载 作者:行者123 更新时间:2023-11-28 05:13:58 25 4
gpt4 key购买 nike

以下循环应该在输入 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;
}
}

关于c++ - While 循环终止问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43023904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com