1
#include <iostream>
#include <vector>
using namespace std;

void printing(vector<int> numbers);
char ask(char selection);

int main(){
    vector<int> numbers{1,1,2};
    char selection{};
    ask(selection);

    switch(selection) { 
      case 'P':
        printing(numbers);
        break;
      case 'M':
        cout << "FF" << endl;
        break;
      default :
        cout << "error" << endl;
    }
    printing(numbers);
    return 0;
}
    
char ask(char selection)
{        
    cout << "Enter the selection " << endl;
    cout << "\nP - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number"<< endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter your selection: ";
    cin  >> selection;
    return selection;
}
    
void printing(vector<int> numbers){
        
    if (numbers.size() == 0){
        cout << "[] - the list is empty" << endl;
    }
    else{
        cout << "[ " ;
        for (auto num : numbers){ cout << num << " " ; }
        cout << "] " << endl;
    }
}

Here the switch statement is not working, it's not printing the vector that I need.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    It "does not work" because `selection` does not get a selected value. – 273K Sep 15 '22 at 02:51
  • 1
    `char ask(char selection)` in the variable `selection` is passed by value (meaning it makes a copy) which seems pointless since the ask() function does not care about the previous value. In your main you call `ask(selection);` and that threw away the return value from the ask() function. – drescherjm Sep 15 '22 at 03:33
  • This may help you understand your bug: [https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – drescherjm Sep 15 '22 at 03:37
  • @drescherjm that should be made into an actual answer. – Remy Lebeau Sep 15 '22 at 04:00

1 Answers1

1

You have declared your function char ask(char selection) to take selection by value. Meaning the calling function passes a copy of the char to the ask() function. Any changes to the copy will only be seen in the ask() function and not the function that calls it.

char ask(char selection)
{        
    cout << "Enter the selection " << endl;
    cout << "\nP - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number"<< endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter your selection: ";
    cin  >> selection;
    return selection;
}

This passing by value is odd since you don't use the value passed into the function and instead want the value from the function. One way to fix this is to change the function to not take a char.

char ask()
{        
    char selection;
    cout << "Enter the selection " << endl;
    cout << "\nP - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number"<< endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter your selection: ";
    cin  >> selection;
    return selection;
}

then in your calling function int main() use the value returned:

   char selection{ask()};

instead of:

   char selection{};
   ask(selection);

For more information about pass by value see this question: What's the difference between passing by reference vs. passing by value?

drescherjm
  • 10,365
  • 5
  • 44
  • 64