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.
ThanksCode:// 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; }



LinkBack URL
About LinkBacks



