Returning Values

This is a discussion on Returning Values within the C++ Programming forums, part of the General Programming Boards category; What's wrong with this code? it returns Zero; Code: /*snipt*/ cout<<"Do you want to know what GPA you currently have ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    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.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  2. returning values (tuples)
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 09-20-2007, 09:24 PM
  3. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 10:24 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. printf returning values problem
    By hayai32 in forum C Programming
    Replies: 20
    Last Post: 06-25-2005, 01:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21