Getting bizarre results from simple math

This is a discussion on Getting bizarre results from simple math within the C++ Programming forums, part of the General Programming Boards category; Code: //Average scores else if ((Menu == 'B') || (Menu == 'b')){ char AvgDone; double TotalGrade; double Grade; double AvgGrade; ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question Getting bizarre results from simple math

    Code:
    //Average scores
    		else if ((Menu == 'B') || (Menu == 'b')){
    			char AvgDone;
    			double TotalGrade;
    			double Grade;
    			double AvgGrade;
    			double NumOfGrades = 0;
    			do{
    			cout << "Please enter your grade" << endl;
    			cin >> Grade;
    			TotalGrade = TotalGrade + Grade;
    			NumOfGrades = NumOfGrades + 1;
    			cout << "Do you have more grades to enter? [Y]es or [N]o " << endl;
    			cin >> AvgDone;
    			} while((AvgDone == 'Y') || (AvgDone == 'y'));
    			AvgGrade = TotalGrade / NumOfGrades;
    			cout << "The average for the " << NumOfGrades << " grades given is " << AvgGrade << endl;
    			cout << "Return to the menu? [Y]es or [N]o" << endl;
    			cin >> Again;
    Gives me the following results if I enter 100 for a single grade.

    Code:
    ************************************************************
    *       Plese choose from the following menu options       *
    *       <A> Convert single numeric grade to alpha grade    *
    *                                                          *
    *       <B> Average numeric grades                         *
    *                                                          *
    *       <C> End Program                                    *
    ************************************************************
    Enter your choice
    b
    Enter your numeric grade:
    Please enter your grade
    100
    Do you have more grades to enter? [Y]es or [N]o
    n
    The average for the 1 grades given is -9.25596e+061
    Return to the menu? [Y]es or [N]o
    It seems to me this is simply 100 / 1 and therefore should be 100, or is this working with some 'new' math??

    Thanks!!


    By the way, yes, this is homework!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    always initialize variables. Otherwise you are likely to get junk where you don't want it. For example, totalGrade starts out with junk you don't initialize it. Then you add 1 to junk and you still have junk. Then you divide junk by more junk to get even more junk when you calculate avgGrade.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    That fixed it!! WOOT! I love this site! Thanks elad....thanks a lot.....

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    haha
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to do simple math with float and int type
    By demuro1 in forum C Programming
    Replies: 16
    Last Post: 09-24-2008, 11:58 AM
  2. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 07:42 AM
  3. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 03:40 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 09:06 PM

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