-1

I was trying to make a list of 3 options. If I type '1', option 1 should be printed. What I don't know how to do is print the result.

My attempt so far:

#include <iostream>
using namespace std;

int Menu(int a)
{
    cout << "Choose an option: " << endl;
    cout << "1.Option 1" << endl;
    cout << "2.Option 2" << endl;
    cout << "3.Option 3" << endl;
    cin >> a;
    return a;
}

char printResult()
{
    char op;

    if (op == '1')
        cout << "OPTION 1";
    if (op == '2')
        cout << "OPTION 2";
    if (op == '3')
        cout << "OPTION 3";
    }

int main()
{   
    char op;
    int a;
    Menu(a);
    printResult();
}
A-Tech
  • 806
  • 6
  • 22
  • 1
    You appear to know about `std::cout`. How about using that to print the result? – eerorika Feb 07 '18 at 13:00
  • 3
    You never assign to `op` in *any* of your functions. Also remember that functions introduce a new scope, and variables at different scopes are different variables, even if they have the same name. In other words, the variable `op` in the `main` function is a different variable from `op` in the `printResult` function. There's also a difference between numbers and characters. The character `'1'` is different from the number `1`. I recommend you [get a couple of good beginners books](https://stackoverflow.com/a/388282/440558) and start over from the beginning with them. – Some programmer dude Feb 07 '18 at 13:00
  • You have to pass the result of `Menu` as parameter to `printResult` and check that instead of your local `char op;`. – Blacktempel Feb 07 '18 at 13:00
  • Maybe `printResult` could take op as an argument. And maybe you could remove that useless argument in `Menu` (or make it a reference, but I personnally think it'd be bad style to "output" that way) – Caninonos Feb 07 '18 at 13:02
  • I'd personally wouldn't make Menu a function at all. Outsourcing that part of the code into a function makes the code less readable. – Fang Feb 07 '18 at 13:05
  • 1
    @Fang I disagree - writing more smaller functions generally aligns with the SRP. It doesn't matter much for this little exercise, but getting in the practice early of preferring more smaller functions to fewer longer functions forms better design habits than the opposite. (I tend to be guilty of writing monster functions myself...) – aschepler Feb 07 '18 at 13:13

2 Answers2

1

You need to pass through the value from where it is put int to where it is processed / put out:

char Menu()
{
    char a;
    cout << "Choose an option: " << endl;
    cout << "1.Option 1" << endl;
    cout << "2.Option 2" << endl;
    cout << "3.Option 3" << endl;
    cin >> a;
    return a;
}

void printResult(char op)
{
    if (op == '1')
        cout << "OPTION 1";
    if (op == '2')
        cout << "OPTION 2";
    if (op == '3')
        cout << "OPTION 3";
}

int main()
{   
    char op = Menu();
    printResult(op);
}

Note that function parameters like void printResult(char op) { ... are different from local variables like void printResult() { char op; .... The former consumes a value that needs to be passed by the caller of the function, e.g. printResult('1') or printResult(someVariable). The latter does not accept any parameter but uses a "local" variable which is visible and valid in the scope of the function body only - so no way to transport values from outside to inside and vice versa this way.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

Menu has both a return value and argument, but you really only need the return value.

int Menu()

printResult doesn't need to return anything, but needs to be told what the value is, so pass it in

void printResult(int a)
{
    if (op == 1)
        cout << "OPTION 1";
    if (op == 2)
        cout << "OPTION 2";
    if (op == 3)
        cout << "OPTION 3";
}

Then main can be simplified a little.

int main()
{   
    int a = Menu();
    printResult(a);

    return 0;
}

I added the "return 0;" because main was declared as returning an int and you would get a warning about it.

Ben
  • 298
  • 1
  • 8