0

The program allows the user to choose what operation to use first before the user can input 5 numbers.

im trying to lessen my line of codes by creating a function that will get 5 numbers of user inputs so i can just call that function every time it is needed instead of typing it all 4times inside my if else statement.

the problem is when i called the inputs function inside my if else statement it does not read the values inputted inside the inputs function.

this is my code.

#include <iostream>
using namespace std;

int main() {
    char Choice;


    cout << "MATH OPERATIONS\n[+]\tAddition\n[-]\tSubtraction\n[*]\tMultiplication\n[/]\tDivision";
    cout << "\nEnter you Choice: ";
    cin >> Choice;

    if ( Choice == '+') {
        inputs();
        double answer = num1 + num2 + num3 + num4 + num5;
        cout << "Sum : " << answer << endl;
    }
    else if (Choice == '-') {}
    else if (Choice == '*') {}
    else if (Choice == '/') {}
    else {}
}
int inputs(int num1, int num2, int num3, int num4, int num5) {
    cout << "1st Number: ";
    cin >> num1;
    cout << "2nd Number: ";
    cin >> num2;
    cout << "3rd Number: ";
    cin >> num3;
    cout << "4th Number: ";
    cin >> num4;
    cout << "5th Number: ";
    cin >> num5;
} 
GraiL
  • 23
  • 5
  • Change `int inputs(int num1, int num2, int num3, int num4, int num5) {` to `int inputs(int& num1, int& num2, int& num3, int& num4, int& num5) {` and the declaration accordingly (needs to go before `main() {`).. – πάντα ῥεῖ Sep 30 '20 at 16:26
  • https://www.cs.fsu.edu/~myers/c++/notes/references.html – PM 77-1 Sep 30 '20 at 16:29
  • after that how do I call it inside my if else statement ``` if ( Choice == '+') { inputs(); double answer = num1 + num2 + num3 + num4 + num5; cout << "Sum : " << answer << endl; } ``` – GraiL Sep 30 '20 at 16:34
  • @GraiL Your function has five parameters, so you need to supply five parameters when you call it. `inputs(num1, num2, nu3, num4, num5);` – john Oct 01 '20 at 06:02

1 Answers1

1

If you want a function to change the parameters passed to it, you should pass by reference

void inputs(int& num1, int& num2, int& num3, int& num4, int& num5) {

No obvious reason for the function to return an int so I changed that to void.

Also I think you need to decide between int and double, code above seems a little unsure on that.

john
  • 85,011
  • 4
  • 57
  • 81
  • There's the problem with the missing declaration also. Next thingy OP will complain about. – πάντα ῥεῖ Sep 30 '20 at 16:28
  • the answer possible to have decimal places that is why i declared answer as double because double accepts int but int does not accept double. – GraiL Sep 30 '20 at 16:36
  • @GraiL So presumably you want the inputs to be double as well? That's all I was saying that you probably should be consistent. – john Oct 01 '20 at 06:00
  • @john yes, later on I realized that the user may want to input a double, that is why I stick to double. having a decimal place even the answer is a whole number is not a big deal in my case – GraiL Oct 02 '20 at 10:26