Problem after running the program

Hi im fairly new to C++ programming and need help with this program i just wrote for practice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

struct peopleInfo{
    string pName;
    int pPhone;
    string pAdress;
};

int main(){
    peopleInfo person;
    string input;

    cout<<"Please enter the persons name you would like to archive: "<<endl;
    cin>>person.pName;
    cout<<"Now input there phone number: "<<endl;
    cin>>person.pPhone;
    cout<<"And lastly their address: "<<endl;
    cin>>person.pAdress;
    cout<<"If you would like to access the stored info just type in -people- "<<endl;
    cout<<"or type end if you would like to end the program"<<endl;
    cin>>input;

    if(input == "people")
    {
        cout<<person.pName<<endl;
        cout<<person.pPhone<<endl;
        cout<<person.pAdress<<endl;
    }
    else if(input == "end")
    {
        return 0;
    }

}


So after i run this it gives me the first question and i can awnser it but after that it prints everything else out without asking for input and ends the program i just need to know whats wrong with the code.
Last edited on
closed account (j3Rz8vqX)
The return character is still in the buffer (from when you used cin).

To remove the buffer, use cin.get() or cin.ignore() after the use of every cin.

example:
1
2
3
4
5
6
7
//from your code:
    cout<<"Please enter the persons name you would like to archive: "<<endl;
    cin>>person.pName;
    cin.ignore(); //clears the buffer
    cout<<"Now input there phone number: "<<endl;
    cin>>person.pPhone;
    cin.get(); //clears the buffer 


http://www.cplusplus.com/reference/istream/istream/get/
http://www.cplusplus.com/reference/istream/istream/ignore/
> The return character is still in the buffer
that wouldn't be a problem, when reading whitespace gets ignored (note that it's using the extraction operator >>)

@OP: ¿could you show your test input?
Thanks! i cant believe I forgot about that, i probably need to slow down t learning c++, just getting ahead of my self but again thanks
when I tried the program the first time i just put in my name and then of cource i couldnt input anything else because of the problem it had(My name is Cory)
closed account (j3Rz8vqX)
Actually ne555 is right, haha.

But if it worked for you, I'm glad.

My idiotic response was set for:


So after i run this it gives me the first question and i can awnser it but after that it prints everything else out without asking for input and ends the program


Which usually occurs when something is in the cin buffer.

If you're using cin with whitespace, especially with strings, the other data after each white space will occupy the next call from cin.

A simple way to solve this would be to use:
 
getline(cin,person.pName)

Which is available from string library.

You can remove the cin.get()/cin.ignore().
Last edited on
Topic archived. No new replies allowed.