简体   繁体   中英

C++ functions, pass by reference

I'm having a problem with passing parameters as a reference when calling a function. We are to build a program with the following instruction:

Complete the program above by writing the function definitions and the function prototypes for the following functions:

displaymenu This function accepts num1 and num2 as arguments passed by reference and returns a character as output. The function displays the menu and accepts as user-given inputs the following:

 A num1 B num1 num2 Q

checkeven This function returns TRUE if the argument is even. Otherwise the function returns FALSE.

divisible This function returns TRUE if the first argument, num1, is divisible by the second argument, num2.

So far this is my code and getting error on passing parameter.

#include <iostream>

using namespace std;
int num1, num2 = 0;
char displaymenu(int num1, int num2);
bool checkeven (int num1);
bool divisible (int num1, int num2);

int main(){
    int num1, num2 = 0;
    char choice;
    do{
       choice = displaymenu(num1,num2);    
       if (choice == 'A'){
           if (checkeven(num1))
               cout << num1 << " is even." << endl;
           else 
                cout << num1 << " is odd." << endl;
       }          
       else if (choice == 'B'){
          if (divisible(num1, num2))
              cout << num1 << " is divisible by " << num2 << endl;
          else
              cout << num1 << " is not divisible by " << num2 << endl;
       }
       else if (choice == 'Q')
            cout << "Bye!" << endl;
       else 
            cout << "Illegal input" << endl;
       
    }while (choice != 'Q');
    return 0;
}

char displaymenu(int &num1 = num1, int &num2 = num2){
    char choice;
    cout << '+' << "______________________________" << '+' <<endl;
    cout << '|'<<"Choose an option: " <<"            |"<<endl; 
    cout << '|'<<"    A: Check if even          |" <<endl;
    cout << '|'<<"    B: Check if divisible     |" <<endl;
    cout << '|'<<"    Q: Quit                   |" <<endl;
    cout << '+' << "______________________________" << '+' <<endl;
    cout << "   Reply: ";
    cin>> choice>> num1>> num2;
    return choice;
    }

bool checkeven(int num1){
    if (num1 % 2 == 0)
        return true;
    else
        return false;
    } 

bool divisible(int num1, int num2){
    
    if (num1 % num2 == 0)
        return true;
    else
        return false;
    }

I'm not going to answer with a complete solution (and you are free to improve your question so you get better answers later), but regarding the question title, here are some hints.

Your declaration

char displaymenu(int num1, int num2);

and your definition

char displaymenu(int &num1 = num1, int &num2 = num2)

should have the same signature. In order to pass references, change the declaration to

char displaymenu(int &num1, int &num2);

Also, since you transfer values by reference, you should get rid of the global variables int num1, num2 = 0; below the using namespace std; . They are not needed anymore. Then, fix the displaymenu function definition by removing the (not working) standard values assignment. After that, at least the compiler should accept your code, just it won't really work for some cases when you execute it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM