Solved

basic C++ question

Posted on 2000-04-04
1
218 Views
Last Modified: 2010-04-02
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
Comment
Question by:chulsoft
1 Comment
 
LVL 3

Accepted Solution

by:
mnewton022700 earned 50 total points
ID: 2685523
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question