#include <iostream>
#include <string>
usingnamespace 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;
}
elseif(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.
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
> 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 >>)
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)
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.