Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 16th, 2005, 4:59 AM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
problem...a simple program...

ok here's the problem...i've been playing with the FOR loops because they were giving me a hard time. so i made this little program using FOR loops. so...it goes like this...
first it outputs the class (1st, 2nd, 3rd). there are 10 students in each class. you have to input their names, then their surnames, and then 3 grades.
after inputting all 3 grades, i want the program to sum these 3 grades, calculate the average (for each student individually after entering his data), and outputting the average of his grades, and then asking you to input data for the next student.
it all worked fine untill i've tried to make the program calculate the grades. it doesnt calculate correctly, and it ends the whole program. where did i go wrong?




#include <iostream.h>

int main()
{
char name[10][15];
char surname[10][15];
int Class[3];
int grade[10][3];
int sum=0;
float average;

for(int i=0; i<3; i++)
{
cout << "**** "<< i+1 << ". CLASS ****.\n\n" << endl;

for(int j=0; j<10; j++)
{
cout << "Input the name of " << j+1 << ". student." << endl;
cin >> name[j];
cout << endl;
cout << "Input the surname of " << j+1 << ". student." << endl;
cin >> surname[j];
cout << endl;

for (int k=0; k<3; k++)
{
cout << "Input " << k+1 << ". grade for " << i+1 << ". student." <<endl;
cin >> grade[k][j];
cout << endl;
}

sum = sum + (int)grade;
average= sum / 3.0;
cout << "The student has an average of: " << average << "." << endl;
}
}
return 0;
}
n00b is offline   Reply With Quote
Old Sep 16th, 2005, 6:25 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Use code tags!

Your problem is this line:
sum = sum + (int)grade;
It probably needs to be:
sum += grade[j][i];
Arevos is offline   Reply With Quote
Old Sep 16th, 2005, 6:34 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
First, you need to learn to put your code inside code tags. These are HTML pages, which means they eat whitespace and farble up your presentation. Many potential respondents will jump ship at this point.

Secondly, you should provide all the information that you have available. You MAY have done that, but usually one has something other than "ends the whole program."

Thirdly, make sure you have all warnings and errors enabled for your compiler.

Fourthly, read my responses to this thread. The probability is high that therein lies your problem. If that doesn't help, post back with formatted code and I'll take an additional look.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 16th, 2005, 6:48 AM   #4
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
yea that does the trick...i also realized that i didn't add this line to that for loop. it works fine now... thanx
n00b is offline   Reply With Quote
Old Sep 16th, 2005, 7:36 AM   #5
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
thanx DaWei, yea i know...it's a long trip for me to get to the C++ paradise.
i'm doing one step at the time..but i'll definately have your tips in mind.

btw...could you please tell me where the options for enabling all these warnings are? is it at the project settings? Alt+F7 ? there's a falling menu called Warning Level: would that be it?
n00b is offline   Reply With Quote
Old Sep 16th, 2005, 8:01 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That would be it. Many warnings are indeed programming errors, so it pays to have the level as stringent as possible.

If you indeed read that thread, you'll see that I caution against acquiring bad habits. The addition of one line per input call is not going to kill you. I can break your program with a keystroke. You can't plead ignorance, now that you've read it, only laziness or schlockiness. Robust code is not paradise, its hell on earth, but it's necessary unless you are pursuing this as a hobby.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 16th, 2005, 9:42 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
About your header files: I suggest you take a look at this post:

http://www.programmingforums.org/for...7&postcount=49
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 16th, 2005, 10:12 AM   #8
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
thanx ooble, i'll keep that in mind
n00b is offline   Reply With Quote
Old Sep 16th, 2005, 5:55 PM   #9
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
didn't somebody else post this exact problem recently?

three classes...
10 students per class...
etc.

but thank you for the source, most would just ask without trying.

good luck!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 16th, 2005, 6:30 PM   #10
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
i dunno...but i've heard lots of ppl asking about similar problems. this kind of exercise seems to be quite good for learning how the for loop works.
n00b is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC