0
我無法跳過某些輸入。C++存儲cin輸入和跳過其他
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;
size_t sz;
cout << "Last name: ";
cin >> input;
if (!input.empty()) { lastname = input; }
cout << "First name: ";
cin >> input;
if (!input.empty()) { firstname = input; }
cout << "Age: ";
cin >> input;
if (!input.empty()) { age = stoi(input, &sz); }
cout << "Street number: ";
cin >> input;
if (!input.empty()) { streetnum = stoi(input, &sz); }
cout << "Street name: ";
cin >> input;
if (!input.empty()) { streetname = stoi(input); }
cout << "Town name:";
cin >> input;
if (!input.empty()) { town = input; }
cout << "ZipCode: ";
cin >> input;
if (!input.empty()) { zipcode = input; }
cout << "Balance: ";
cin >> input;
if (!input.empty()) { balance = stof(input); }
}
如果用戶只輸入回車,我所有的嘗試都無法跳過輸入。
我覺得需要'std :: getline'。 – WhozCraig 2014-09-12 23:28:24
只要確保你不要混用'std :: getline'和'>>'輸入。 – 2014-09-12 23:30:22