重複の可能性:
cin を 2 回使用する問題
このコードは機能しますが、意図したものではありません。コマンドプロンプトで出力を押して新しい給与を入力するたびに1
、次のようになります。
Comic books : USD Input error! Salary must be in positive integer.
コードは 4 行目で停止するはずですがcout<<"\n\nComic books\t\t: USD ";
、内側の while ループで実行されただけです。これはコードです:
double multiplePay =0;
cout<<"\n\nEnter employee pay for each job";
while (1){
cout<<"\n\nComic books\t\t: USD ";
//cin.get(); if enable, the first user input will be 0. this is not working.
std::string comic_string;
double comic_double;
while (std::getline(std::cin, comic_string))
{
std::stringstream ss(comic_string); // check for integer value
if (ss >> comic_double)
{
if (ss.eof())
{ // Success so get out
break;
}
}
std::cout << "Input error! Salary must be in positive integer.\n" << std::endl;
cout<<"Employee salary\t: ";
}
comic = strtod(comic_string.c_str(), NULL);
multiplePay = comic + multiplePay; // update previous salary with new user input
cout << multiplePay;
cout << "Add other pay?"; // add new salary again?
int y;
cin >> y;
if (y == 1){
cout << multiplePay;
}
else{
break;
}
} // while
cout << multiplePay; //the sum of all salary
を使用cin.get()
すると問題は解決しますが、最初のユーザーの給与入力は になり0
、次の入力のみが計算されます。これで私を助けてください。前もって感謝します。