0

The output for this program, thanks to you guys, is fixed. Except for the studentNumber. I read the comment that I never set a value to it and that confused me.

void process_file(ifstream& input)
    {
     int thisStudent = 0;

     StudentRecord student = StudentRecord(); 
        while (thisStudent++ < CLASS_SIZE)
        {
         student.input(input, thisStudent);
            student.computeGrade();
                student.output();
            }
        }

would this not set studentNumber equal to 0 then add +1 every time it runs through the loop.

 // Author: 
// Assignment 8
#include <iostream>
#include <fstream> 
#include <iomanip>
using namespace std;

ofstream     outputfile("output.txt");     

const int    MAX_FILE_NAME = 35;            
const int    CLASS_SIZE = 5;

class StudentRecord  
{
public:
void input( ifstream& input,int studentid);

void computeGrade();

void output();

private:
  int studentNumber;
  double exam1;
  double exam2;
  double exam3;
  double exam4;
  double average;
  char grade;
};


void open_input(ifstream& input, char name[]); 
void process_file(ifstream& input);            

int main() 
{  char again;             
   char file_name[MAX_FILE_NAME + 1];
   ifstream input_numbers;            

   cout << "This program can calculate the exam average and grade for\n"
        << "each student.\n" << endl;
   system("pause"); 

   do 
   {  
      system("cls");                          
      open_input(input_numbers, file_name);   
      process_file(input_numbers);             
      input_numbers.close();                   

      cout << "\nDo you want to process another file (Y/N)? ";
      cin >> again;
      cin.ignore(256, '\n');  

   } while ( again == 'y' || again == 'Y'); 

   cout << "\nEnd of Program!" << endl;
   outputfile << "\n\nThanks for using GradeCalc!\f"; 
   outputfile.close();

   return 0; 
}  

void process_file(ifstream& input)
{
 int thisStudent = 0;

 StudentRecord student = StudentRecord(); 
    while (thisStudent++ < CLASS_SIZE)
    {
     student.input(input, thisStudent);
        student.computeGrade();
        student.output();
    }
}

void open_input(ifstream& input, char name[]) 
{  int count = 0;             
   do 
   {  count++;
      if (count != 1)  
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." 
              << endl;
      }
      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(256, '\n');           
      input.clear();                  
      input.open(name,ios_base::in); 
    } while (input.fail() );          
} 

void StudentRecord::input(ifstream& input, int studentid)
{
      input >> exam1 >> exam2 >> exam3 >> exam4;

}

void StudentRecord::computeGrade()
{
    average = (exam1 + exam2 + exam3 + exam4) / 4 ;
      if (average >= 90)
        grade = 'A'; 
else if (average >=  80)
  grade = 'B';
else if (average >= 70)
  grade = 'C';
else if (average >= 60)
  grade = 'D';
else if (average < 60)
  grade = 'F';
}

void StudentRecord::output()
{  
   cout << "\n\nThe record for student number:" << setw(8) << studentNumber << endl;
   cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
   cout << "The numeric average is:" << setw(8) << average << endl;
   cout << "and the letter grade assigned is:" << setw(8) << grade << endl;
}
5
  • 1
    Sure you don't mean << endl; instead of >> endl; which I imagine would start complaining about integral types and overloads of endl Commented Nov 15, 2011 at 22:35
  • step back, create something much simpler (read one grade at a time) and then work forward from there. what you have will be too hard to debug here. Commented Nov 15, 2011 at 22:35
  • 1
    Voted to close as "too localized." This question asks about too many disparate problems; it cannot be generalized, so it won't be helpful to anyone but the people who have this specific homework assignment. Commented Nov 15, 2011 at 22:39
  • @RobKennedy - that's the purpose of my comment also Commented Nov 15, 2011 at 22:41
  • Why do you think your code would set studentNumber to something, @User? Show me the line that starts studentNumber =. Commented Nov 16, 2011 at 0:09

4 Answers 4

2

Well, studentNumber is garbage because you never put a value in it. So it just has whatever happened to already be in memory at that location.

The exam grades print out wrong because commas in C++ don't do what you think they do, and that's also why adding an endl; to it gives you an error.

The formatting I'm going to let you work out for yourself. You should consider reading up on output or at least doing some trial and error.

Sign up to request clarification or add additional context in comments.

Comments

2

One of the errors is that instead of this:

cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;

I think you mean this:

cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;

Comments

0

CLASS_SIZE is defined as 5, so this loop:

while (thisStudent++ < CLASS_SIZE)

will iterate 6 times.

Also

cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;

This outputs exam1, and then evaluates and does nothing with the rest of the variables.

Comments

-1

70 80 90 95 95 85 90 80 75 85 70 80 55 85 50 70 45 50 40 35

does it have the spaces? If yes, you need to ignore them. input >> exam1 >> exam2 >> exam3 >> exam4; would load space into one of the exam variables.

-- edit for MooingDuck --

#include <iostream>
#include <sstream>

using namespace std;

int main() {

    cout << "main() ENTRY" << endl;

    stringstream s1(ios_base::in | ios_base::out),
                 s2(ios_base::in | ios_base::out);

    int i = -1;

    s1 << "111 222";
    s1 >> i; cout << i << endl;
    s1 >> i; cout << i << endl;

    s2 << "111 222";
    s2 >> noskipws;
    s2 >> i; cout << i << endl;
    s2 >> i; cout << i << endl;

    return 0;
}

Output:

main() ENTRY
111
222
111
0

6 Comments

No; istreams use whitespace as separators.
@DavidThornley Assuming you're right in this case. Please note that C++ ref says "The specific way in which the data is parsed depends on the manipulators previously used on the stream and on its associated locale.".
@thekashyap: I don't know of any manipulators that make it not treat whitespace as seperators. Formatted input (such as input >> exam1) will always ignore whitespace.
@MooingDuck "I don't know of any manipulators" ==> see the edit
@thekashyap: I stand corrected. I've never heard of std::noskipws before.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.