![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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; } |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Use code tags!
Your problem is this line: sum = sum + (int)grade; sum += grade[j][i]; |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
|
yea that does the trick...i also realized that i didn't add this line to that for loop. it works fine now... thanx
|
|
|
|
|
|
#5 |
|
Programmer
|
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? |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
About your header files: I suggest you take a look at this post:
http://www.programmingforums.org/for...7&postcount=49 |
|
|
|
|
|
#8 |
|
Programmer
|
thanx ooble, i'll keep that in mind
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#10 |
|
Programmer
|
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|