Input students score

This is a discussion on Input students score within the C++ Programming forums, part of the General Programming Boards category; I currently want to input 2 text files and then output them to a text file that contains the summary ...

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Input students score

    I currently want to input 2 text files and then output them to a text file that contains the summary of the students marks. One of the text file is called studentsDay and the other one is called studentsEvening. Both text files contains the student ID, given name, surname, score1, score2, score3, score4, score5. The studentsDay is look as follows:
    Code:
    s1012 Anne Bennet       45 77 62 18 79
    s1236 Tony Bridges       81 83 89 95 63
    s2341 Michael Butcher   72 83 48 26 75
    The studentsEvening is look as follows:
    Code:
    s5454 Joe Sunshine      66 89 78 74 85
    s5466 Vanessa White   62 77 44 88 79
    s5553 Jim Sunder         66 74 48 53 95
    Now I want to output them to the file called report_regular.txt and it should contain student ID, surname, givenname, D/E, average score, grade and once the coding is done it should look like this:
    Code:
    s1012  Bennet, Anne        D  56.20  P
    s1236  Bridges, Tony       D  82.20  D
    s2341  Butcher, Michael    D  60.80  P
    s5454  Sunshine, Joe       E  78.40  D
    s5466  White, Vanessa      E  70.00  Cr
    s5553  Sunder, Jim         E  67.20  Cr
    I have already done my code and it look like this:
    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
      ifstream inFileDay;
      ifstream inFileEvening;
      ofstream outFile;
      
      // delcare other variables
     
      string studentdayId1;
      string studenteveningId1;
      string givenname, surname;
      char attendancemode;
      int score1, score2, score3, score4, score5;
      double average;
      string grade;
      
      inFileDay.open("studentsDay.txt");  
      inFileEvening.open("studentsEvening.txt"); 
      outFile.open("report_regular.txt");                                         
      
      outFile << fixed << showpoint;
      outFile << setprecision(2);
    
      inFileDay >> studentdayId1;
      inFileEvening >> studenteveningId1;
      outFile << studentdayId1;
      outFile << studenteveningId1;
      inFileDay >> surname;
      inFileEvening >> surname;
      inFileDay >> givenname;
      inFileEvening >> givenname;
      outFile << " " << givenname;
      outFile << ", " << surname;
      inFileDay >> attendancemode;
      inFileEvening >> attendancemode;
      outFile << "  " << attendancemode;
      inFileDay >> score1 >> score2 >> score3 >> score4 >> score5;
      inFileEvening >> score1 >> score2 >> score3 >> score4 >>  score5; 
      average = static_cast<double>(score1 + score2 + score3 + score4 + score5)/5.0;
      outFile << "  " << average << endl;
    
      while(grade = average);
      {
       if ( grade >= 85 )
    	cout << "HD";
       else
    	if ( grade >= 75 )
    		cout << "D";
    	else
    		if ( grade >= 65 )
    			cout << "Cr";
    		else
    			if ( grade >= 55 )
    				cout << "P";
    			else
    				cout << "F";                 
      }       
    
      inFileDay.close();
      inFileEvening.close();
      outFile.close();
                                                                              
      pause();
      return EXIT_SUCCESS;
    }
    Currently I can only output one student but not the other 5, I can only output the first evening student which is Joe Sunshine, I can output his student ID, his given name, surname, average score but I can't output his attendance mode and grade. Can somebody help me please.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here, I fixed some of what was wrong... I don't know why/how you got your code working the way you posted it, but here's how to do it in a better way:
    Code:
    #include <iostream>	//for the pause at the end
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
      ifstream inFileDay;
      ifstream inFileEvening;
      ofstream outFile;
      
      // delcare other variables
     
      string studentdayId1;
      string studenteveningId1;
      string givenname, surname;
      char attendancemode;
      int score1, score2, score3, score4, score5;
      double average;
      double grade;		//this won't work as a string
      
      inFileDay.open("studentsDay.txt");  
      inFileEvening.open("studentsEvening.txt"); 
      outFile.open("report_regular.txt",ios::trunc);     //put the 'trunc' flag in there                                    
    
      if(!outFile)	//make sure the file is opened
      {
    	  cerr<<"Error\n";	//if not, pring an error to the standard error output
    	  exit(1);	//exit with status 1
      }
      
      outFile << fixed << showpoint;
      outFile << setprecision(2);
    
      inFileDay >> studentdayId1;
      inFileEvening >> studenteveningId1;
      outFile << studentdayId1;
      outFile << studenteveningId1;
      inFileDay >> surname;
      inFileEvening >> surname;
      inFileDay >> givenname;
      inFileEvening >> givenname;
      outFile << " " << givenname;
      outFile << ", " << surname;
      inFileDay >> attendancemode;
      inFileEvening >> attendancemode;
      outFile << "  " << attendancemode;
      inFileDay >> score1 >> score2 >> score3 >> score4 >> score5;
      inFileEvening >> score1 >> score2 >> score3 >> score4 >>  score5; 
      average = static_cast<double>(score1 + score2 + score3 + score4 + score5)/5.0;
      outFile << "  " << average << endl;
    
      //while(average == grade)	//what was this for?
      //{
      if ( grade >= 85 )		//see above - grade can't be a string for this type of comparison
    	outFile << "HD";
      else if ( grade >= 75 )
    	outFile << "D";
      else if ( grade >= 65 )
    	outFile << "Cr";
      else if ( grade >= 55 )
    	outFile << "P";
      else
    	outFile << "F";                 
    //  }       
    
      inFileDay.close();
      inFileEvening.close();
      outFile.close();
              
      //cin.get();	//uncomment to put a pause in your program
      return 0;	//IMO, and that of Bjarne Stroustrup's, avoiding macros is good
    }
    and as a hint for reading in all the files: your logic assumes that both files have the same exact amount of students in them... first sort that out...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Ooops... while I was posting this, major_small also answered you.

    I don't know why/how you got your code working the way you posted it
    Well, not me either. I didn't get it to compile.

    Anyway, here is the code I came up with. it will load all the students (no matter how many) from your two files. I understand, too, that it may be a bit complicated, so may be you will not use it at all... But if it's something urgent, you can well make use of it if you like....

    Oh, but, anyway:

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <vector>
    using namespace std;
    
    typedef struct Student
    {
    	char AttMode;
    	int Score[5];
    	string FirstName, Surname, ID;
    	
    	double getAverageScore()
    		{
    			double Ans;
    			for(int i=0; i<5; i++)
    				Ans+=(double)Score[i];
    			
    			return (double)(Ans/(double)5.0);
    		}
    		
    	string getAverageGrade()
    		{
    			double Average=getAverageScore();
    			string Grade;
    			
    			if ( Average >= 85 )
    				Grade="HD";
    			else
    				if ( Average >= 75 )
    					Grade="D";
    				else
    					if ( Average >= 65 )
    						Grade="Cr";
    					else
    						if ( Average >= 55 )
    							Grade="P";
    						else
    							Grade="F"; 
    							
    							
    			return Grade;
    		}
    };
    
    int main()
    {
    	ifstream FileDay;
    	ifstream FileEvening;
    	ofstream FileOut;
    	
    	FileDay.open("studentsDay.txt");  
    	FileEvening.open("studentsEvening.txt"); 
    	FileOut.open("report_regular.txt");
    	
    	vector<Student> Students;
    	
    	while (!FileDay.eof())
    		{
    			Student NewStudent;
    			
    			FileDay>>NewStudent.ID>>NewStudent.FirstName>>NewStudent.Surname;
    			
    			for(int i=0; i<5; i++)
    				FileDay>>NewStudent.Score[i];
    			
    			NewStudent.AttMode='D';
    			
    			Students.push_back(NewStudent);
    		}
    		
    	while (!FileEvening.eof())
    		{
    			Student NewStudent;
    			
    			FileEvening>>NewStudent.ID>>NewStudent.FirstName>>NewStudent.Surname;
    			
    			for(int i=0; i<5; i++)
    				FileEvening>>NewStudent.Score[i];
    				
    			NewStudent.AttMode='E';
    			
    			Students.push_back(NewStudent);
    		}
    		
    
    	 //outputting
    	 vector<Student>::iterator Iter=Students.begin();
    	 
    	 FileOut<<fixed<<showpoint<<setprecision(2);
    	 
    	 while(Iter!=Students.end())
    	 	{
    			FileOut<<(*Iter).ID<<" "<<(*Iter).Surname<<", "<<(*Iter).FirstName;
    			FileOut<<" "<<(*Iter).AttMode<<" "<<(*Iter).getAverageScore();
    			FileOut<<" "<<(*Iter).getAverageGrade()<<endl;
    			
    			Iter++;
    		}
    	
    	FileDay.close();
    	FileEvening.close();
    	FileOut.close();
    
    	return EXIT_SUCCESS;
    }
    I'm sorry, I'm in a hurry so I didn't do much commenting, but just ask if something is unclear

    Of course, this code isn't perfect. There are several drawbacks:

    1. DO NOT have blank lines in the end of the two input files
    2. The aligning of names etc. is not implemented

    Hope this could help
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  4. #4
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Thanx MathFan, but after I have I complied and run your code, it comes up with all the zeros between the student name, average score, and grade. Also the average score is not the right one as it comes up with 164646946413131 etc. How can I fix this?

  5. #5
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I am having difficulty in showing the attendace mode (day, evening) for the student from the file I am reading from studentsDay.txt and studentsEvening.txt as I trying to output to the file called report_regular.txt
    The following is an examples of what should be look like:
    Code:
    s1012  Bennet, Anne        D  56.20  P
    s1236  Bridges, Tony        D  82.20  D
    s2341  Butcher, Michael    D  60.80  P
    s5454  Sunshine, Joe        E  78.40  D
    s5466  White, Vanessa     E  70.00  Cr
    s5553  Sunder, Jim           E  67.20  Cr
    Code:
    studentDay.txt is like:
    s1012 Anne Bennet       45 77 62 18 79
    s1236 Tony Bridges       81 83 89 95 63
    s2341 Michael Butcher   72 83 48 26 75
    Code:
    studentEvening.txt is like:
    s5454 Joe Sunshine      66 89 78 74 85
    s5466 Vanessa White   62 77 44 88 79
    s5553 Jim Sunder         66 74 48 53 95
    Currently I can show the section of studentID, surname, givenname, average score and grade but not the D or E. I need to put the attendace mode between the givenname and average score. I have done this code without the D or E:
    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
      ifstream inFileDay;
      ifstream inFileEvening;
      ofstream outFile;
      
      string studentdayId;
      string studenteveningId;
      string givenname, surname;
     
      int score1, score2, score3, score4, score5;
      int studentcounter;
      double Average;
    
      inFileDay.open("studentsDay.txt"); 
      inFileEvening.open("studentsEvening.txt");
      outFile.open("report_regular.txt");
    
      outFile.setf(ios::fixed, ios::floatfield);
      outFile.setf(ios::showpoint);
      outFile<<setprecision(2);
     
      outFile<<endl<<"+++++Section 1 Student List+++++"<<endl<<endl;
    
    while (!inFileDay.eof() )
        {
         
         inFileDay>>studentdayId;
         outFile<<studentdayId<<"  ";
         inFileDay>>givenname>>surname;
         outFile<<surname<<", ";
         outFile<<givenname<<" ";
         
         inFileDay>>score1>>score2>>score3>>score4>>score5;
     
         Average=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
       
         outFile<<Average<<"  ";
         outFile.setf(ios::right);
         
         if(Average>=85 && Average<=100)
           outFile<<"HD "<<endl;
         else if(Average>=75 && Average<=85)
           outFile<<"D "<<endl;
         else if(Average>=65 && Average<=75)
           outFile<<"Cr "<<endl;
         else if(Average>=55 && Average<=65)
           outFile<<"P "<<endl;
         else
           outFile<<"F "<<endl;
         
         
         studentcounter++;
         }
         
         while (!inFileEvening.eof() )
        {
         
         inFileEvening>>studenteveningId;
         outFile<<studenteveningId<<"  ";
         inFileEvening>>givenname>>surname;
         outFile<<surname<<", ";
         outFile<<givenname<<" ";
         inFileEvening>>score1>>score2>>score3>>score4>>score5;
     
         Average=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
       
         outFile<<setw(2)<<Average<<" ";
         outFile.setf(ios::right);
     
         if(Average>=85 && Average<=100)
           outFile<<"HD "<<endl;
         else if(Average>=75 && Average<=85)
           outFile<<"D "<<endl;
         else if(Average>=65 && Average<=75)
           outFile<<"Cr "<<endl;
         else if(Average>=55 && Average<=65)
           outFile<<"P "<<endl;
         else
           outFile<<"F "<<endl;
         
         studentcounter++;
         }
    
      inFileDay.close();
      inFileEvening.close();
      outFile.close();
                                                                                              
      pause();
      return EXIT_SUCCESS;
    }
    How can I show the D and E?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You might want to check out a recent thread discussing potential problems with the while condition in this liine:

    while (!inFileEvening.eof() )

    Check out pianorain's post at the end of this thread:

    Loop question

  7. #7
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I have just check the thread but it didn't help me. How can I show the D and E?

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In the last program you posted, do you know which lines are writing to the file?

  9. #9
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    The outFile in the while loop?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Yes. Can you write a short program that uses cout<< to output this information to a console window:

    string name = "Tom"

    a 'D'

    int score = 85;

  11. #11
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Don't I need outFile because I am writing to a text file?

  12. #12
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Keep trying I've done it, but I don't want to just post the code like I was doing before- since it is unethical of me.

    I believe you can do it.


  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by wiz23
    Don't I need outFile because I am writing to a text file?
    Don't worry about that right this second. Instead, post the code for outputing those 3 pieces of data to the console window.

  14. #14
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I think this wrong but is it
    cout << surname << givenname << endl;
    cout << D << endl;
    cout<< average << endl;

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, you could throw in a space:

    <<" "<<

    between the names, so it wasn't all one word, but that's good. You also want the input all on one line, so get rid of the endl's.

    Ok, cout is an output stream that is connected to your console window. outFile is an output stream connected to your file. To write to your file instead of the console window, substitute outFile for cout in those statements.
    Last edited by 7stud; 04-28-2005 at 08:52 PM.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 05:24 PM
  2. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  3. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  4. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM

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