Solved

basic C++ question

Posted on 2000-04-04
1
215 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now