Problem withb accepting char arrays
May 9, 2013 at 3:22pm UTC
My C++ program never accepts a char array and instead jumps to the next statement of accepting the marks . It neither displays the name.Can somebody solve my problem?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
class candidate
{ long rno;
char name[50];
float score;
char rem[50];
void assignrem();
public :
void enter();
void disp();
}C;
void ::candidate::assignrem()
{ if (score>=50)
strcpy(rem,"Selected" );
else
strcpy(rem,"Not Selected" );
}
void ::candidate::enter()
{ cout<<"\nEnter roll number\n" ;
cin>>rno;
cout<<"\nEnter name\n" ;
gets(name);
cout<<"\nEnter score\n" ;
cin>>score;
assignrem();
}
void ::candidate::disp()
{ cout<<"\nRoll number: \t" <<rno;
cout<<"\nName: \n" ;
puts(name);
cout<<"\nScore: \t" <<score;
cout<<"\n Remarks: \n" ;
puts(rem);
}
int main()
{ C.enter();
C.disp();
return 0;
}
May 9, 2013 at 3:42pm UTC
use
cin >> name
May 10, 2013 at 2:27pm UTC
I am trying not to be rude but can you tell me why should that be? Shouldn't char arrays be accepted with gets() and not 'cin' since cin can only accept single characters. I tried cin.getline but got no success.
May 10, 2013 at 3:12pm UTC
cin reads up until it reaches a space or newline.
I was presuming you would be using a single word for a name.
If you wanted multiple words you can use getline.
getline (cin, name);
Hope this clears up any confusion
May 12, 2013 at 6:49am UTC
It worked ! Thank you for your kind support !
Topic archived. No new replies allowed.