In the following program i am inputting data form file,
file contains : 1st line: answer key, subsequent line contains students ID and their answer response.
in the program i am comparing answer key and student response key and calculating grade and marks.
after reading id i need to discard space after the id, for that i need to read one character but while reading one character from the file with char
variable it is reading space but also eating away next character so i am left with 9 char length student answer key, but it should be 10 char long.
and the while loop inside int main()
which contain display function should run only one time because i have only one record in the file, but it is running 2 times.
what is wrong in this?
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
class student{
string ans,usn;
int marks,tot;
char grade;
public:
void input(string key,fstream &fp){
char ch;
marks=tot=0;
grade='z';
fp>>usn>>ch;
getline(fp,ans);
string::iterator it1,it2;
for(it1=key.begin(),it2=ans.begin()+1;it1!=key.end();++it1,++it2){
tot+=2;
if(*it1==*it2)
marks+=2;
else if(*it1!=*it2&&*it2!=' ');
//marks-=1;
}
grade=marks<0?'z':(75-(marks*10/tot));
}
void display(){
cout<<left<<setw(20)<<usn<<setw(20)<<ans<<setw(10)<<marks<<setw(10)<<grade<<ans.length()<<" "<<tot<<endl;
}
};
int main(){
student s;
string key;
char ch;
fstream fp("file.txt");
if(!fp){
cout<<"\nError in opening file"<<endl;
exit(0);
}
getline(fp,key);
cout<<left<<"key:"<<key<<endl<<setw(20)<<"usn"<<setw(20)<<"Answer key"<<setw(10)<<"marks"<<setw(10)<<"grade"<<endl<<setfill('-')<<setw(60)<<"-"<<setfill(' ')<<endl;
while(!fp.eof()){
s.input(key,fp);
s.display();
}
fp.close();
return 0;
}
file:
TTTTTTTTTT
1rv16is089 FTTTTTTTTT
output:
key:TTTTTTTTTT
usn Answer key marks grade
------------------------------------------------------------
1rv16is089 TTTTTTTTT 16 C 9 20
1rv16is089 TTTTTTTTT 16 C 9 20