-1

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
Rahul
  • 945
  • 7
  • 22
  • 2
    I think it's time you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Nov 26 '17 at 11:47
  • 2
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Mat Nov 26 '17 at 11:47
  • sorry, but tried to every thing, but why `char` will eat 2 characters? – Rahul Nov 26 '17 at 11:50
  • char doesn’t eat 2 characters! –  Nov 26 '17 at 11:53
  • @manni66 i know that, thats why i asked, instead of `char ch; fp>>ch` if i do this `char ch[1]; fp.read(ch,1);` it is working fine. but in the program `char` eating two character – Rahul Nov 26 '17 at 11:59
  • @Some programmer dude i tried to debug, but i just could not, asking here is the last thing i want to do. – Rahul Nov 26 '17 at 12:00
  • http://www.cplusplus.com/reference/ios/skipws/ –  Nov 26 '17 at 12:02
  • 1
    @Rahul What do you mean by saying that you could not debug? Debugging is the act in which everyone finds bugs in their code. If you do not know how - suggest you learn it, it is an essential skill for any developer. – Algirdas Preidžius Nov 26 '17 at 12:04
  • @AlgirdasPreidžius i mean i wasn't able to fine the solution, i checked every variable but the `char` variable behaves unexpectedly, thats why i posted question, if debugging was this easy the why do people ask question here. – Rahul Nov 26 '17 at 12:08
  • @Rahul "_if debugging was this easy the why do people ask question here._" From what I've seen - they are too lazy to learn how to use debuggers. – Algirdas Preidžius Nov 26 '17 at 12:10
  • @AlgirdasPreidžius man! please help if you could, no one i lazy the are trying their best but not all is as capable as you, and do not have infinite time, if you know something then please answer it, it will save time for both of us. but sometime people just downvote without fully understanding the meaning, and hit me with 5 down votes then its over before other could understand the question – Rahul Nov 26 '17 at 12:14
  • @Rahul SO is meant to be the last place where people ask, after trying out everything, but, as it is often the case, people ask without even trying to debug. You don't need infinite time, you only need to learn how to use the tool, that you will use for every day of your working career, anyway. That's pretty decent investment of time. 1) So, having said that: what value does `ch` contain, after reading from the stream? If you used debugger, you would know. 2) Please read the documentation on how [std::istream::operator >>](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) works. – Algirdas Preidžius Nov 26 '17 at 12:26

1 Answers1

2

Unless you have specifically asked the stream not to (noskipws) all the operator>> in the standard library will automatically skip all leading whitespace before starting to read its input.

So your fp >> ch will first skip all spaces and then read one more character.

There is a special manipulator std::ws that can be used to eat whitespace but not anyhting else. Just do fp >> std::ws;.

However, you also have a few other issues. When mixing >> and std::getline you might consider this, which probably is the base problem you tried to solve:

Why does std::getline() skip input after a formatted extraction?

And your problem with main() seeming to read one record too many is explained here:

Why is iostream::eof inside a loop condition considered wrong?

Bo Persson
  • 86,087
  • 31
  • 138
  • 198