-1

I'm having trouble with this code. I want it to take 2 words and it's always ignoring the first word and then taking the 2nd word. If I put in 3 words, it works perfectly and picks up the final 2 words. The prompt for this is these bullet points.

  • Prompt the user to enter his/her first and last name.
  • Read the name from the keyboard using the getline method and store it into a variable called fullName (you will need to declare any variables you use).
  • Print out the fullName.
  • Compile, debug, and run, using your name as test data.
  • Since we are adding on to the same program, each time we run the program we will get the output from the previous tasks before the output of the current task.

How do you make this work correctly?

string fullName;
cout << "What is your full name" << endl;
    cin >> fullName;
getline(cin, fullName);
    cout << fullName << endl;
cout << endl;
JaMiT
  • 9,693
  • 2
  • 12
  • 26
snowstv
  • 3
  • 2
  • 2
    duplicate: https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – JohnFilleau Sep 23 '20 at 03:56
  • Picking up only the final two names when you entered three is working "perfectly"? You still skip the first name. – JaMiT Sep 23 '20 at 04:19
  • Why are you streaming from `cin` when the instructions say to use `getline`? What is that line supposed to do (other than skip a word of input)? – JaMiT Sep 23 '20 at 04:20
  • I really don't know! I'm super new to all of this sorry. – snowstv Sep 23 '20 at 04:22
  • 2
    @snowstv Writing code that you do not know the purpose of leads to [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). (That's a bad thing.) Programming is not about magic incantations; it is about telling the computer what to do. Think about what you want the computer to do, then express those instructions in C++. Each line serves a purpose, and you should know the purpose of each line you write. – JaMiT Sep 23 '20 at 04:54
  • Also it can be more effective to think in terms of behaviours you want to express than instructions. By the time the compiler's done, the instructions often look nothing like the instructions you wrote, but if you described the behaviour correctly you'll get what you want out the other side. [See the As-if Rule](https://en.cppreference.com/w/cpp/language/as_if). This becomes very important when profiling code. Many people put together very complicated loops, start timing, and get 0 because they left out observable behaviour. If the program does nothing, the compiler removes all of the code. – user4581301 Sep 23 '20 at 04:59

1 Answers1

3
cin >> fullName;

reads the first word of the full name.

getline(cin, fullName);

reads the rest of the name over top of the first word. Computer programs do exactly what you tell them to do and show somewhat less than zero mercy if that's the wrong thing to do.

So given John Jacob Jingleheimer Schmidt

cin >> fullName; // reads John into fullname
getline(cin, fullName); // reads  Jacob Jingleheimer Schmidt into fullname, 
                        // replacing John
cout << fullName << endl; // prints  Jacob Jingleheimer Schmidt

Solution

Remove the cin >> fullName;

getline(cin, fullName); // reads John Jacob Jingleheimer Schmidt into fullname, 
cout << fullName << endl; // prints John Jacob Jingleheimer Schmidt
user4581301
  • 29,019
  • 5
  • 26
  • 45