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;

    [B]void menu[/B](int& choice){
      //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;
      menu(choice); 
      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]menu(choice)[/B];  
      }
     
     [B] return 0[/B];
   }

Dani AI

Generated

There are three root causes in the posted program that explain the strange behaviour: uninitialized variables, functions that never set their reference output, and an incorrect gcd implementation. is right to ask about i — using an uninitialized local causes undefined behaviour, which is why the program can "compile with no errors" but still act wrong. is also correct that you should be watching compiler warnings (use -Wall -Wextra) to catch these.

Keep each function responsible for initializing its result reference, handle edge cases, and validate inputs. Example fixes (do not copy the original code) — these are concise, safe implementations you can drop into your program:

void sum(int n, int& result) {
    result = (n > 0) ? n * (n + 1) / 2 : 0;
}
void power(int base, int exp, int& result) {
    result = 1;
    if (exp < 0) { /* handle error: negative exponent not supported */ return; }
    for (int i = 0; i < exp; ++i) result *= base;
}
void gcd(int a, int b, int& result) {
    a = std::abs(a); b = std::abs(b);
    while (b != 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    result = a;
}

In main initialize variables (e.g. int outcome = 0;) and only rely on outcome after calling a function that sets it. Improve menu(int& choice) by validating user input in a loop so non-numeric or out-of-range entries are re-prompted. Test each function independently (unit-test with known inputs) to confirm expected outputs: sum(20) -> 210, power(3,4) -> 81, gcd(155,75) -> 5.

Compile with warnings enabled (g++ -Wall -Wextra -std=c++11) to see uninitialized-variable warnings. Also watch for integer overflow in power and sum; consider using long long or checking ranges if inputs can be large.

Recommended Answers

All 4 Replies

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.

<oops! wrong thread, sorry.>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.