-
MINOR char ERROR
In this program, I'm accepting the values for the variables you see here.. Everything is fine until output of NAME. It just skips Name and ends the program. Am i missing something??
Code:
#include <iostream.h>
#include <stdlib.h>
void PRINT(int INT, float FLOAT, double DOUBLE, char MI, char NAME[30])
{cout<<"\n Please enter a value for int: "; cin>>INT;
cout<<" Please enter a value for float: "; cin>>FLOAT;
cout<<" Please enter a value for double: "; cin>>DOUBLE;
cout<<" Please enter a value for mi: "; cin>>MI;
cout<<" Please enter a value for Name: "; cin.getline(NAME,30);
cout<<"\n\n INT: "<<INT;
cout<<"\n FLOAT: "<<FLOAT;
cout<<"\n DOUBLE: "<<DOUBLE;
cout<<"\n MI: " <<MI;
cout<<"\n NAME: "<<NAME[30]; }
main()
{ int INT; float FLOAT; double DOUBLE; char MI, NAME[30];
PRINT(INT, FLOAT, DOUBLE, MI, NAME);
system("PAUSE");
return 0;
}
Thanks for checkin' it out
-
when you cout your name, you actually couting only index 30 of the array. it should be like this:
Code:
cout<<"\n NAME: "<<NAME;
our problem is when you enter an int before a string it doesnt clear the buffer so implement a small ClearBuffer() function like this:
Code:
#include <iostream.h>
#include <stdlib.h>
void ClearBuffer()
{
char Dummy[30];
getline (cin, Dummy);
}
void PRINT(int INT, float FLOAT, double DOUBLE, char MI, char NAME[30])
{cout<<"\n Please enter a value for int: "; cin>>INT;
cout<<" Please enter a value for float: "; cin>>FLOAT;
cout<<" Please enter a value for double: "; cin>>DOUBLE;
cout<<" Please enter a value for mi: "; cin>>MI;
ClearBuffer();
cout<<" Please enter a value for Name: "; cin.getline(NAME,30);
cout<<"\n\n INT: "<<INT;
cout<<"\n FLOAT: "<<FLOAT;
cout<<"\n DOUBLE: "<<DOUBLE;
cout<<"\n MI: " <<MI;
cout<<"\n NAME: "<<NAME[30]; }
main()
{ int INT; float FLOAT; double DOUBLE; char MI, NAME[30];
PRINT(INT, FLOAT, DOUBLE, MI, NAME);
system("PAUSE");
return 0;
}
-
I tried that..it's still not working
:(
-
sorry i made some changes to it, try it again, instead of "string Dummy" :rolleyes: try "char Dummy[30]"
-
...Still Unsuccessful..is this the only way of doing it? i also tried #define CB cin.ignore(cin.rdbuf()->in_avail())
//
....
....
...
cin>>MI; CB;
...
....
//
and it still doesn't work.
-
-
instead of clearBuffer() or #define CB (whatever) try just putting this in:
cout<<" Please enter a value for mi: "; cin>>MI;
cin.ignore();
cout<<" Please enter a value for Name: "; cin.getline(NAME,30);
assuming user only pushed enter key once after inputting data fro MI, then should work without stumbling over creating another function or writing a macro.