But one more problem has has occurred now (i think this one is due to that #7 point you
stated) -
now when the the program asks for second student it skips the name part.
No, that's because of what I mentioned in #6...
Code:
void getdata()
{
cout<<"\nenter name "; gets(name);
cout<<"enter grade "; cin>>grade;
cout<<"enter marks "; cin>>marks;
cout<<"enter roll no "; cin>>rn;
}
Processing input works differently depending on what you are trying to read. When you reach your cin>>rn code, you enter a numeric value and press the return key. The value is converted and stored into the rn variable but the newline is kept in the input buffer and can be read/processed in subsequent input operations. This is where the rules can be confusing... if you were to read another numeric value at this point, the existing newline in the input buffer would be skipped and the input operation would block (wait for input) until you entered another number and then pressed the enter key once more. But, in your loop you go back to the gets call which read character input instead of numeric input. The processing of character data here is handled differently than if you were trying to read a number for instance. Here, the existing newline that is still in the buffer from the previous cin>>rn statement is interpreted as you - the user - having pressed the enter key without really entering anything. Nothing gets stored in the name field and the effect is that your code appears to skip that statement altogether. This fault is therefore due to the newline that is left over from a previous input. What needs to happen - probably after the cin>>rn statement is you must throw away the trailing newline in the input buffer so that when you next attempt to read the name it does not skip over that line. This can be done in a couple ways one of which is:
Code:
void getdata()
{
...
cout<<"enter roll no "; cin>>rn;
cin.get(); // Eat up the leftover newline character
}
Another way to deal with a leftover newline would be to call cin.ignore(). With this in place, your second (and subsequent) calls to the getdata function will not skip the input of the name field.
************************************************** *****************
Can teach me a little more about fgets?
Its header file and parameters and anything that i need to know.
fgets is prototyped in the stdio.h/cstdio headers. It would be used as such:
Code:
fgets(name,sizeof(name),stdin);
For your program I would recommend cin.getline instead:
Code:
cin.getline(name,sizeof(name));
...this is a C++ solution and therefore fits more with the rest of your program. Putting everything together, I would have the getdata function look as such:
Code:
void getdata()
{
cout<<"\nenter name "; cin.getline(name,sizeof(name));
cout<<"enter grade "; cin>>grade;
cout<<"enter marks "; cin>>marks;
cout<<"enter roll no "; cin>>rn;
cin.get();
}
************************************************** *****************
#3
I know about them. its just that i copied the code from my textbook in hurry.
but i think windows is needed for system("pause") although i won't use it again.
No, see #4:
In addition to the headers you've already got, I believe the
system function needs the
stdlib.h/cstdlib header - pick one (hopefully the later).
If you've made the change mentioned above to deal with the skipping of the input, then all you would need to replace a system call is another cin.get() call at the bottom of the main function.
************************************************** *****************
#6
I don't have any idea about what you are saying. Can you explain and give solution
for it since i think this is were the problem is?
Already done in the first part of this post.
************************************************** *****************
Change the display function from:
Code:
void display()
{ cout<<"\nName ";puts(name);
cout<<"Grade "<<grade;
cout<<"\nMarks "<<marks;
cout<<"\nroll no "<<rn;
}
To:
Code:
void display()
{ cout<<"\nName " << name;
cout<<"\nGrade "<<grade;
cout<<"\nMarks "<<marks;
cout<<"\nroll no "<<rn;
}
************************************************** *****************
One more thing i need to know can write whole array through one write f(x)
after calling getdata() for all objects(same goes for read())?
Well, you currently call write once in each iteration of the loop. If you want a single write call, you could move it to after the loop and instead of writing one chunk of data simply write 5 chunks worth (one for each student in the array) all at once... you control how many bytes are written in that call, it is after all the second parameter to that particular function. Something similar can be done when reading from the file.