Can I have a switch statement in a separate function and call it into main?

Can I have a switch statement in a separate function and call it into main with its variables?

It's an odd question and I don't quite know how to ask it but I know functions can only return at most 1 value, so does that mean I should keep my switch statement inside the main function? I have two separate functions that are void types and call the menu options (cout displays), but I can't seem to get the switch statement in another function right.

Can I have a switch statement in a separate function

Yes.

and call it into main with its variables

I'm not quite sure what you mean by that statement. You can call the separate function from main. You can pass any values you need in the separate function as arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
void some_function (int choice) 
{  switch (choice) 
    {
    case 0:  //  do something
                break;
    case 1:  // do something else
                break;
    }
}

int main ()
{  int choice;
     
    //  display menu
    cin >> choice;
    some_function (choice);
    return 0;
}


so does that mean I should keep my switch statement inside the main function?

Not necessarily. Since it's not clear what you're trying to do, I can't really say. I'm a proponent of having many small functions, rather than stuffing everything into main. It makes debugging and testing easier. Write a function. Test it. Go on to the next function.
Last edited on
I appreciate your reply, I was passing the values through a separate function that had a switch statement but it was a double type function not a void

I'm just confused since in my main function I have a loop that calls one menu and then uses a switch in the main function, and when a case is chosen it branches into the second menu and switch statement which I both have in seperate functions but it doesn't seem to carry the values on as it passes through, like a value will be 2 in one case, and then when I get to the second menu it reverts back to 1
it doesn't seem to carry the values on as it passes through, like a value will be 2 in one case, and then when I get to the second menu it reverts back to 1

It doesn't seem like you're passing your arguments correctly. I'd have to see your code to say for sure.
I've cleaned up most of it actually today, the goal of my code is to take a car and it's price once selected, ask the user if they want to upgrade or downgrade, reset the car price, reset the car itself, and display the Bill.

I seem to have most it right, but i'm stumped on the upgrade and downgrade options, I appreciate again your reply and if you could critique what I've done so far.

#include <iostream>
#include <iomanip>

using namespace std;
int carMenu();
int modMenu(int, int);
int carPrice(int);


int main()
{
int choice = 0;
int engineLevel = 0;
int upgradeCost = 0;
int carCost = 0;
int quoteNumber = 1;
const int hundred = 100;
const int counter = 0;
const int VOLKSWAGEN = 8000;
const int PONTIAC = 8581;
const int CHEVY = 10500;
const int PATRIOT = 15209;
const int WRANGLER = 24390;
int Total = 0;

carMenu();
cin>>choice;
while (choice < 1 || choice > 5)
{
cout<<"Invalid Selection!"<<endl;
cin>>choice;
cin.clear();
cin.ignore();
}
carPrice(choice);

}

int carMenu()
{
int choice = 0;
cout<<"Welcome to Bob's used car lot and chop shop!\n";
cout<<"As you can see we have quite a few to choose from!\n";
cout<<"Which one would you like?: "<<endl;
cout<<"[1] 2005 Volkswagen Beetle ($8,000)"<<endl;
cout<<"[2] 2008 Pontiac G6 ($8,581)"<<endl;
cout<<"[3] 2004 Chevy S-10 ($10,500)"<<endl;
cout<<"[4] 2016 Jeep Patriot ($15,209)"<<endl;
cout<<"[5] 2012 Jeep Wrangler Sport ($24,390)"<<endl;
cout<<"Which car would you like?: ";
}

int carPrice(int choice)
{
const int VOLKSWAGEN = 8000;
const int PONTIAC = 8581;
const int CHEVY = 10500;
const int PATRIOT = 15209;
const int WRANGLER = 24390;
int engineLevel = 0;
int upgradeCost = 0;
int quoteNumber = 1;
int carCost = 0;



switch(choice)
{
case 1:
carCost = VOLKSWAGEN;
break;
case 2:
carCost = PONTIAC;
break;
case 3:
carCost = CHEVY;
break;
case 4:
carCost = PATRIOT;
break;
case 5:
carCost = WRANGLER;
break;
}



cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout<< "{" << quoteNumber++ << "} ";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " E(" << engineLevel << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ")" << endl;
modMenu(choice, carCost);

}


int modMenu(int choice, int carCost)
{
const int VOLKSWAGEN = 8000;
const int PONTIAC = 8581;
const int CHEVY = 10500;
const int PATRIOT = 15209;
const int WRANGLER = 24390;
const int hundred = 100;
const int counter = 0;
int engineLevel = 0;
int upgradeCost = 0;
int quoteNumber = 1;


int Total = 0;
choice = 0;

while(engineLevel != 4)
{

cout<<"Do you want to upgrade your car?"<<endl;
cout<<"[-/+1] Downgrade / Upgrade Engine"<<endl;
cout<<"[ 2] Clear all upgrades"<<endl;
cout<<"[ 3] Reset car"<<endl;
cout<<"[ 4] Buy Car!!!"<<endl;
cout<<"What would you like to do?: ";
cin>>engineLevel;


switch(engineLevel)
{
case 1:
engineLevel++;
for(int counter = 0; counter <= engineLevel; counter++)
{
upgradeCost+= counter * hundred;
}
cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout<< "{" << quoteNumber++ << "} ";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " E(" << engineLevel++ << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ")" << endl;

break;
case -1:
engineLevel--;
for(int counter = 0; counter <= engineLevel; counter--)
{
upgradeCost+= counter * hundred;
}
cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout<< "{" << quoteNumber++ << "} ";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " E(" << engineLevel++ << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ")" << endl;
break;
case 2:
engineLevel = 0;
upgradeCost = 0;
cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout<< "{" << quoteNumber++ << "} ";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " E(" << engineLevel << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ")" << endl;
break;
case 3:
carCost = 0;
engineLevel = 0;
cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout<< "{" << quoteNumber++ << "} ";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " E(" << engineLevel << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ")" << endl;
break;
case 4:
Total = carCost + upgradeCost;
cout<<showpoint<<setprecision(2)<<fixed<<endl;
cout <<"Bill:";
cout << " Car($" << static_cast<double>(carCost) << ")";
cout << " Upgrades($" << static_cast<double>(upgradeCost) << ") ";
cout << "Total ($"<<Total<<")"<<endl;
cout << "Press ENTER to continue";
cin.ignore();
cin.get();
break;
}
}

}
Lines 18-22, 54-58, 99-103: These constants are repeated. While general advice is to avoid globals, that doesn't apply to constants. Declare them once at line 9.

Line 38: Why is this an int function? It doesn't return anything.

Line 40: Why are you declaring choice here?

Line 53: carPrice() is an int function, but doesn't return anything.

Line 66-83: There a simpler way than a switch statement.
1
2
3
  const int costs[6] = { 0, VOLKSWAGEN, PONTIAC, CHEVY, PATRIOT, WRANGLER };
...
  carCost = costs[choice];


Line 98: modMenu is an int function, but doesn't return anything.

Line 98: What's the point of passing choice as an argument? You set it to 0 at line 113, then don't use it.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.