Basic C++ Program help

This is a discussion on Basic C++ Program help within the C++ Programming forums, part of the General Programming Boards category; I have completed my first C++ program I am just unsure what I have done incorrectly for the 'average' portion ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Basic C++ Program help

    I have completed my first C++ program I am just unsure what I have done incorrectly for the 'average' portion of the program. I am not getting the correct answers. I would appreciate any help and/or suggestions.

    Code:
    // Math Program - provides quick calculations to basic math problems
    
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    // Function for factorial
    
    long factorial (long a)
    {
      if (a > 0)
       return (a * factorial (a-1));
      else
       return (1);
    }
    
    int main()
    {
    
    for (int i=1; ; i++)
    {
    
    // Header to program
    
        cout << "********************************************************************************" << endl;
        cout << "********************************* Math Program *********************************" << endl;
        cout << "********************************************************************************" << endl << endl;
        cout << "Please choose from the following options" << endl;
        cout << endl << "1-Factorial" << endl;
        cout << "2-Slope of a line" << endl;
        cout << "3-Average of unlimited numbers" << endl;
        cout << "4-Area of different geometries";
        cout << endl << endl << "Selection: ";
        int choice;
        cin >> choice;
        cout << endl;
    
    // Decide which choice to run
    
        long number;
        if (choice == 1)
            {
    
    // Enter a number and call factorial() to provide answer
    
                cout << endl << "Please type a number: ";
                cin >> number;
                    if(number < 0)
                    {
                        cout << "Enter a positive integer!..." << endl <<endl;
                    }
                    else
                    {
                        cout << number << "! = " << factorial (number) << endl << endl;
                    }
            }
        else if (choice == 2)
    
    // Enter coordinates and calculuates slope to provide answer
    
            {
                double x1,x2,y1,y2;
                double slope;
                cout << endl << "Please enter the first set of coordinates: ";
                cin >> x1;
                cin >> y1;
                cout << endl << "Now enter the second set of coordinates: ";
                cin >> x2;
                cin >> y2;
                slope = ((y2-y1)/(x2-x1));
                cout << "The slope of the line is:" << slope << endl <<endl;
            }
    
        else if( choice == 3)
        {
    
    // "Loops forever"
    
        for(int count = 1;;count ++)
        {
            double value;
            double average;
            double accumulator=0;
    
    // Determine whether count is 1 or greater
    
            if(count == 1)
            {
            cout << "Enter first value: ";
            }
            else
            {
            cout << "Enter next value or enter 0 to exit: ";
            }
            cin >> value;
    
    // If value is 0... exit
    
            if (value == 0)
            {
    
    // ...then average and then exit
    
            cout << "The average is: " << average << endl;
            break;
            }
            else
            {
            accumulator = accumulator + value;
            average = accumulator/count;
            }
        }
        }
    
    // Finds the area of various geometries
    
        else if (choice == 4)
        {
    
    // Choose a geometry
    
            int selectGeo;
            cout << "Select a geometry: " << endl << endl;
            cout << "1-Square" << endl;
            cout << "2-Rectangle" << endl;
            cout << "3-Triangle" << endl;
            cout << "4-Circle" << endl << endl;
            cout << "Selection: ";
            cin >> selectGeo;
            cout << endl << endl;
    
    // Determines area of square
    
            if (selectGeo==1)
            {
                cout << "What is the length of one side: " << endl;
                double areaSquare;
                double lengthSquare;
                cin >> lengthSquare;
                areaSquare = lengthSquare*lengthSquare;
                cout << "The area of the square is " << areaSquare << " units squared." << endl << endl;
            }
    
    // Determines area of rectangle
    
            else if (selectGeo==2)
            {
                cout << "What is the length: ";
                double areaRectangle;
                double lengthRectangle;
                double widthRectangle;
                cin >> lengthRectangle;
                cout << endl << "What is the width: " << endl;
                areaRectangle = lengthRectangle * widthRectangle;
                cin >> widthRectangle;
                cout << "The area of the rectangle is " << areaRectangle << " units squared." << endl << endl;
            }
    
    // Determines area of triangle
    
            else if (selectGeo==3)
            {
                cout << "What is the base length: ";
                double baseTriangle;
                double heightTriangle;
                double areaTriangle;
                cin >> baseTriangle;
                cout << endl << "What is the height length: ";
                cin >> heightTriangle;
                areaTriangle = (0.5*baseTriangle)*(heightTriangle);
                cout << "The area of the triangle is " << areaTriangle << " units squared." << endl << endl;
            }
    
    // Determines area of circle
    
            else if (selectGeo==4)
            {
                cout << "What is the radius of the circle: ";
                double radiusCircle;
                double areaCircle;
                cin >> radiusCircle;
                areaCircle = 3.14*(radiusCircle*radiusCircle);
                cout << "The area of the circle is " << areaCircle << " units squared." << endl << endl;
            }
    
    // Informs user selection is invalid
            else
            {
                cout << "INVALID SELECTION!!!" << endl << endl;
            }
        }
    
        else
    
    // Informs user that input is not recognized by program
    
            {
                cout << "INVALID SELECTION!!!" << endl << endl;
            }
    }
    
    // Wait for user to respond to quit program
    
    system ("pause");
    return 0;
    }
    Thanks

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Check the value of count, after entering one non-zero number and then zero. You'll find that it is dividing by two. You shouldn't increment count until you know the value entered was non-zero.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    I am not sure that the solution you have provided fixes the problem. It would seem that if it was a problem with the count variable, then on the first iteration of the loop, the program would output "enter the next number...." instead of "enter the first number". However, I think that the problem is with the accumulator. I noticed that when inputting one number, that same number is returned. When inputting two numbers it returns half of the second number entered. When you input three numbers, it returns a third of the third number. In other words, the accumulator is not actually summing the numbers. It is only giving the last input as the accumulator value. How can this be corrected?

    Thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    accumulator is redefined and reinitialized to 0 everytime the loop starts over.
    Put it outside the loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 10:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-13-2002, 12:15 AM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM

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