以下循环应该在输入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;
答案 0 :(得分:1)
我在想如果你输入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;
}
}
答案 1 :(得分:0)
不要忘记使用std::endl
在std::cout
上刷新并插入换行符,甚至使用std::flush
。
std::cout << "Some text" << std::endl;