i am having a problem in c++ program (its my school project). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

i am having a problem in c++ program (its my school project).

in this program i have to input the details from the user but here a after entering the name it goes to infinite loop in function input and it in not doing cin please help: struct employ{ char code,name; int d,m,y; }e[5]; void input(){ for(int i=0;i<5;i++){ cout<<"employ "<<i+1<<" enter your data"; cout<<"enter your name"<<endl; cin>>e[i].name; cout<<"enter code"<<endl; cin>>e[i].code; cout<<"enter your date of joining of job in ddmmyyyy"<<endl; cin>>e[i].d>>e[i].m>>e[i].y; } } void display(){ for(int i=0;i<5;i++){ cout<<"data of "<<i+1<<" employ"<<endl; cout<<"name: "<<e[i].name<<endl; cout<<" code: "<<e[i].code<<endl; cout<<" date of joining"<<e[i].d<<e[i].m<<e[i].y; } } void main(){ clrscr(); char x,code1; int ch,a,j,i,d1,m1,y1; double n[5],n1,l,l1,yoj[5]; a=1; while(a!=0){ cout<<"1.input the data"<<endl; cout<<"2.display all data"<<endl; cout<<"3.exit"<<endl; cout<<"enter choice 1 or 2 or 3"<<endl; cin>>ch; switch(ch){ case 1: input(); break; case 2: display(); break; case 3: break; } cout<<"do you want to continue 1 for yes or 0 for no"; cin>>a; } getch(); }

4th Jan 2019, 2:14 PM
Huzaifa qureshi
Huzaifa qureshi - avatar
5 Answers
+ 1
Well, then I can't help you anymore :/ On my PC (with Linux) it worked perfectly, it might be that Windows needs a different approach.
4th Jan 2019, 4:32 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
Adding cin.clear() will probably fix your problem. cin doesn't interrupt the program because stdin isn't empty, so you should use cin.clear() after cin to clear stdin... Here's a fixed version of your program: https://code.sololearn.com/cmL2q1Hf40wR/?ref=app
4th Jan 2019, 2:58 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
the display is not clear (random inputs) in this project i have to display menu properly
4th Jan 2019, 3:16 PM
Huzaifa qureshi
Huzaifa qureshi - avatar
0
maybe you want to uncomment clrscr() ? I commented it because conio.h is not available for Linux
4th Jan 2019, 3:23 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
no its not working . after i enter name of first employ it displays :{ entrer code enter your data of joining of job inddmmyyyy employ 2 enter your data.......employ 4 enter your data enter your name enter code} this program is not working properly and it doesn't cin year of joining
4th Jan 2019, 3:28 PM
Huzaifa qureshi
Huzaifa qureshi - avatar