I can't figure this out.

I can't seem to figure this one out. Trying to create a class that allows the user to input information about a student and tally up the final grade and letter grade as shown below. Afterwards, it displays the information and writes it to the file. Help!!!


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

int main ()
{
string filename = "students.dat";

char name = 0;
char letterGrade = 0;
double finalGrade = 0;
double exam1 = 0;
double exam2 = 0;
double homeWork = 0;
double finalExam = 0;

ofstream outFile;


while (name != 'Q')
{
cout << "Enter the student's name or enter the letter Q to quit: " << endl;
cin >> name;
cout << "Enter the student's grade for Exam 1: " << endl;
cin >> exam1;
cout << "Enter the student's grade for Exam 2: " << endl;
cin >> exam2;
cout << "Enter the student's homework average: " << endl;
cin >> homeWork;
cout << "Enter the student's Final Exam Grade: " << endl;
cin >> finalExam;

finalGrade = (0.20 * exam1) + (0.20 * exam2) + (0.35 * homeWork) + (0.25 * finalExam);

if (finalGrade >= '90')
letterGrade = 'A';
if (finalGrade >= '80')
letterGrade = 'B';
if (finalGrade >= '70')
letterGrade = 'C';
if (finalGrade >= '60')
letterGrade = 'D';
if (finalGrade < '60')
letterGrade = 'F';

outFile << name << exam1 << exam2 << homeWork << finalExam << finalGrade << letterGrade << endl;

}

cout << name << exam1 << exam2 << homeWork << finalExam << finalGrade << letterGrade << endl;

return 0;
}


http://en.wikipedia.org/wiki/Class_(computer_science) :
"A class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share."

http://en.wikipedia.org/wiki/Computer_program :
"Computer programs (also software programs, or just programs) are instructions for a computer."

<cstdlib> is not used.
<cmath> is not used.
<algorithm> is not used.
doubles are unnecessary. Use floats instead.
char name...; A char is an integral constant, not a string. Use std::string, whose header I see you have already included.
name=0; By intializing 'name' to zero, the while loop will never execute. However, since you've already changed the type of 'name' to std::string, you can leave it uninitialized.
Make the loop infinite (put a 1 in the condition), and instead perform a check after the name is entered.
while (name != 'Q') While you have correctly used single quotes to compare with a char, this is now an std::string, so the condition is incorrect. This is what you should use after entering the name:
1
2
if (!strcmp(name.c_str(),"Q") || !strcmp(name.c_str(),"q"))
    break;

You never open outFile with something like std::ofstream outFile("out.txt");

I spared you because the program was short. I should have asked you to refine your question.
Last edited on
Topic archived. No new replies allowed.