Solved

need help using a loop and switch statement

Posted on 2004-10-10
8
214 Views
Last Modified: 2010-04-01
hey jkr
i am working on that assignment still and i am trying to use a loop to control the program.  I cant get it to go inside my switch statment and pull out the information.  the user starts out with 1000$ and it is to keep running until the user runs out of money or desides to quit.  any help you can give me i will appreciate hopefully you can read what i was tryen to do.
thanks
ted

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(){
int Dice1        = 0;
int Dice2        = 0;
float Bet        = 0;
int Point        = 0;
int DiceNum1     = 0;
int DiceNum2     = 0;
int SumOfDice    = 0;
float TotalMoney = 1000;
int Counter = 1;

      cout << "Welcome to CRAPS\n\n" << "You have $1,000 dollars\n" << "Please Place A Bet!!\n" << endl;
      cin >>  Bet;
      




srand(time(0));
Dice1 = 1 + rand() % 6;
Dice2 = 1 + rand() % 6;


SumOfDice = Dice1 + Dice2;



while(TotalMoney > 0)
      
Counter = Counter++;




switch(SumOfDice){

case 2:
      if(Counter == 1){
      TotalMoney = TotalMoney - Bet;
      cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
break;
      }
      else if(Counter > 1){
      cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
break;
      }
case 3:
      if(Counter == 1){
      TotalMoney = TotalMoney - Bet;
      cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
break;
      }
      else if(Counter > 1){
      cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
break;
      }

case 4:      
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;      
      }
      else if(Counter > 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      
break;
      }
case 5:
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;      
      }
      else if(Counter > 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      
break;
      }
case 6:
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;
      }      
      else if(Counter > 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      
break;
      }
case 7:
      if(Counter == 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled a " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;
break;
      }
      else{
      TotalMoney = TotalMoney - Bet;
      cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
break;
      }
case 8:
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;
      }
      else if(Counter > 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      
break;
case 9:
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;
      }
      else if(Counter > 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      

break;
      }
case 10:
      if(Counter == 1){
      cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
break;      
      }
      else if(Counter > 1)
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;      
break;
      }
case 11:
      if(Counter == 1){
      TotalMoney = TotalMoney + Bet;
      cout << "WINNER!!! You rolled a " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;
break;      
      }
      else{
      cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
break;
      }
case 12:
      if(Counter == 1){
      TotalMoney = TotalMoney - Bet;
      cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
break;
      }
else if(Counter > 1)
      cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;

break;
}
0
Comment
Question by:tedschnieders
  • 7
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12272848
It's not entering your switch statement because of your while loop

while(TotalMoney > 0)
            Counter = Counter++;

There's no way for this loop to exit.
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 12272855
If you want your switch statement to be inside the while loop, then you need to add start and end braces{}

Example:
      while(TotalMoney > 0)
      {      
            Counter = Counter++;
            
            switch(SumOfDice){
                  
            case 2:
                  if(Counter == 1){
                        TotalMoney = TotalMoney - Bet;
                        cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
                        break;
                  }
0
 
LVL 30

Expert Comment

by:Axter
ID: 12272864
The case 8 section is missing an end brace.
      case 8:
            if(Counter == 1){
                  cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                  break;
            }
            else if(Counter > 1){
                  TotalMoney = TotalMoney + Bet;
                  cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                  break;


And case 12 is also missing an end brace.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12272887
Actually, case 12 is missing a start and end brace.

Also case 10 is missing a start brace.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Axter
ID: 12272890
Here's code with the corrections:

int main(){
      int Dice1        = 0;
      int Dice2        = 0;
      float Bet        = 0;
      int Point        = 0;
      int DiceNum1     = 0;
      int DiceNum2     = 0;
      int SumOfDice    = 0;
      float TotalMoney = 1000;
      int Counter = 1;
      
    cout << "Welcome to CRAPS\n\n" << "You have $1,000 dollars\n" << "Please Place A Bet!!\n" << endl;
      cin >>  Bet;

      srand(time(0));
      Dice1 = 1 + rand() % 6;
      Dice2 = 1 + rand() % 6;
      
      
      SumOfDice = Dice1 + Dice2;
      
      
      
      while(TotalMoney > 0)
      {      
            Counter = Counter++;
            switch(SumOfDice)
            {
            case 2:
                  if(Counter == 1){
                        TotalMoney = TotalMoney - Bet;
                        cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
                        break;
                  }
            case 3:
                  if(Counter == 1){
                        TotalMoney = TotalMoney - Bet;
                        cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
                        break;
                  }
                  
            case 4:    
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;    
                  }
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 5:
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;    
                  }
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 6:
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;
                  }    
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 7:
                  if(Counter == 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled a " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;
                        break;
                  }
                  else{
                        TotalMoney = TotalMoney - Bet;
                        cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
                        break;
                  }
            case 8:
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 9:
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 10:
                  if(Counter == 1){
                        cout << "Your Point is Set at " << SumOfDice <<" Please roll again" << endl;
                        break;    
                  }
                  else if(Counter > 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled your POINT of " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;    
                        break;
                  }
            case 11:
                  if(Counter == 1){
                        TotalMoney = TotalMoney + Bet;
                        cout << "WINNER!!! You rolled a " << SumOfDice << " You Just won " << Bet <<" Dollars" << endl;
                        break;    
                  }
                  else{
                        cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
                        break;
                  }
            case 12:
                  if(Counter == 1){
                        TotalMoney = TotalMoney - Bet;
                        cout << "SORRY!! You did not win......:( PLEASE TRY AGAIN!!" << endl;
                        break;
                  }
                  else if(Counter > 1){
                        cout << "You did not roll your POINT!! PLEASE ROLL AGAIN!!!" << endl;
                        break;
                  }
            }
      }
      return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 12272892
You should also add a break statement after each else-if condtion statement.
0
 

Author Comment

by:tedschnieders
ID: 12273109
how would i keep track of the money i can't do it in the switch statement because it keeps subtracting it til it equals 0 or it just keeps looping if it is adding
0
 
LVL 30

Expert Comment

by:Axter
ID: 12273361
>>how would i keep track of the money i can't do it in the switch statement because it keeps subtracting it til it equals 0 or it just
>>keeps looping if it is adding

In looking at your code, it looks like you also need to move the while loop much higher in your code, so as to include the random input and the user input.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 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

19 Experts available now in Live!

Get 1:1 Help Now