Trying to have a user input their grades as letter values and then assigning the letters numbers to be saved in an array. For some reason I can't get case 1 to work correctly. All it does is ask for the lab and then the grade, but once you input a letter (i.e. A, B, C, D, F) it gets in an infinite loop.

#include <iostream>									//References iostream library to be used
using namespace std;								//Makes the library available

void main()
{

	int Counter=1;			//Counter for letter nested statement
	int Assignment[7]= {0, 0, 0, 0, 0, 0, 0};	//Array
	int Selection, Lab, LabGrade, Count;	//Declare variables
	char A, B, C, D, F;		//Assignment letter variables
	float GPA;				//float variable to allow for decimals
	float Divisor = 7.0;

							//Selection menu for grades


	cout << "Please select for pay type from the following choices:\n";
	cout << "    1 - Input Letter Grade Data\n";
	cout << "    2 - Lab Average\n";
	cout << "    3 - Print Lab Grades to Screen\n";
	cout << "    0 - Exit\n";
	cin >> Selection;

	while (Selection != 0)
	{
		switch (Selection)
		{
		case (1):
	

			while
			{
				cout << "Enter lab number (1-7): \n";
				cin >> Lab;
				cout << "Enter current grade for lab: \n";
				cin >> LabGrade;

				if (LabGrade = 'A' && 'a')
				{
					A = 4;
				}
				else
				{
					if (LabGrade = 'B' && 'b')
					{
						B = 3;
					}
					else
					{
						if (LabGrade = 'C' && 'c')
						{
							C = 2;
						}
						else
						{
							if (LabGrade = 'D' && 'd')
							{
								D = 1;
							}
							else
							{
								if (LabGrade = 'F' && 'f')
								{
									F = 0;
								}
							}
						}
					}
				}
			}
			Assignment[Lab-1] = LabGrade;

			cout << "Current lab " << Lab << ", is now " << LabGrade << ".\n\n";
			break;


		case (2):
			GPA = 0.0;

			for (Count=0; Count <=6; Count++)
			{
				GPA = GPA + Assignment[Count];
			}
			cout << "Current lab average is " << GPA / Divisor << ".\n\n";
			break;


		case (3):
			Count = 0;
			
			while (Count <= 6)
			{
				cout << "Lab " << Count+1 << " grade is " << Assignment[Count] << ".\n";
				Count++;
			}
			break;


		default:
			cout << "Enter correct menu selection.\n";
		}
	cout << "    1 - Input Letter Grade Data\n";
	cout << "    2 - Lab Average\n";
	cout << "    3 - Print Lab Grades to Screen\n";
	cout << "    0 - Exit\n";
	cin >> Selection;
	}
}
if (LabGrade = 'A' && 'a')

While this is a valid C++ statement, it has several problems:
- = is an assignment operator. You probably want to use the comparison operator, == .
- The '&&' operator is being used incorrectly. If you want to see if 'LabGrade' is something and/or something else, you must write two expressings, i.e. 'LabGrade == 'A' && LabGrade =='a'
- The '&&' operator won't do what you want because 'LabGrade' will never match both expressions. What you most likely meant to do was use the '||' OR operator to ensure that the computer handles cases correctly.

Finally, I can see a switch statement in order here. Something like:

switch LabGrade {

   case 'A': case 'a':
   ...
   break;

   case 'B': case 'b':
   ...
   break;

// etc

}

John is correct on both counts. Change that nested IF into a switch and it'll be easier to read.

I see a while statement in case 1: -- what is that supposed to be doing?

I see a while statement in case 1: -- what is that supposed to be doing?

The while is supposed to have more to it. When I copied the code I copied the wrong file that I was playing with.. hehe..

Supposed to be like..

