0

I created a function which takes a string for a message and an input value (primarily an integer, at least in this case) so I can print a message and take an input in a single function instead of two lines of code. Here is the snippet:

main.h

void input(std::string msg, int choice) {
    std::cout << msg;
    std::cin >> choice;
}

However, when passing the function in my main function, it doesn't perform the same way this would:

main.cpp (working code with expected result)

int initial_choice;
std::cout << "Invalid input provided. Please try again: ";
std::cin >> initial_choice;

main.cpp (working code with unexpected result)

int initial_choice;
input("Invalid input provided. Please try again: ", initial_choice);

When it comes to the snippet with the expected result, my switch case function properly detects the input (in this case it is initial_choice) as a valid integer. For example, entering 1, 2, or 3 will work with case 1:, case 2:, and case 3:. However, the snippet with the unexpected result doesn't detect any of the three valid numbers in my switch case function, instead they all run the code under the default case.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 3
    You should read about pass-by-value and pass-by-reference in C++. – user202729 Jan 04 '21 at 16:19
  • When you pass a variable as `int choice`, the compiler actually makes a copy of the parameter. Therefore, modifications to `choice` happen on the copy, not the original. Passing by reference ensures that all references to `choice` affect the original. – h0r53 Jan 04 '21 at 16:28
  • 2
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Dec 14 '22 at 08:24

1 Answers1

2

This has nothing to do with cin, cout, switch statements, etc.

As user202729 has pointed out, it is a simple matter of function parameter-passing.

C++ is not Fortran III; by default, parameters to functions are passed by value, not by reference. This means that if within a function you change the value of one of its parameters, this change will only be in effect within the function; the change will not be reflected to the caller.

So, your std::cin >> choice; statement works perfectly well, and it stores the value entered by the user in your choice variable. However, this value forever ceases to exist once the function ends, and it is never reflected back to the caller, which in this case is the initial_choice variable of your main() method.

To fix this, you have two options:

  1. Pass the variable by reference, so that the function can modify it.
  2. Do not pass the variable at all; instead, have the function return the value; that's generally what functions are for.
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142