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;
}

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);

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

(credit goes to siddhant)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.