[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

basic C++ question

Hi!
I'm starting to learn C++
I'm trying to do the following:

void evaluateCommand(Coins & piggyBank, char choice)
{
      int quarters, dimes, nickels, cents;
      switch(choice)
      {
      case 'B':case 'b':
            cout <<"Total amount is $" << piggyBank.total();
            break;
      case 'C':case 'c':
            cout <<"C was chosen";
            break;
      case 'D':case 'd':
            cout <<"How many quarters? ";
            cin >>quarters;
            cout <<"How many dimes? ";
            cin >>dimes;
            cout <<"How many nickels? ";
            cin >>nickels;
            cout <<"How many cents? ";
            cin >>cents;
            //piggyBank.depositChange(makeDepositObject(quarters, dimes, nickels, cents));
            Coins deposit(quarters,dimes,nickels,cents);
            piggyBank.depositChange(deposit);
            break;
      case 'P':case 'p':
            cout <<"P was chosen";
            break;
      case 'Q':case 'q':
            cout <<"Good bye!";
            exit(0);
      default:
            cout <<choice<<" is a invalid command";
            break;
      }


It works fine if I make a function to initialize the object(commented out section), but it throws an 5 error messages "initialization of 'deposit' is skipped by 'case label." when I try to directly initialize the object with in a case statement.  Is it suppose to do that or am I doing something wrong?

Thanks

0
chulsoft
Asked:
chulsoft
1 Solution
 
mnewton022700Commented:
You need to put brackets around the stuff in the "case 'D'" section

case 'D':case 'd':
{
cout <<"How many quarters? ";
cin >>quarters;
cout <<"How many dimes? ";
cin >>dimes;
cout <<"How many nickels? ";
cin >>nickels;
cout <<"How many cents? ";
cin >>cents;
//piggyBank.depositChange(makeDepositObject(quarters, dimes, nickels, cents));
Coins deposit(quarters,dimes,nickels,cents);
piggyBank.depositChange(deposit);
}
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now