Reputation: 5
I'm having trouble skipping certain inputs.
int main(){
string input;
string lastName;
string firstName;
int age;
int streetNum;
string streetName;
string town;
string zipCode;
float balance;
later on
Update(lastName, firstName, age, streetNum, streetName, town, zipCode, balance);
}
The idea is, if the user enters nothing (just hits enter) the value should stay the same and move to the next input.
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); }
}
All my attempts have failed to skip the input if the user only hits enter.
Upvotes: 0
Views: 189
Reputation: 73366
The problem with cin
for your code is:
Consider use of getline()
into a string (and eventually stringstream to parse further the result received):
getline (cin, input); // instead of cin>>input;
By the way, streetname isn't an integer, is it ? ;-)
Upvotes: 1