所以我有這個C++值更新/忽略
int main(){
string input;
string lastName;
string firstName;
int age;
int streetNum;
string streetName;
string town;
string zipCode;
float balance;
Update(lastName, firstName, age, streetNum, streetName, town, zipCode, balance);
}
,這裏是功能更新
void Update(string &lastname, string &firstname, int &age, int &streetnum, string &streetname, string &town, string &zipcode, float &balance){
cout << "Update the following, enter nothing to leave the same: " << endl;
string input;
cout << "Last name: ";
getline(cin, input);
if (input != "\n") { lastname = input; }
cout << "First name: ";
getline(cin, input);
if (input != "\n") { firstname = input; }
cout << "Age: ";
getline(cin, input);
if (input != "\n") { age = atoi(input.c_str()); }
cout << "Street number: ";
getline(cin, input);
if (input != "\n") { streetnum = atoi(input.c_str()); }
cout << "Street name: ";
getline(cin, input);
if (input != "\n") { streetname = input; }
cout << "Town name:";
getline(cin, input);
if (input != "\n") { town = input; }
cout << "ZipCode: ";
getline(cin, input);
if (input != "\n") { zipcode = input; }
cout << "Balance: ";
getline(cin, input);
if (input != "\n") { balance = atof(input.c_str()); }
}
我的目標是更新的價值或跳到下一個值,如果輸入的是' \ N」。
一旦運行並且程序調用Update,它會在同一行上輸出「Last Name:First Name:」而不讓用戶輸入任何內容到姓氏中。我不知道爲什麼這樣做。任何提示或線索方向進入將是有益的。
我似乎無法用您提供的代碼重現您的問題。也許有一個'\ n'「卡在」cin中,你沒有在你的代碼中的其他地方處理? – godel9 2014-09-13 04:54:38
你是否在某處使用'cin >>?看起來像尾隨換行問題 – P0W 2014-09-13 04:55:53
是的,我有另一個功能類似於更新()。唯一的區別是它更新可以跳過輸入。什麼是後續換行問題? – user3348713 2014-09-13 05:19:40