Programming grades

hey guys, I'm making this program which requires the user to enter each grade for each assignment listed so far. What I want to do is, for example..add all the homeworks together and give it a new name, which is going to be "total hw". and so on for the rest(quizes, tests, and extra credit). here's my program. let me know if you understand my question.

#include <iostream>
namespace std;
int main ()
{
int totalhw = 0, int totalquiz = 0, int totaltest = 0, int totalec = 0, int grade = 0;

cout << ("Enter your grade for homework 1: ");
cin >> hw1;
cout << ("Enter your grade for homework 2: ");
cin >> hw2;
cout << ("Enter your grade for homework 3: ");
cin >> hw3;
cout << ("Enter your grade for homework 4: ");
cin >> hw4;
cout << ("Enter your grade for homework 5 ");
cin >> hw 5;
cout << ("Enter your grade for homework 6: ");
cin >> hw 6;

cout << ("Enter your grade for quiz 1: ");
cin >> quiz1
cout << ("Enter your grade for quiz 2: ");
cin >> quiz2;
cout << ("Enter your grade for quiz 3: ");
cin >> quiz3;

cout << ("Enter your grade for test 1: ");
cin >> test1;
cout << ("Enter your grade for test 2: ");
cin >> test 2;

cout << ("Enter your grade for extra credit 1: ");
cin >> ec1;
cout << ("Enter your grade for extra credit 2: ");
cin >> ec2;
cout << ("Enter your grade for extra credit 3: ");
cin >> ec3;
cout << ("Enter your grade for extra credit 4: ");
cin >> ec4;
cout << ("Enter your grade for extra credit 1: ");
cin >> ec5;
I don't really get what you're asking but I have to say that your code is seriously long-winded. Have you read about arrays or loops yet?

I would take the time to study them because if you understand them you can turn all that redundant code into something like this:

1
2
3
4
5
6
7
int homework[6];  // array 

	for (int i = 0; i < 6; i++) {   // count controlled loop
		cout << "Enter your grade for homework " << i+1 << ": ";
		cin >> homework[i];  // note: arrays start at 0, not 1  
	}


If you merely want to add the homework scores just declare the array int hw[6];

Then fill it up: (arrays start at zero by default)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << ("Enter your grade for homework 1: ");
cin >> hw[0];
cout << ("Enter your grade for homework 2: ");
cin >> hw[1];
cout << ("Enter your grade for homework 3: ");
cin >> hw[2];
cout << ("Enter your grade for homework 4: ");
cin >> hw[3];
cout << ("Enter your grade for homework 5 ");
cin >> hw[4];
cout << ("Enter your grade for homework 6: ");
cin >> hw[5];


for (int i = 0; i < 6; i++)
      totalhw += hw[i];    //adds them all up (totalhw += is the same as totalhw = totalhw + ) 



P.S.- you have a typo on the second to last line and it doesn't look like you declared any of the variables you are using to store the grades.
Last edited on
Topic archived. No new replies allowed.