home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ array loop w/ nested if statement problem

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2138
Discussions: 200,950, Posts: 2,379,422, Members: 246,311
Old February 28th, 2008, 01:40 PM   Digg it!   #1 (permalink)
Member
 
Ramen's Avatar
 
Join Date: Apr 2003
Location: Colorado Springs
Posts: 289
Angry
C++ array loop w/ nested if statement problem

Hey everyone. I've been trying to get this work for for a day now and can't figure out why it won't do what I want it to. Basically, I had to write an array that will input grades and output a list of grades or average the grades. Sounds easy huh? Well, I got that part done.. right here:

Code:
#include <iostream>
using namespace std;

void main()
{

	int Counter=1;
	int Assignment[7]= {0, 0, 0, 0, 0, 0, 0};
	int Selection, Lab, Count;
	float GPA;
	float Divisor = 7.0;
	int LabGrade;



	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):
	

			cout << "Enter lab number (1-7): \n";
			cin >> Lab;
			cout << "Enter current 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;

			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;
	}

}
Well, instead of using numbers for the grades the user is supposed to be able to input letters (A, B, C, D, & F). Then save the letters they input as the score (A = 4, B = 3, C = 2, D = 1, & F = 0). Here's the code I wrote trying to make it work like I want it to, but it's not..

Code:
#include <iostream>
using namespace std;

void main()
{

	int Counter=0;
	int Assignment[7]= {0, 0, 0, 0, 0, 0, 0};
	int Selection, Lab, Count;
	char LabGrade, A, B, C, D, F;
	float GPA;
	float Divisor = 7.0;


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

				if (LabGrade = 'A')
				{
					A = 4;
				}
				else
				{
					if (LabGrade = 'B')
					{
						B = 3;
					}
					else
					{
						if (LabGrade = 'C')
						{
							C = 2;
						}
						else
						{
							if (LabGrade = 'D')
							{
								D = 1;
							}
							else
							{
								if (LabGrade = '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;
	}
}
__________________
Yeah, meow..
Ramen is offline   Reply With Quote
Old February 28th, 2008, 10:48 PM     #2 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
i'm assuming your a student. Take my advice, properly indenting your code so that it's readable is KEY. It's probably the MOST important thing. It's what i stress most for those i'm teaching. If it's not readable i send it right back to them to redo it. I've reformatted the code so that it's much easier to read and it leaves the bug in your code quite obvious. I left a comment next to the bug as well describing it. Up to you to fix it though. Feel free to submit it for further review if you want advice as to how it could be done better... Best way to learn to code is to see how something you've written would have been done by someone with more experience! :-D I'm having trouble with that one now..i don't wanna admit i'm not the best and go look around! hehehe. Good luck on your assignment! email me if you want more help - u_kcormier at umassd dot edu

Code:
#include <iostream>
using namespace std;

void main()
{
	int Counter=0;
	int Assignment[7]= {0, 0, 0, 0, 0, 0, 0};
	int Selection, Lab, Count;
	char LabGrade, A, B, C, D, F;
	float GPA;
	float Divisor = 7.0;

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

					if (LabGrade = 'A') A = 4;
					else if (LabGrade = 'B') B = 3;
					else if (LabGrade = 'C') C = 2;
					else if (LabGrade = 'D') D = 1;
					elseif (LabGrade = 'F') F = 0;

					//here's your bug?  the if else structure above does nothing!  LabGrade is just a character, being put into an array of ints....whoops
					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;
	}
}

Last edited by sr71000 : February 28th, 2008 at 11:06 PM.
sr71000 is offline   Reply With Quote
Old February 28th, 2008, 11:18 PM     #3 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,977
Send a message via AIM to lost-and-found
Also, another problem I see in the code is in your if statements

you have:
Code:
if(Labgrade='A')
which should be
Code:
if(Labgrade=='A')
__________________
lost-and-found is offline   Reply With Quote
Old February 29th, 2008, 12:42 AM     #4 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
It's late and I'm tired, but this is a pretty basic CS 101 assignment.

Yeah, keep your code cleaner indentation wise. Break it down into easy steps:
1. Get Data
2. Store Data
3. Calc Data
4. Print Data

Define your array like:
int MaxArraySize = 7;
char Grade_Array[MaxArraySize];
int num_grades_entered; // you'll have to count these

Then you can calc average with for loop...

int sum = 0;
float average;

for (int i=0; i < num_grades_entered; ++i) {
if(Grade_Array[i] == 'A')
sum += 4;
if(Grade_Array[i] =='B')
sum += 3;
if(Grade_Array[i] == 'C')
sum += 2;
if(Grade_Array[i] == 'D')
sum += 1;
if(Grade_Array[i] =='F')
sum += 0;
}

average = sum/num_grades_entered;

Last edited by Rootstonian : February 29th, 2008 at 12:44 AM.
Rootstonian is offline   Reply With Quote
Old February 29th, 2008, 12:52 PM     #5 (permalink)
Member
 
Ramen's Avatar
 
Join Date: Apr 2003
Location: Colorado Springs
Posts: 289
Talking

Thanks everyone for their input. I finally fixed my problem with all of your help.I thught my indenting was pretty good, but I guess not.. hehe.. I'll work out it though.

When I was working through it last night before you all posted I did notice that having my array as int was screwing me up because like sr71000 pointed out with the comment is trying to use LabGrade char in an int array. So I solved that, fixed my comparisons using == instead of just = like lost suggested. (That's another habit I need to break cause I've done that in the past with other assignments and it's messed me up) Finally, with Rootstonian's help with the averaging it all worked out. I think I just needed to take a break, get some good advise, and try some options that was given. No wonder I love TechIMO community so much.. hehe.
Ramen is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bootup Loop Problem ** $ Prize Sandizzle Technical Support 5 September 12th, 2007 10:10 AM
x1600pro constant loop problem dru_down Graphics Cards and Displays 0 August 22nd, 2006 02:16 PM
Excel nested IFs or Index rmanja Applications and Operating Systems 0 November 18th, 2005 07:56 PM
Java if statement problem... Blazer06 Webmastering and Programming 3 February 23rd, 2005 11:20 AM
Load an array w/ a do/while loop Martoch Webmastering and Programming 6 October 14th, 2002 09:02 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (176)
Is It Just Me? (2940)
The disrespect of Obama by Russian .. (26)
Wireless Televisions. (12)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
radeon x850xt platinum & shader.. (6)
Is the PSU I received dead? (12)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
windows vista security holes (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
Happy Thanksgiving: At discount from .. (0)
For Sale BFG GTX285 OC2 with 10 year .. (3)
Point and Shoot Camera Suggestions. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Laptop with wireless problem. (2)
Internet Lost (1)
Hp Artist Edition + Matching Bag (0)
My monitor won't turn on after instal.. (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)


All times are GMT -4. The time now is 10:57 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28