Help with taking input from a file

This is a discussion on Help with taking input from a file within the C++ Programming forums, part of the General Programming Boards category; The following program should take the following input from this file and calculate the average for Q-Quizzes, P-Programming Assignments and ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Help with taking input from a file

    The following program should take the following input from this file and calculate the average for Q-Quizzes, P-Programming Assignments and also calculate the final percentage for each student. Somehow I cant get it to detect the two names and calculate correct averages and percentage.
    This is the content of the input file
    CPTR151‐01
    000143 Doe John Q 10 5
    000143 Doe John Q 10 7
    000143 Doe John Q 10 9
    000143 Doe John P 20 17
    000143 Doe John P 20 15
    000143 Doe John M 50 45
    000143 Doe John F 50 40
    991253 Dough Jane Q 10 10
    991253 Dough Jane Q 10 6
    991253 Dough Jane Q 10 2
    991253 Dough Jane P 20 20
    991253 Dough Jane P 20 7
    991253 Dough Jane M 50 35
    991253 Dough Jane F 50 47.5

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    int main()
    
    {
     string Course_Name;
     int Student_id;
     int New_id;
     string Lastname;
     string Firstname;
     char Assignment_Type;
    
     double Poss_Quiz_Score;
     double Acheived_Quiz_Score;
     double Tot_Poss_Quiz;
     double Tot_Ach_Quiz;
     double Quiz_Average;
    
     double Poss_Prog_Score;
     double Acheived_Prog_Score;
     double Tot_Poss_Prog;
     double Tot_Ach_Prog;
     double Prog_Average;
    
     double Poss_Mid_Score;
     double Acheived_Mid_Score;
     double Tot_Poss_Mid;
     double Tot_Ach_Mid;
    
     double Poss_Final_Score;
     double Acheived_Final_Score;
     double Tot_Poss_Fin;
     double Tot_Ach_Fin;
    
    
     double Poss_Score;
     double Acheived_Score;
     double Per;
    
     //Section 1- This section reads the information from the input file and assigns appropriate variable names.
     ifstream myfile;
     myfile.open("student_info.dat");
     while(!myfile.eof())
    {
    
    
            getline(myfile,Course_Name);
    
            myfile>>Student_id;
    
            myfile.get();
    
            myfile>>Lastname;
            myfile.get();
    
            myfile>>Firstname;
            myfile.get();
    
            myfile>>Assignment_Type;
    
    
            switch(Assignment_Type)
            {
                    case 'Q':
                            myfile>>Poss_Quiz_Score>>Acheived_Quiz_Score;
    
                            Tot_Poss_Quiz+=Tot_Poss_Quiz;
                            Tot_Ach_Quiz+=Tot_Ach_Quiz;
                    break;
    
                    case 'P':
                     myfile>>Poss_Prog_Score>>Acheived_Prog_Score;
    
                            Tot_Poss_Prog+=Poss_Prog_Score;
                            Tot_Ach_Prog+=Tot_Ach_Prog;
                    break;
    
                    case 'M':
                            myfile>>Poss_Mid_Score>>Acheived_Mid_Score;
    
                            Tot_Poss_Mid+=Poss_Mid_Score;
                            Tot_Ach_Mid+=Acheived_Mid_Score;
                    break;
    
                    case 'F':
                            myfile>>Poss_Final_Score>>Acheived_Final_Score;
                      myfile>>Poss_Final_Score>>Acheived_Final_Score;
    
                            Tot_Poss_Fin+=Poss_Final_Score;
                            Tot_Ach_Fin+=Acheived_Final_Score;
                    break;
    
                    default:cout<<"Invald Input";
    
            }
    
      myfile>>New_id;
    
      if(Student_id!=New_id)
    {
    
            Quiz_Average=Tot_Ach_Quiz/Tot_Poss_Quiz;
    
            Prog_Average=Tot_Ach_Prog/Tot_Poss_Prog;
    
            Poss_Score= Tot_Poss_Quiz+Tot_Poss_Prog+Tot_Poss_Mid+Tot_Poss_Fin;
    
            Acheived_Score=Tot_Ach_Quiz+Tot_Ach_Prog+Tot_Ach_Mid+Tot_Ach_Fin;
    
            Per=(Acheived_Score/Poss_Score)*100/1;
    
            cout<<Firstname<<" "<<Lastname<<endl;
            cout<<"Quiz Average: "<<Quiz_Average<<endl;
            cout<<"Programming Average: "<<Prog_Average<<endl;
            cout<<"Final Percentage: "<<Per;
    }
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    What problem are you having? Do you get a compiler error? Incorrect output?

    Why are you getting the course name inside the while loop? It only appears once at the top of the file.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Last I checked, averages are not calculated by dividing the points by possible points. In order for you to do this assignment, grade point averages had to be explained. I remember when I did an assignment similar to this, ten quizzes equaled one quarter test, and the final counted twice, so you had to calculate something like:

    ( (q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10) + (t1 + t2 + t4) + (final + final)) / 6.0

    You left that specific information out, so I would look over the instructions again to figure out what you need to calculate.

    And, if possible, work it out in a spreadsheet before you write the program. That helped me gain some clarity.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You are using eof to control your loop. This is problematic. You'll end up processing the last line from the file twice and the Student_id and New_id are not likely to be different so the last "person" in the file isn't going to have your nice summary output for them. I'd suggest a loop that looks more like:
    Code:
    getline(myfile,Course_Name);
    while( myfile >> Student_id >> Lastname >> Firstname >> Assignment_Type >> Poss_Score >> Achieved_Score )
    {
        // Do your calculations here
    
        // Do your summary output here
    }
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

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