0

So I have this

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);
}

and here is the function Update

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()); }

}

My goal is to update the value or skip to the next value if the input is '\n'.

Once running and the program calls Update, it prints out "Last Name: First Name: " on the same line without letting the user input anything into lastname. I have no idea why it does this. Any tips or clues to directions to go in would be helpful.

  • I can't seem to reproduce your problem with the code you've provided. Maybe there's a `\n` "stuck" in cin that you didn't process somewhere else in your code? – godel9 Sep 13 '14 at 04:54
  • 1
    Did you use `cin >>` somewhere ? Looks like trailing newline issue – P0W Sep 13 '14 at 04:55
  • Yea I have another function similar to Update(). The only difference is that it Update can skip an input. What is a trailing newline issue? – user3348713 Sep 13 '14 at 05:19
  • To understand the trailing newline issue, take a look at http://stackoverflow.com/questions/6642865/getline-not-asking-for-input. – R Sahu Sep 13 '14 at 05:22
  • 1
    `if (input != "\n") { lastname = input; }` input will **never** equal `"\n"` because `getline()` removes the `"\n"` character. You can use `if(!input.empty())` instead but you might want to look into finding a function to trim away any spaces that may have been typed in by mistake. – Galik Sep 13 '14 at 05:44
  • This works fine for me (except the issue with checking if the user just pressed return). – Galik Sep 13 '14 at 05:50

1 Answers1

0

getline() doesn't wait for user input. I believe unless you're told to use getline() you may want to use cin. Which would look as such:

cout<< "Lastname: ";
cin>>input;
if(input != " ")
{
    lastname= input;
}

The only problem I foresee is that you won't be able to use '\n' as your condition for your if statements. In the above example I used a space as my skip character.

  • 1
    I think he is using `cin`. He's just passing `cin` to `getline`. His code's working for me. – godel9 Sep 13 '14 at 04:55
  • 1
    `getline()` does wait for user input. It can't `get` anything until the user inputs it and it will block and wait if the user has not typed anything. – Galik Sep 13 '14 at 05:46