home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2840
Discussions: 188,380, Posts: 2,243,467, Members: 232,608
Old February 28th, 2008, 01:40 PM   Digg it!   #1 (permalink)
Member
 
Ramen's Avatar
 
Join Date: Apr 2003
Location: Colorado Springs
Posts: 285
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,004
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,959
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)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
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 online now   Reply With Quote
Old February 29th, 2008, 12:52 PM     #5 (permalink)
Member
 
Ramen's Avatar
 
Join Date: Apr 2003
Location: Colorado Springs
Posts: 285
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


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

Most Active Discussions
Is It Just Me? (2885)
The United States Debt (20)
Looks like Burris will get his Sena.. (8)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Help with an Ati Radeon HD 4850 512.. (25)
Recent Discussions
RCA 52Inch HDTV wont turn on (1)
Building a gaming computer advi.. (1)
Best digital camera for under 2.. (14)
Help with an Ati Radeon HD 4850.. (25)
Install Problem for Windows Def.. (0)
New Build ( Finally ) (1)
dual monitors wont boot (0)
Folderchat Weekday thread (439)
MSN Hotmail Down??? (7)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:55 PM.
TechIMO Copyright 2008 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