Thread: Printing contents of an input file to the screen

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

    Printing contents of an input file to the screen

    My program is supposed to read from this output file and print the contents to the screen :
    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

    I have a read until end of file command and all the necessary other commands.
    I read in the first id number as Student_id and then if the id is not equal to the new id read in then it calculated the grades for the student and outputs it to the screen and then continues to read the rest of the data entered.
    This is what I have for that section of code:
    Code:
     if(Student_id!=New_id)
    {
    
            quiz.Average=quiz.Tot_Ach/quiz.Tot_Poss;
    
            prog.Average=prog.Tot_Ach/prog.Tot_Poss;
    
            Poss_Score= quiz.Tot_Poss+prog.Tot_Poss+mid.Tot_Poss+fin.Tot_Poss;
    
            Acheived_Score=quiz.Tot_Ach+prog.Tot_Ach+mid.Tot_Ach+fin.Tot_Ach;
    
            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;
    }
    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    struct Type
    {
     double Poss_Score;
     double Acheived_Score;
     double Tot_Poss;
     double Tot_Ach;
     double Average;
    }quiz,prog,mid,fin;
    
    int main()
    
    {
    
     string Course_Name;
     int Student_id;
     int New_id;
     string Lastname;
     string Firstname;
     char Assignment_Type;
    
     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>>quiz.Poss_Score>>quiz.Acheived_Score;
    
                            quiz.Tot_Poss+=quiz.Tot_Poss;
                            quiz.Tot_Ach+=quiz.Tot_Ach;
                    break;
    
                    case 'P':
                            myfile>>prog.Poss_Score>>prog.Acheived_Score;
    
                            prog.Tot_Poss+=prog.Poss_Score;
                            prog.Tot_Ach+=prog.Tot_Ach;
                    break;
    
                    case 'M':
                            myfile>>mid.Poss_Score>>mid.Acheived_Score;
    
                            mid.Tot_Poss+=mid.Poss_Score;
                            mid.Tot_Ach+=mid.Acheived_Score;
                    break;
    
                    case 'F':
                    while(!myfile.eof())
    {
    
    
            getline(myfile,Course_Name);
    
            myfile>>Student_id;
    
            myfile.get();
            myfile>>Lastname;
            myfile.get();
            myfile.get();
    
            myfile>>Firstname;
            myfile.get();
    
            myfile>>Assignment_Type;
    
    
            switch(Assignment_Type)
            {
                    case 'Q':
                            myfile>>quiz.Poss_Score>>quiz.Acheived_Score;
    
                            quiz.Tot_Poss+=quiz.Tot_Poss;
                            quiz.Tot_Ach+=quiz.Tot_Ach;
                    break;
    
                    case 'P':
                            myfile>>prog.Poss_Score>>prog.Acheived_Score;
    
                            prog.Tot_Poss+=prog.Poss_Score;
                            prog.Tot_Ach+=prog.Tot_Ach;
                    break;
    
                    case 'M':
                            myfile>>mid.Poss_Score>>mid.Acheived_Score;
    
                            mid.Tot_Poss+=mid.Poss_Score;
                            mid.Tot_Ach+=mid.Acheived_Score;
                    break;
    
                    case 'F':
                            myfile>>fin.Poss_Score>>fin.Acheived_Score;;
    
                            fin.Tot_Poss+=fin.Poss_Score;
                            fin.Tot_Ach+=fin.Acheived_Score;
                    break;
    
                    default:cout<<"Invald Input";
    
            }
            }
    
      myfile>>New_id;
    
      if(Student_id!=New_id)
    {
    
            quiz.Average=quiz.Tot_Ach/quiz.Tot_Poss;
    
            prog.Average=prog.Tot_Ach/prog.Tot_Poss;
    
            Poss_Score= quiz.Tot_Poss+prog.Tot_Poss+mid.Tot_Poss+fin.Tot_Poss;
    
            Acheived_Score=quiz.Tot_Ach+prog.Tot_Ach+mid.Tot_Ach+fin.Tot_Ach;
    
            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;
    }
    }
    Last edited by babe20042004; 11-24-2009 at 12:58 PM. Reason: Indentation

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    do some indentation please seņor

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I assume this is a continuation of these threads:

    Help with taking input from a file
    Editing and Changing c++ text output files

    Please be more specific about what problem you are having. Ask a specific question. Explain what error you are getting, is it a compiler error, or are you getting unexpected output? Or are you needing help finishing a certain part of the code? You really should provide more information than just posting the code if you want to have the best chance at actually getting some assistance.

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. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Problem With search a file and printing to screen
    By sell682 in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2004, 05:55 AM