I am trying to create a simple grade storing console. The console allows you to enter grades or view them. When I try to view the grades I have entered, it always asks me if I would like to enter or view grades, like in the beginning. help, please!
Source Code
#include <iostream>
usingnamespace std;
//These are all the variables I want to store.
int enterorsee;
int whichstudent;
int whichstudentsee;
string name1;
string name2;
string name3;
int whichassign;
int assign1grade1;
int assign1grade2;
int assign1grade3;
int assign1pp1;
int assign1pp2;
int assign1pp3;
int assign2grade1;
int assign2grade2;
int assign2grade3;
int assign2pp1;
int assign2pp2;
int assign2pp3;
int assign3grade1;
int assign3grade2;
int assign3grade3;
int assign3pp1;
int assign3pp2;
int assign3pp3;
int Finalgrade1;
int Finalgrade2;
int Finalgrade3;
int i;
//This is where I will put all my code
int main()
{
cout<< "Welcome to the GMC. Type in 1 to enter grades or 2 to look at them. ";
while (1) {
cout<< "Would you like to enter grades or look at existing ones?\n ";
cin >> enterorsee;
if (enterorsee == 1) {
cout << "Which student would you like to enter grades for?\n";
cin >> whichstudent;
if (whichstudent == 1) {
if (name1 == "") {
cout<< "What is Student Ones name?\n";
cin >> name1;
}
else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp1;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp1;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp1;
}
}
}
if (whichstudent == 2) {
if (name2 == "") {
cout<< "What is Student Twos name?\n";
cin >> name2;
}
else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp2;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp2;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp2;
}
}
}
if (whichstudent == 3) {
if (name2 == "") {
cout<< "What is Student Threes name?\n";
cin >> name3;
}
else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp3;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp3;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp3;
}
if (enterorsee == 2) {
cout<<"Which student's grades would you like to see?\n";
cin >> whichstudentsee;
if (whichstudentsee == 1) {
cout << name1;
cout << " got a";
cout << assign1grade1 / assign1pp1 * 100;
cout << " percent on his/her first assignment,\n";
cout << name1;
cout << " got a";
cout << assign2grade1 / assign2pp1 * 100;
cout << " percent on his/her second assignment,\n";
cout << name1;
cout << " got a";
cout << assign3grade1 / assign3pp1 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade1+assign2grade1+assign3grade1) / (assign1pp1+assign2pp1+assign3pp1) * 100;
cout << " in this class.\n";
}
if (whichstudentsee == 2) {
cout << name2;
cout << " got a";
cout << assign1grade2 / assign1pp2 * 100;
cout << " percent on his/her first assignment,\n";
cout << name2;
cout << " got a";
cout << assign2grade2 / assign2pp2 * 100;
cout << " percent on his/her second assignment,\n";
cout << name2;
cout << " got a";
cout << assign3grade2 / assign3pp2 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade2+assign2grade2+assign3grade2) / (assign1pp2+assign2pp2+assign3pp2) * 100;
cout << " in this class.\n";
}
if (whichstudentsee == 3) {
cout << name3;
cout << " got a";
cout << assign1grade3 / assign1pp3 * 100;
cout << " percent on his/her first assignment,\n";
cout << name3;
cout << " got a";
cout << assign2grade3 / assign2pp3 * 100;
cout << " percent on his/her second assignment,\n";
cout << name3;
cout << " got a";
cout << assign3grade3 / assign3pp3 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade3+assign2grade3+assign3grade3) / (assign1pp3+assign2pp3+assign3pp3) * 100;
cout << " in this class.\n";
}
}
}
}
}
}
}
With that amount of if statements it's pretty hard to tell what's going on.
Please put [ Code] [ /Code] statements about your code so it is easier to read.
Well one of the main problems is your lack of indentation right now (which is actually probably causing the problem because of your {} not matching up correctly.
For example you should do
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while (1)
{
cout<< "Would you like to enter grades or look at existing ones?\n ";
cin >> enterorsee;
if (enterorsee == 1)
{
cout << "Which student would you like to enter grades for?\n";
cin >> whichstudent;
if (whichstudent == 1)
{
if (name1 == "")
{
cout<< "What is Student Ones name?\n";
}
}
}
(This might not be exactly how you have your code set up right now but you get the idea). It's almost impossible for people to review your code when it is all in 1 line and your teacher wont like it either.
hard to read without indentation, but what i see so far is that the number of open and close curly brackets between line 41 to 131 don't match up. Your indentation at line 132 should line up with the one at 41, so your code starting at line 132 if(enterorsee == 2) wil never happen because it is inside the if(enterorsee == 1) condition.
You need to learn how to use arrays and functions.
You're doing the same time over and over. The only thing that changes is the student number and the assignment number. Breaking your program into functions helps to reduce the problem massively nested ifs.
For example:
1 2 3 4
if (enterorsee == 1)
enter_student();
if (enterorsee == 2)
see_student();
Each function does one thing.
Instead of creating a separate variable for each grade. Use an array.
1 2 3 4
constint MAX_GRADES = 3;
int grade[MAX_GRADES];
...
cin >> grade[whichassign];