String input 'overflowing' into next string variable

This is a total noob question but here goes:
I'm trying to write a simple console program that just displays the limitations of using cin instead of getline().

1
2
3
4
5
6
7
8
9
10
11
12
13
    //cin doesn't work for more than one word at a time
    cout << "What's your full name?\n" ;
    string fullname;
    cin >> fullname;
    //this displays just the first name and ignores the rest of the input
    cout << "\n\nYour full name is: " << fullname <<".\n" << endl;

    //to get the whole input from the user you have to use getline
    cout << "What's your address?\n" ;
    string address;
    getline(cin, address);
    //this displays the whole address instead of just the first input
    cout << "\n\nYour address: " << address <<".\n\n" ;


Let's say, if the user inputs 'Peter Brown', 'Peter' is assigned to fullname (like it should) and that is then displayed back to the user. The issue arises in the next bit of code. The user is never then asked to input a value again and address receives the value 'Brown'. As you may guess, that's not what I'm trying to do.

To clarify, I'm not trying to 'fix' the first part of the code. That is to demonstrate the limitations of cin. What I want to do is show that getline() successfully accepts a full line of code.

Any help is greatly appreciated! :)
Try this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main()
{
    
 //cin doesn't work for more than one word at a time
    cout << "What's your full name?\n" ;
    string fullname;
    getline (cin,fullname);
    //this displays just the first name and ignores the rest of the input
    cout << "\n\nYour full name is: " << fullname <<".\n" << endl;

    //to get the whole input from the user you have to use getline
    cout << "What's your address?\n" ;
    string address;
    getline(cin, address);
    //this displays the whole address instead of just the first input
    cout << "\n\nYour address: " << address <<".\n\n" ;
}


hope it helps.
the underline are the added or changed info.
Last edited on
@iamnoob15 Although I appreciate your help, that's not quite what I'm trying to do. I want the first half of the code to not actually work. Because I want to show to whoever reads the code (probably future me) that using cin has limitations and that is why it is better to use getline(). The purpose in the code is to demonstrate the difference between the two. The comments are in there not reporting the problem but to show the programmer the limitations and what-not.
Last edited on
One way is to add line cin.ignore(1000, '\n'); on line 12.
It ignores everything till 1000 characters or it gets newline.
http://www.cplusplus.com/reference/istream/istream/ignore/

Additionally it can be done as cin.ignore(numeric_limits<streamsize>::max(), '\n'); which need <limits> included.
Last edited on
@Jezze Thank you very much. This fixed it! I have no idea how it works so now I have some reading up to do!
Topic archived. No new replies allowed.