1

Im trying to run fuctions inside of 3 cases (case1,case2,case3) but when i run the code nothing happens so ive been trying to reseach how you put a function inside of a case correctly in c++ but iv ehad no luck.

Ive tried to run the code inside of the cases without linking to the functions and it works but for the outcomes i need to link a caseto a function.

switch (choice)
//wont run case functions   
{
    case 1:
        void getnames();
        break;
    case 2:
        void getNumber();
        break;
    case 3:
        cout << "End of Program.\n";
        break;
    default:
        cout << "Not a Valid Choice. \n";
        cout << "Choose again.\n";
        break;
}

The result i need is that the program will run the fuctions when the right number is inputed the program runs fine but it just keeps looping the menus and does not run the functions

Tobias Wollgam
  • 761
  • 2
  • 8
  • 25

3 Answers3

3

That's because it looks like you're trying to declare the functions there rather than call them. Give this a try:

{
   case 1:
      getnames();
      break;
   case 2:
      getNumber();
      break;
 ...
Component 10
  • 10,247
  • 7
  • 47
  • 64
0

Remove the void keywords, and assuming the functions exist, this will work, as @Tyler commented.

So now, your code should look like this:

  {
     case 1:
        getnames();
        break;
     case 2:
        getNumber();
        break;
     case 3:
        cout << "End of Program.\n";
        break;
     default:
        cout << "Not a Valid Choice. \n";
        cout << "Choose again.\n";
        break;

    }
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Yes Thanks i have changed my code to that thanks now i have a validation issue that means that it continusly loops the menu and displays invalid input whatever is inputed – user12334252 Nov 06 '19 at 21:16
  • 1
    Take a debugger to it. Put a breakpoint on the `switch (choice)` line and inspect the `choice` variable to see what it contains. – dgnuff Nov 06 '19 at 21:52
0

change

void getnames();

to

getnames();

the first from declare a function not invoking it. so nothing will happen.

Ahmed Anter
  • 650
  • 4
  • 13