This is a
very well constructed program for a beginner. You have a good grasp of how to divide up problem and solve it. All of the mistakes are little things. I hope you continue to pursue programming.
Now for the errors.
On line 7 you declare getChoice() as taking a char but the definition on line 49 takes a reference to a char. Change line 7 to
void getChoice(char &choice);
Line 28: When you pass an array to a function, you just pass the name, so it should be
getvalues(numberArray);
.
The same thing applies to line 34. Also on line 34, when you pass a variable by reference, you pass it with the name only. Finally, doTheMath doesn't return anything and there's no need for it to do so. So all together, line 34 should be
doTheMath(choice,numberArray);
.
You also need to change the declaration and definition of doTheMath to return void instead of double on lines 8 and 57.
At line 87, add a newline to the output:
cout << sum << '\n';
getChoice()
should put the choice into the "choice" parameter that you pass into the function. So that should be :
1 2 3 4 5
|
void getChoice(char &choice)
{
cout << "please make a selection, will it be choice A,B or C?\n";
cin >> choice;
}
|
Your code that prints the square roots needs to separate the printed values. Also you're printing "here are the numbers square rooted" inside the loop, so it prints 5 times instead of once. So the loop should be:
1 2 3 4 5
|
cout << "here are the numbers square rooted\n";
for ( int i=0; i < 5; i++)
{
cout << sqrt (numberArray[i]) << '\n';
}
|
Here is your code with the changes I mentioned. It seems to work fine. Normally I'd make you do the edits yourself but you've demonstrated enough knowledge - doing the edits wouldn't teach you anything.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <cstdlib>
#include <iostream>
#include <cmath>
void showMenu();
void getChoice(char &choice);
void doTheMath(char &choice, double numbers[]);
void getValues(double numberArray[]);
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
double numberArray[5];
double result;
char choice;
double working;
getValues(numberArray);
showMenu();
getChoice(choice);
doTheMath(choice,numberArray);
return 0;
}
void showMenu()
{
cout << " A. Display the square root of each number.\n";
cout << " B. Display the sum of all numbers\n";
cout << " C. Display the average of all numbers\n";
}
void getChoice(char &choice)
{
cout << "please make a selection, will it be choice A,B or C?\n";
cin >> choice;
}
void doTheMath(char &choice, double numberArray[])
{
double sum=0;
if (choice == 'A')
{
cout << "here are the numbers square rooted\n";
for ( int i=0; i < 5; i++)
{
cout << sqrt (numberArray[i]) << '\n';
}
}
else if (choice == 'B')
{
for (int i=0;i<5; i++)
{
sum += numberArray[i]; //For each item in array, add them to the sum
}
cout << "the numbers added together would be\n"
<< sum << endl;
}
else if (choice == 'C')
{
for (int i=0;i<5;i++)
{
sum+=numberArray[i]; //first find sum
}
sum /= 5; //Divide sum by 5 to get average
cout << "here is the average of all numbers\n";
cout << sum;
}
}
void getValues(double numberArray[])
{
cout << "please enter five numbers\n";
for ( int i = 0; i < 5; i++)
{
cin >> numberArray[i];
}
}
|
In exchange for this gift, I ask that you change doTheMath() to use a switch statement as Auroch has mentioned instead of if/then/else statements. I think you'll get it easily. But one note of caution! With a switch statement, the program flow will drop from one case right into the next (and the next) unless you put in a "break" statement.
Good luck, and keep up the good work. I'd hire you in a few years.