# Returning Values

• 03-17-2002
incognito
Returning Values
What's wrong with this code?
it returns Zero;

Code:

``` /*snipt*/ cout<<"Do you want to know what GPA you currently have in this School Year\n"; cout<<"To see if you're on track for you goal?\n"<<endl; cout<<" (1)Yes\t";cout<<"(2) No"; cin>>choice; long double GPA; switch (choice) { case 1: cout<<"Let's take a hack at it then\n";         GPA=YearGPA();         break; case 2: cout<<"No Problem\n";         break; } cout<<"That's an average of \n"; cout<<GPA; if(FinalGpa2<=GPA) { cout<<"See You're on your way already.\n"; } else cout<<"Well you have to do better than that\n"; /*snip*/ //Function definition       long double YearGPA() { cout<<"For how many nine weeks do you want to average your GPA?\n"; int N_Weeks; cin>>N_Weeks; int CurrentGPA; switch (N_Weeks) { case 1: CurrentGPA=OneNineWeeks();       break; case 2: TwoNineWeeks();         break; case 3: ThreeNineWeeks();         break; case 4: FourNineWeeks();         break;         return CurrentGPA; }  } int OneNineWeeks() { int first; int second; int third; int fourth; int fifth; int six; cout<<"What Grade did you get for the following classes\n"; cout<<"First Period:\n"; cin>>first; cout<<"Second Period:\n"; cin>>second; cout<<"Third Period:\n"; cin>>third; cout<<"Fourth Period:\n"; cin>>fourth; cout<<"Fifth Period:\n"; cin>>fifth; cout<<"Six Period:\n"; cin>>six; int temp1; int temp2, temp3, temp4, temp5, temp6; int tempEX=2; temp1=first * tempEX; temp2=second * tempEX; temp3=third * tempEX; temp4=fourth * tempEX; temp5=fifth * tempEX; temp6=six * tempEX; temp6=temp1+temp2+temp3+temp4+temp5+temp6; int temp7; temp7=temp6/6; return temp7; }```

Yet I aways get zero for the GPA.
• 03-17-2002
biosx
The problem rests in many things (as I can see by just glancing at your code). The main problem is this:
Code:

`temp7=temp6/6;    // NO NO NO`
In C/C++, when you do division with integers it truncates the decimal part. Which also points out another problem with your program.. You defined/declared everything as an integer. GPA's and grades are usually real numbers, so redefine everything as a double.

So to fix your program (mostly), redefine everything as double's and do the above code like this:
Code:

`temp7 = temp6 / 6.0; // Correct`
I didn't really have time to go through your code thoroughly (sorry), but I think I pointed out the recurring problem.

Good luck ;)
• 03-17-2002
incognito
Thanks for your help man that seems to have fixed it. Well gotta keep working on it though, there's lot more stuff to it than that.