0

Hello!
I am writing c++ program on linux.
Whenever I read a char or string, I got problem.
In c, I sove the problem using
if(getchar()=='\n')
in c++, how can I sove that problem?
Here is my program. I cannot do any input for name.
Please help me.

#include<iostream.h>
struct Student
{
int rno;
char name[20];
int  cmark;
int  osmark;
int  javamark;

void setdata()
{
cout<<"\nEnter rno:";
cin>>rno;
cout<<"\nEnter name:";
//if(getchar()=='\n');
cin.getline(name,20);
cout<<"\nEnter mark for c:";
cin>>cmark;
cout<<"\nEnter mark for os:";
cin>>osmark;
cout<<"\nEnter mark for java:" ;
cin>>javamark;
}
void testdata()
{
int fcount=0;


if(cmark<=40)fcount++;
if(osmark<=40)fcount++;
if(javamark<=40)fcount++;
if(fcount>1)
cout<<"\n"<<rno;
}

};
int main()
{
struct Student stu[3];
int i;
for(i=0;i<3;i++)
stu[i].setdata();
cout<<"\nFailed list";
for(i=0;i<3;i++)
stu[i].testdata();


return 0;
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

Your problem is perhaps about flushing the input stream.
Read the sticky thread http://www.daniweb.com/forums/thread90228.html
.
You though, have not fully described your problem.
What I feel is, you may not be able to enter the 'marks for c' as the '\n' remains in the input stream and is entered automatically when prompted to enter 'marks for c'.
Try cin.ignore(1000,'\n'); after cin.getline(name,20);

0

Yeah, do something like siddhant: put cin.ignore(); after cin.getline(name,20); it works !!

(credit goes to siddhant)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.