February 28th, 2008, 01:40 PM
|
#1 (permalink)
| | Member
Join Date: Apr 2003 Location: Colorado Springs
Posts: 285
| 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..
|
| |
February 28th, 2008, 10:48 PM
|
#2 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
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.
|
| |
February 28th, 2008, 11:18 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
Also, another problem I see in the code is in your if statements
you have: which should be |
| |
February 29th, 2008, 12:42 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
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.
|
| |
February 29th, 2008, 12:52 PM
|
#5 (permalink)
| | Member
Join Date: Apr 2003 Location: Colorado Springs
Posts: 285
|
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. |
| | |
Currently Active Users Viewing This Thread: 2 (0 members and 2 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |