File I/O ..HELP!!

This is a discussion on File I/O ..HELP!! within the C++ Programming forums, part of the General Programming Boards category; I'm Trying to read from a file- IStudent.dat, and i keep getting the wrong output- "invalid grade" ..what is wrong ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    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
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    Check out the best File I/O tutorial on the net:
    http://www.cpp-home.com/FileIO_tutorial.php
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  5. #5
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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 in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  6. #6
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  8. #8
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  9. #9
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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];

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21