Help with program

Hello, Im having trouble with this program that is a calculator that does average, range MtoTheNth power and and factorials, which are initiated by a menu that allows the user to choose with function to call. I believe that my functions are arithmetically correct, but every time I run the program, It automatically calls the "range" function through that menu. Here is my code:

/******************************************************************************
CSCI 240 - Program 6 - Summer 2011

Name:
Z-Number:
Section:
TA:

Purpose:
******************************************************************************/

#include <iostream>
#include <iomanip>

using namespace std;

/* Function Prototypes */

string Menu();

int getValue( string, int, int );

void doAverage();

double calcAverage( int, int );

void doFactorial();

int calcFactorial( int );

void doMtoNth();

int calcMtoNth( int, int );

void doRange();

int calcRange( int, int, int, int );

int main()
{
string userChoice;

userChoice = Menu();

while ( userChoice != "Q" && userChoice != "q" )
{
if (userChoice == "1")
{
doAverage();
}
else if ( userChoice == "2" )
{
doFactorial();
}
else if ( userChoice == "3" )
{
doMtoNth();
}
else if (userChoice == "4")
{
doRange();
}


userChoice = Menu();
}//end of the while loop

//system("pause");
return 0;
}

/********** Code your Functions below this line **********/

string Menu()
{

string userChoice;

cout << "Special Purpose Calculator";

cout << "\n\n1) Average";
cout << "\n2) Factorial ";
cout << "\n3) M to the Nth Power";
cout << "\n4) Range";

cout << "\n\nQ) Quit";

cout << "\n\nEnter your choice?";

cin >> userChoice;

return userChoice;

}



int getValue(string prompt, int lowBound, int upBound)
{
int number;

cout << prompt << lowBound << " and " << upBound << ": ";

do
{
if (number < lowBound || number > upBound)
{
cout << "\nError: " << number << " is invalid. Try again: ";
}
}
while (number < lowBound || number > upBound);

return number;
}

void doAverage()
{
string state1 = "Enter a sum to be averaged between ";
string state2 = "Enter the number of entries ";

int lowBoundA = 1,
upBoundA = 1000,
lowBoundB = 1,
upBoundB = 1000,
sum,
count;

double average;

sum = getValue(state1, lowBoundA, upBoundA);
count = getValue(state2, lowBoundB, upBoundB);

average = calcAverage(sum, count);

cout << "\nThe average is: ";

return;
}

double calcAverage( int sum, int count )
{
double average;

average = (double) sum/count;

return average;
}

void doFactorial()
{
string state1 = "Enter a number between: ";

int lowBound = 0,
upBound = 10,
numfactorial,
factorial;

numfactorial = getValue(state1, lowBound, upBound);
factorial = calcFactorial(numfactorial);

return;
}

int calcFactorial( int number )
{
int count,
factorial = 1,
numfactorial;

count = numfactorial;
cout << "\nThe factorial value is: ";

while (count > 0)
{
factorial = factorial * count;
cout << count << "\n x";
count --;
}

cout << " = " << factorial;

return factorial;
}

void doMtoNth()
{
string state1 = "Enter a value: ";
string state2 = "Enter a value: ";

int lowBoundA = 1,
upBoundA = 10,
lowBoundB = 1,
upBoundB = 8,
M,
N,
MtoNth;

double average;

M = getValue(state1, lowBoundA, upBoundA);
N = getValue(state2, lowBoundB, upBoundB);
MtoNth = calcMtoNth (M, N);

cout << "\n" << M << " to the " << N << "th power is: ";
cout << MtoNth;

return;
}

int calcMtoNth( int M, int N )
{
int MtoNth = 1,
count = 1,
Nth;

while (count <= Nth)
{
MtoNth *=M;
count ++;
}

return MtoNth;

}

void doRange()
{
string state1 = "Enter a number between ";
string state2 = "Enter another number between ";

int lowBound = 0,
upBound = 1000,
num1,
num2,
num3,
num4,
range = 0;

double average;

num1 = getValue(state1, lowBound, upBound);
num2 = getValue(state2, lowBound, upBound);
num3 = getValue(state2, lowBound, upBound);
num4 = getValue(state2, lowBound, upBound);
range = calcRange(num1, num2, num3, num4);

cout << "\nThe range of " << num1 << ", " << num2 << ", " << num3 << ", " << num4 << "is " << range;

return;
}

int calcRange( int number1, int number2, int number3, int number4 )
{
int max,
min,
range;

if (number1 >= number2)
max = number1;

else
max = number2;

if (number3 >= max)
max = number3;

if (number4 >= max)
max = number4;

if (number1 <= number2)
min = number1;

else
min = number2;

if (number3 <= min)
min = number3;

if (number4 <= min)
min = number4;

range = max - min;

return range;

}

Please use code tags.

Try a getch() function for whichever compiler you are using.
This function has many error, "number" IS NULL, it must be assigned.
Accept the user with the value of CIN.

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
int getValue(string prompt, int lowBound, int upBound)
{
int number;         //error: number IS NULL ; Can not compare it. Initial value, and then accept the user data.       true: int number = 0;

cout << prompt << lowBound << " and " << upBound << ": ";
do
{
////////////////////true:    cin>> number;
if (number < lowBound || number > upBound)
{
cout << "\nError: " << number << " is invalid. Try again: ";
}
}
while (number < lowBound || number > upBound);

return number;
}



void doAverage()
{
string state1 = "Enter a sum to be averaged between ";
string state2 = "Enter the number of entries ";

int lowBoundA = 1,
upBoundA = 1000,
lowBoundB = 1,
upBoundB = 1000,
sum,
count;

double average;

sum = getValue(state1, lowBoundA, upBoundA);
count = getValue(state2, lowBoundB, upBoundB);

average = calcAverage(sum, count);

cout << "\nThe average is: "; \\ error: add << average; 

return;
}

Program so that you can complete the first feature.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
int calcMtoNth( int M, int N )
{
int MtoNth = 1,
count = 1,
Nth;     \\  error: IS NULL

while (count <= Nth)    \\error: Nth IS NULL; Nth, is it N ?
{
MtoNth *=M;
count ++;
}

true :cin>> number;
Last edited on
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
 
int calcFactorial( int number )
{
int count,
factorial = 1,
numfactorial; \\delete numfactorial;

count = numfactorial; \\ error: NOT numfactorial ,IS number.   true: count = number;
cout << "\nThe factorial value is: ";

while (count > 0)
{
factorial = factorial * count;
cout << count << "\n x";
count --;
}


int calcMtoNth( int M, int N )
{
int MtoNth = 1,
count = 1,
Nth;  \\ delete Nth;

while (count <= Nth)   \\ error : NOT Nth IS N ; true: count<=N;
{
MtoNth *=M;
count ++;
}

return MtoNth;

}

These are the fault of your program
With some modifications...
Last edited on
Topic archived. No new replies allowed.