My program is to display a menu choice for the user, they will then pick one of the choices and the program will perform a calculation for the user. It contains four functions (menu, sum, power, and gcd) and it must contain reference parameters...i have most of the program, it wont compile, but shows no error messages either....I am new to this....heres the code:

``````#include<iostream>
using namespace std;

//menu prints out options to the user, and receives the user's choice
cout<<"1) Find the sum of 1 to n."<<endl;
cout<<"2) Find the power of x raised by y."<<endl;
cout<<"3) Find the greatest common divisor."<<endl;
cout<<"4) Quit."<<endl;
cin>>choice;
}

[B]void sum[/B](int num, int& result){
result = 0;
for (int a = 1; a<= num; a++)
result += a;
}

[B]void power[/B](int pow1, int pow2, int& result){
int i;
while ( i <= pow2 )
{
result *= pow1;
i++;
}

}

[B]void gcd[/B](int gcd1,int gcd2, int& result){
result = 0; //running total
for (int a = 1; a<= gcd2; a++)
result += a;

}

[B]int main[/B](){
int choice,num;
int num1,num2,input,outcome;
while (choice != 4){

[B]switch[/B](choice){
[B]case 1[/B]: cout<<"Enter the number of sum to: ";
cin>>input;
[B]break[/B];
[B] case 2[/B]: cout<<"Enter the number to raise: ";
cin>>num1;
cout<<"Enter the power to raise to: ";
cin>>num2;
break;
[B]case 3[/B]: cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
[B] break[/B];
}

[B]if[/B] (choice == 1) sum(input, outcome);
[B] else if [/B](choice == 2) power(num1,num2, outcome);
[B]else[/B] gcd(num1,num2, outcome);
cout<<"Result: "<<outcome<<endl;
}

[B] return 0[/B];
}``````

Edited by Nick Evan: Added code-tags. It's not that hard, just press the code button once and paste your code in between the tags

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by jjennings11
``````int i;
while ( i <= pow2 )
{
result *= pow1;
i++;
}``````

In the above code, what is the initial value of variable i?

it wont compile, but shows no error messages either

No either it compiles in which case it will show no errors but you may get warnings which don't stop it compiling or if fails to compile in which case you get at least 1 error.

As it happens your code compiles for me with a single warning.

I suggest you may need to give us more detail of what you are doing and what the result is and what you expected the result to be.

Edited by Ancient Dragon: n/a

The output should look like this
1) Find the sum of 1 to n.
2) Find the power of x raised by y.
3) Find the greatest common divisor of two numbers.
4) Quit
2
Enter the number to raise: 3
Enter the power to raise to: 4
Result: 81

1) Find the sum of 1 to n.
2) Find the power of x raised by y.
3) Find the greatest common divisor of two numbers.
4) Quit
1
Enter the number of sum to: 20
Result: 210
1) Find the sum of 1 to n.
2) Find the power of x raised by y.
3) Find the greatest common divisor of two numbers.
4) Quit
3
Enter the first number: 24
Enter the second number: 12
Result: 12
1) Find the sum of 1 to n.
2) Find the power of x raised by y.
3) Find the greatest common divisor of two numbers.
4) Quit
3
Enter the first number: 155
Enter the second number: 75
Result: 5
1) Find the sum of 1 to n.
2) Find the power of x raised by y.
3) Find the greatest common divisor of two numbers.
4) Quit
4

The program should first call menu, and display the options for the program – to find
the summation of 1 up to and including a certain number, to find the power of one
number raised to the second number, the greatest common divisor of two numbers, or
quit. The choice is sent back via reference parameter (so it should be an argument).

Next, the program should prompt for value(s) and then will perform the calculations for
summation, power, or greatest common divisor. The functions will return the results of
each of the three functions. Additionally, sum will have one argument, the number to
sum to; both power and gcd will have two arguments each. The arguments for power are
the number and the power to raise the number to (both integers), and gcd will take in two
arguments (integers).
The program should print out the result to the screen. Last, the program should ask the
user to select an option from the menu of four choices.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.