| Techno01 (55) | |
|
when I execute it skeeps the name extraction like this way Enter the information of student nbr First name : Age : if you notice I can not enter the name thanks for reading int main() { studentInfo *sInfo; int sNbr; cout << "Enter your the number of the students -> ";cin >> sNbr; system("cls"); sInfo = new studentInfo[sNbr]; for(int i=0;i<sNbr;i++,sInfo++) { cout << "Enter the information of student nbr " << i+1 << endl; cout << "First name : " ;gets(sInfo->name); cout << "Age : " ;cin >> sInfo->age; cout << "Gender : " ;cin >> sInfo->gender;cout << endl; }sInfo-=sNbr; for(int i=0;i<sNbr;i++,sInfo++) { cout << "Information of student nbr " << i+1 << endl; cout << "First name is : " << sInfo->name << endl; cout << "Age is : " << sInfo->age << endl; cout << "Gender is : " << sInfo->gender << endl << endl; } delete sInfo; return 0; } | |
|
|
|
| Techno01 (55) | |
|
I forgot the head of the program thans again # include <iostream> # include <cstdio> # include <cstdlib> using namespace std; struct studentInfo { char name[20]; int age; char gender; }; | |
|
|
|
| Techno01 (55) | |
| No solutions ?! | |
|
|
|
| Moschops (5961) | ||||
|
gets will read from stdin until it finds a newline. You are leaving a newline there from previous operations, so it reads that in straightaway without you having to put any new data into the input buffer. If you compile your code using g++, you even get warned about how bad gets is:
Bad solution:
Good solution: DON'T USE gets | ||||
|
Last edited on
|
||||
| Techno01 (55) | |
|
Thank you so much reading but can I ask what those lines do int ch; while ((ch = getchar()) != '\n' && ch != EOF); | |
|
|
|
| Moschops (5961) | |
int ch; Create a new int variable, named ch.while ((ch = getchar()) != '\n' && ch != EOF);Read in using the getchar function and store the input into the variable ch nutil it's the end of a line or the end of a file. Don't use gets. | |
|
|
|
| ankit2313 (26) | |
| sorry to ask but i'd like to ask you Moschops that why did u use EOF when there is no file issue in the program just !='\n' is sufficient. | |
|
|
|
| Moschops (5961) | |
| It's generic input buffer flushing code. Since it's a bad solution anyway, it didn't really seem important. | |
|
|
|
| Cubbi (1583) | |
| It's important anyway: a user could enter EOF from keyboard and get this program stuck in an endless loop (although abusing gets() to read OP's passwords would be a more profitable use of the input access) | |
|
|
|
| ne555 (4041) | |
¿where is that endless loop? EOF will break the while@Moschops, please indent your code. | |
|
|
|
| Cubbi (1583) | |
|
@ne555 I meant that in response to @ankit2313's "why did u use EOF [...] just !='\n' is sufficient" but yes, I was thinking C++ for a bit there. getchar()'ing a EOF'd stdin does not always return immediately (like cin.get()'ting a eof'd cin) : the outcome depends on the OS and probably terminal settings. Without the EOF check, it loops forever on my Solaris, but reads the following characters on my AIX or Linux. | |
|
Last edited on
|
|
| Techno01 (55) | |
|
The program works perfectly well , thanks to you Moschops ,but I have couple of questions: the first one is : I have a hard time to understand the relation between the while loop and gets() , I think the while loop dose not ends until It gets a caractere different than '\n' or EOF,then gets the name , am I right? | |
|
|
|