while (Counter <= 6)
			{
				Counter = Counter + 1
				cout << "Enter lab number (1-7): \n";
				cin >> Lab;
				cout << "Enter current grade for lab: \n";
				cin >> LabGrade;

And then the rest of the code is the same. I'll try using a switch instead. I see what you both mean about switch being easier to read. Thanks again for the help and I'll posted an update once I type it out and try it.

I was just going through trying out the switch and remembered why I was trying to use the nested IF statement.. my instructor wants me to use it along with a loop and an array. So I'm back to the same problem of trying to figure out why the IF gets stuck in a loop and spams me till I CTRL C..

Any pointers?

Tried declaring LabGrade as a char and it fixed my infinite loop, but now limits me to only 7 entries for lab grades. Also, if I use the print list option it prints up funky numbers that aren't related to any of my variables in the IF statement... *blink*

switch (Selection)
		{
		case (1):
	

			while         //while what ??
			{
				cout << "Enter lab number (1-7): \n";
				cin >> Lab;
				cout << "Enter current grade for lab: \n";
				cin >> LabGrade;//declare this as a char

				if (LabGrade = 'A' && 'a') //I think you should use OR operator (||)
				{
					A = 4;
				}
				else
				{
					if (LabGrade = 'B' && 'b')
					{
						B = 3;
					}
					else
					{
						if (LabGrade = 'C' && 'c')
						{
							C = 2;
						}
						else
						{
							if (LabGrade = 'D' && 'd')
							{
								D = 1;
							}
							else
							{
								if (LabGrade = 'F' && 'f')
								{
									F = 0;
								}
							}
						}
					}
				}
			}

You should declare A,B,C,D,F as int.

Thanks for the help everyone. After some brain picking and help from a few others I got it figured out. Here's the working code for everyone to see and learn from:

#include <iostream>		//References iostream library to be used
using namespace std;	//Makes the library available

void main()
{

	int Counter=1;			//Counter for letter nested statement
	char Assignment[7]= {0, 0, 0, 0, 0, 0, 0};	//Char Array
	int Selection, Lab, Count;	//Declare variables
	float GPA;				//float variable to allow for decimals
	float Divisor = 7.0;	//Used to find the GPA for case 2
	char LabGrade;//Variables used in char array
	int Sum = 0;

	//Selection menu for grades


	cout << "Please select for pay type from the following choices:\n";
	cout << "\n    1 - Input Letter Grade Data\n";
	cout << "    2 - Lab Average\n";
	cout << "    3 - Print Lab Grades to Screen\n";
	cout << "    0 - Exit\n";
	cin >> Selection;


	//while statement that will run until 0 is input as a selection

	while (Selection != 0)
	{
		switch (Selection)
		{
		case (1):
	

	//Asking user to input lab number (1-7) then input the letter grade
	//for the lab. Then assigns a cell with the value taking Lab number
	//entered and subtracting 1.

			cout << "Enter lab number (1-7): \n";
			cin >> Lab;
			cout << "Enter current letter grade for lab: \n";
			cin >> LabGrade;

			Assignment[Lab-1] = LabGrade;

			cout << "Current lab " << Lab << ", is now " << LabGrade << ".\n\n";
			break;


		case (2):

			GPA = 0.0;		//Assigning GPA value for future calculation
			

	//Count starts at 0 and will continue to run so long as Count is 
	//less than or equal to 6. Count is then incremented by 1 each time.

				for (Count=0; Count <=6; Count++)
				{

	//if statement takes each array cell and checks to see what the letter
	//is and then adds sum, which is assigned 0, and adds the correct value
	//for the letter in that cell.

					if(Assignment[Count] == 'A')
						Sum += 4;
					if(Assignment[Count] == 'B')
						Sum += 3;
					if(Assignment[Count] == 'C')
						Sum += 2;
					if(Assignment[Count] == 'D')
						Sum += 1;
					if(Assignment[Count] == 'F')
						Sum += 0;


	//Simple averaging statement

			GPA  = Sum / Divisor;
				}

			cout << "Current lab average is " << GPA << ".\n\n";
			break;



		case (3):
			Count = 0;
			
	//Prints out each cell while the count is less than 6 and then increments
	//Count by 1 each time until 6 is reached.

			while (Count <= 6)
			{
				cout << "Lab " << Count+1 << " grade is " << Assignment[Count] << endl;
				Count++;
			}
			break;


	//Default error message when user inputs an incorrect selection from the
	//menu.

		default:
			cout << "Enter correct menu selection.\n";
		}
	cout << "\n    1 - Input Letter Grade Data\n";
	cout << "    2 - Lab Average\n";
	cout << "    3 - Print Lab Grades to Screen\n";
	cout << "    0 - Exit\n";
	cin >> Selection;
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.