C++ 由于字符串的原因,While循环无法正确运行
当我在main中将此函数调用到while循环时,while循环第二次运行部件以输入客户机名称时,已经用用户第一次输入的名称填充。我怎样才能避免这种情况C++ 由于字符串的原因,While循环无法正确运行,c++,C++,当我在main中将此函数调用到while循环时,while循环第二次运行部件以输入客户机名称时,已经用用户第一次输入的名称填充。我怎样才能避免这种情况 void u_input(double &pr, string &nacl) { cout<<"Enter the name of the client: "<<nacl; getline(cin, nacl); cout<<"How much is the sale p
void u_input(double &pr, string &nacl)
{
cout<<"Enter the name of the client: "<<nacl;
getline(cin, nacl);
cout<<"How much is the sale price of the house";
cin>>pr;
}
void u_输入(双精度&pr,字符串&nacl)
{
下面的cout可以为您做这件事:
void u_input(double &pr,string &nacl)
{
cout<<"Enter the name of the client: "<<endl;
getline(cin, nacl);
cout<<"How much is the sale price of the house";
cin>>pr;
}
void u_输入(双精度&pr,字符串&nacl)
{
cout好吧,避免这种情况的一种方法是,当你要求一个新名字时,不要打印它
void u_input(double &pr, string &nacl)
{
cout<<"Enter the name of the client: ";
getline(cin, nacl);
cout<<"How much is the sale price of the house";
cin>>pr;
}
void u_输入(双精度&pr,字符串&nacl)
{
我不知道你的节目还有多长时间,但我要试一试
您可以保留变量以将用户信息存储在main中,并为这两个变量构建一个数组,以便您可以输入尽可能多的信息。。
我是一个新手,但我希望这只是给你一个大概的想法
当你再次启动程序时,你输入的信息将被删除XD
loopControl = 1;
char answer;
string clientName[10];
float price[10];
int i = 0;
while(loopControl =0)
{
void u_input();
cin >> clientName[i];
cin >> price[i];
i++;
cout << "would u like to enter more information? y/n" << endl;
cin >> answer;
if ( answer = 'y')
loopControl = 0;
else
loopControl = 1
}
loopControl=1;
答案;
字符串clientName[10];
浮动价格[10];
int i=0;
while(loopControl=0)
{
无效u_输入();
cin>>客户名称[i];
cin>>价格[i];
i++;
答不上来;
如果(答案为“y”)
loopControl=0;
其他的
loopControl=1
}
您可以通过不将输入存储在用于输出的同一变量中来避免这种情况。或者干脆不要这样做尝试了两种方法,但第二次它跳过了运行“输入客户名称”行。它会立即转到“销售价格是多少?”