-
File I/O ..HELP!!
I'm Trying to read from a file- IStudent.dat, and i keep getting the wrong output- "invalid grade" ..what is wrong with my program?
the data file is at the bottom after the code.
Code:
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
#define Max 8
main()
{ int ID[Max], Test[Max], C; char Grade;
ifstream IStudent("IStudent.dat");
for(C=0;C<Max;C++)
{ IStudent>>Test[C];
if(Test[C]<100 && Test[C]>90)
{Grade='A';}
else if(Test[C]<89 && Test[C]>80)
{Grade='B';}
else if(Test[C]<79 && Test[C]>70)
{Grade='C';}
else if(Test[C]<69 && Test[C]>60)
{Grade='D';}
else if(Test[C]<59 && Test[C]>0)
{Grade='F';}
else
{cout<<"Invalid Grade"<<endl;}
}
system("PAUSE");
return 0;
}
Data File
Code:
8 Records in File
ID Test Score
-------------------------------------------------------
123 97
234 54
345 77
456 65
567 87
678 69
789 84
890 76
-
Look at my changes below in red.
Code:
int Id[Max];
for(C=0;C<Max;C++)
{
IStudent >> Id[C] >> Test[C];
if(Test[C]<=100 && Test[C]>=90) Grade='A';
else if(Test[C]<90 && Test[C]>=80) Grade='B';
else if(Test[C]<80 && Test[C]>=70) Grade='C';
else if(Test[C]<70 && Test[C]>=60) Grade='D';
else if(Test[C]<60 && Test[C]>=0) Grade='F';
else cout<<"Invalid Grade"<<endl;
}
You are reading in two values per line, an id and a score. You read the id and thinking it is a score, compare it in the if-else if portion of the code. The result is you get the "Invalid Grade" message because the value, 123 for example, isn't a valid score. You needed to add another array to keep track of the ids.
Also, your if-else if tests needed work because any values of 100, 90, 80, 70, 60, 0 would be counted as invalid due to how you were comparing the values.
-
Hey..Thanks hk_mp5kpdw, I made the changes, but I'm still getting an output of "Invalid Grade" the weird thing is that it prints out only SIX "Invalid Grade"s when I have 8 Scores. Ironically there are SIX if else statements. I have no idea why this is going on..any thoughts?
-
Check out the best File I/O tutorial on the net:
http://www.cpp-home.com/FileIO_tutorial.php
-
Thanks Guys..just one more question.. Is there a way to specify where I want ifstream to start validating from? Like say I have
Code:
8 Records in File
ID Test Score
-------------------------------------------------------
123 97
234 54
345 77
456 65
567 87
678 69
789 84
890 76
stored in a Data File, How can i get it to start reading from Line 6 and on..so as to read the Id's and Test Scores? Thanks for the Help!!
-
for input, include the flag ios::binary. This is how it's set up for your program:
Code:
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
#define Max 8
main()
{ int ID[Max], Test[Max], C; char Grade;
ifstream IStudent("IStudent.dat");
IStudent.setf(ios::binary);
for(C=0;C<Max;C++)
{ IStudent>>Test[C];
if(Test[C]<100 && Test[C]>90)
{Grade='A';}
else if(Test[C]<89 && Test[C]>80)
{Grade='B';}
else if(Test[C]<79 && Test[C]>70)
{Grade='C';}
else if(Test[C]<69 && Test[C]>60)
{Grade='D';}
else if(Test[C]<59 && Test[C]>0)
{Grade='F';}
else
{cout<<"Invalid Grade"<<endl;}
}
system("PAUSE");
return 0;
}
That's only if it's in C++, if it's in C, then I think it might still work.
I'm actually not sure if that is for input or output though.
-
>stored in a Data File, How can i get it to start reading from Line 6 and on..so as to read the Id's and Test Scores? Thanks for the Help!!
Maybe the easiest way is just to use getline() to skip over the first few lines.
char line[81];
.
.
IStudent.getline(line,81); //Skip a line
You could also use function ignore() to skip stuff.
-
I'm not sure if I understood that. I just wanna specify where to start reading the data from(Line 6)!!! lol. I know I didn't put these (marked red) in the right places.. where do i put them?
Code:
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
#define Max 8
main()
{ int ID[Max], Test[Max], C, CountA=0, CountB=0, CountC=0, CountD=0, CountF=0; char Grade;
ifstream IStudent("IStudent.dat");
char line[80];
for(C=0;C<Max;C++)
{ IStudent>>ID[C]>>Test[C];
IStudent.getline(line,80);
if(Test[C]<100 && Test[C]>=90)
{Grade='A'; CountA=CountA+1;}
else if(Test[C]<90 && Test[C]>=80)
{Grade='B'; CountB=CountB+1;}
else if(Test[C]<80 && Test[C]>=70)
{Grade='C'; CountC=CountC+1;}
else if(Test[C]<70 && Test[C]>=60)
{Grade='D'; CountD=CountD+1;}
else if(Test[C]<60 && Test[C]>=0)
{Grade='F'; CountF=CountF+1;}
else
{cout<<"Invalid Grade"<<endl;}
}
cout<<CountA<<"As"<<endl;
cout<<CountB<<"Bs"<<endl;
cout<<CountC<<"Cs"<<endl;
cout<<CountD<<"Ds"<<endl;
cout<<CountF<<"Fs"<<endl;
system("PAUSE");
return 0;
}
The Data File I'm reading from is
Code:
8 Records in File
ID Test Score
-------------------------------------------------------
123 97
234 54
345 77
456 65
567 87
678 69
789 84
890 76
-
Please Help- I'm so Tired, Lonely, and Depressed- all because of this little question mark I've got..please contribute to what will be a very short lived happy moment- one clear reply is all it takes..lol
-
If you want to start reading at line 6, do this:
Code:
ifstream IStudent("IStudent.dat");
char line[80];
for (int i=0; i<5; i++)
IStudent.getline(line,80);
//Now comes the real data
for(C=0;C<Max;C++)
{ IStudent>>ID[C]>>Test[C];
-
Or you could put five getline()'s in a row.
IStudent.getline(line,80);
IStudent.getline(line,80);
IStudent.getline(line,80);
IStudent.getline(line,80);
IStudent.getline(line,80);