Home > database >  "0" appearing after function is completed in C
"0" appearing after function is completed in C

Time:03-02

// This program is able take two variables`
// and apply Auto increment or decrement`
// based on the user's input and by calling a function

#include <iostream>

using namespace std;

int inc (int, int); // Increment function prototype
int dec (int, int); // Decrement function prototype

int main () 
{
    int num1, num2;
    char operation = ' ', I, D;

    cout << "Enter the first variable: ";
    cin >> num1;
    cout << "Enter the second variable: ";
    cin >> num2;
    cout << endl;
    
    cout << "Do you want Auto increment, decrement or both? (Type 'I', 'D' or 'B'): ";
    cin >> operation;
    
    if (operation == 'I') {
        cout << inc(num1,num2);
    }
    else if (operation == 'D') {
        cout << dec(num1,num2);
    }
    else if (operation == 'B') {
        cout << inc(num1,num2);
        cout << endl;
        cout << dec(num1,num2);
    }
    else {
        cout << "Error please enter 'I', 'D' or 'B' as a option" << endl;
    }

    return 0;   
}

int inc (int num1, int num2) {
    num1  ; num2  ;
    
    cout << "Number 1    is: " << num1 << endl;
    cout << "Number 2    is: " << num2 << endl;
    
    return 0;
}

int dec (int num1, int num2) {
    --num1; --num2;
    
    cout << "Number 1 -- is: " << num1 << endl;
    cout << "Number 2 -- is: " << num2 << endl;
    
    return 0;
}

I just started taking Fundamentals of programming I at college and i have this assignment and this 0 keeps appearing after the function has completed

CodePudding user response:

This happens because your inc and dec print stuff, but also return an int. You then proceed to print the return value, on these lines:

cout << inc(num1,num2);

and

cout << dec(num1,num2);

.

You can safely remove these cout << prefixes, and probably the entire return value, as it does not seem necessary to output your resuls (that happens in inc and dec).

CodePudding user response:

int inc (int &num1, int &num2) int dec (int &num1, int &num2)

Don't forget the ampersand to modify globalvariables

CodePudding user response:

    if (operation == 'I') {
        cout << inc(num1,num2);
    }
    else if (operation == 'D') {
        cout << dec(num1,num2);
    }
    else if (operation == 'B') {
        cout << inc(num1,num2);
        cout << endl;
        cout << dec(num1,num2);
    }

In this section don't use cout. The function is returning 0 and you are printing it using cout << dec(num1,num2);.

Just call the function as you are printing the values inside the function.

    if (operation == 'I') {
        inc(num1,num2);
    }
    else if (operation == 'D') {
        dec(num1,num2);
    }
    else if (operation == 'B') {
        inc(num1,num2);
        cout << endl;
        dec(num1,num2);
    }

And also as if you want to modify num1 and num2 inside the functions so that the variables declared in main function is modified also, then you have to pass by reference like:

int inc (int &num1, int &num2) {
    num1  ; num2  ;
    
    cout << "Number 1    is: " << num1 << endl;
    cout << "Number 2    is: " << num2 << endl;
    
    return 0;
}

int dec (int &num1, int &num2) {
    --num1; --num2;
    
    cout << "Number 1 -- is: " << num1 << endl;
    cout << "Number 2 -- is: " << num2 << endl;
    
    return 0;
}
  •  Tags:  
  • c
  • Related