cin & if, else mix

Why does it ignores cin and how make it work ?
please help



#include <iostream>
int main()
{
using namespace std;
char a[10];
char aa;
char aaa;
char b[10];
char bb;
char bbb;
cout<<"choice 1 or 2"<<endl;
int c;
cin>>c;
if (c==1){
cout<<"choose a"<<endl;
cout<<endl;
cin.getline(a,10); //ignores a
cout<<"choose b"<<endl;
cout<<endl;
cin.getline(b,10);
cout<<"a = "<<a<<" b = "<<b<<endl;
}else if(c==2){
cout<<"choose aa"<<endl;
cout<<endl;
cin>>aa;
cout<<"choose bb"<<endl;
cout<<endl;
cin>>bb; //ignores bb
cout<<"aa = "<<aa<<" bb = "<<bb<<endl;
}else{
cout<<"choose aaa"<<endl;
cout<<endl;
cin>>aaa;
cout<<"choose bbb"<<endl;
cout<<endl;
cin>>bbb; //ignores bbb
cout<<"aaa = "<<aaa<<" bbb = "<<bbb<<endl;
}
system("PAUSE");
return 0;
}
Try to not mix getline() and operator>>.
Read this:
http://www.cplusplus.com/forum/articles/6046/
The problem ain't getline.
I can remove getline from whole code and just use cin. Nothing will change, only than the second cin will be ignored, like my example demonstrates.
Put that in code tags, how are we supposed to analyze the code if it's illegible?
And chances are the problem *is* getline. Even if the getline actually works, it probably is still due to the buffering issues with getline.
If you remove getline and use cin (ONLY cin, no mixing, as that is generally bad), does it still work? Have you tried it? The way getline and cin operate are quite different.
Your problem IS mixing cin>> and getline.
Have you read the article I sent you?
Last edited on
Don't you hate people that don't read?
DrChill wrote:
@People not reading what is in front of them

The difference between Expert users and Beginners.
Last edited on
I have read the post and here is an example that doesn't uses getline:



#include <iostream>
int main()
{
using namespace std;
char a;
char aa;
char aaa;
char b;
char bb;
char bbb;
cout<<"choice 1 or 2"<<endl;
int c;
cin>>c;
if (c==1){
cout<<"choose a"<<endl;
cout<<endl;
cin>>a;
cout<<"choose b"<<endl;
cout<<endl;
cin>>b; //ignores b
cout<<"a = "<<a<<" b = "<<b<<endl;
}else if(c==2){
cout<<"choose aa"<<endl;
cout<<endl;
cin>>aa;
cout<<"choose bb"<<endl;
cout<<endl;
cin>>bb; //ignores bb
cout<<"aa = "<<aa<<" bb = "<<bb<<endl;
}else{
cout<<"choose aaa"<<endl;
cout<<endl;
cin>>aaa;
cout<<"choose bbb"<<endl;
cout<<endl;
cin>>bbb; //ignores bbb
cout<<"aaa = "<<aaa<<" bbb = "<<bbb<<endl;
}
system("PAUSE");
return 0;
}



Can you please correct that so that all cin are working.
It does work for me.
I made it work. I can use as many cin as i want now, including mixing cin, >> and getline with if, else. I tryed this code and it worked before in past. Now that i have the newest windows and Visual C++ updated to newest it doesn't works like that anymore and i can't use many of .h includes also anymore.
Topic archived. No new replies allowed.