开发者

printing an error message

Hey guys this is my program, however I want to print out an error statement if the user enters a bet greater than 100. In my program so far I did a do while loop but I want to actually print an error message saying "Bet amount must be less than 100". If anyone could help that would great. Thanks

#include <iostream>
#include <string>
using namespace std ;

int GetBet ();
string PullOne ();
int GetPayMultiplier (string s1, string s2, string s3);
void Display (string s1, string s2, string s3, int winnings);

int main ()
{
   int betamount;
   string s1;
   string s2;
   string s3;
   int winnings;
   betamount= GetBet();
   while  (betamount !=0)
   {
      s1=PullOne();
      s2= PullOne ();
      s3= PullOne ();
      winnings = betamount * GetPayMultiplier(s1, s2, s3);
      Display(s1, s2, s3, winnings);
      betamount= GetBet();
   }
}

int GetBet ()
{
   int betamt;
   do
   {
      cout << "Enter Bet amount from 0 to 100. Enter 0 to quit" <<endl;
      cin >> betamt;
   }

   while (betamt > 100);

   return betamt;
}

string PullOne ()
{
   int chance;
   string slots[4] = {"Bar", "7", "cherries", "space"};
   chance= rand() %4;
   return slots [chance];
}

int GetPayMultiplier (string s1, string s2, string s3)
{
   int multiplier;
    string slots[4] = {"Bar", "7", "cherries", "space"};

   if (s1== slots[2] && s2 != slots[2])
      multiplier = 3;
   else if (s1 == slots[2] && s2== slots [2] && s3 != slots[2])
      multiplier =10;
   else if (s1 == slots[2] && s2 == slots[2] && s3== slots[2])
      multiplier = 20;
   else if ( s1== slots[0] && s2 == slots[0] && s3== slots[0])
      multiplier = 35;
   else if ( s1 == slots[1] && s2== slots[1] && s3 == slots[1])
      multiplier = 50;
   else 
      multiplier=0;

   return multiplier;
}

void Display (string s1, string s2, string s3, int wi开发者_开发知识库nnings)
{
   cout << s1 << "  " << "   " << s2 << "   " << s3 << endl;
   if( winnings==0)
      cout << "Sorry You Lose" << endl;
   else 
      cout << "Congratulations you have won " << winnings << " dollars"<< endl;
}


why is this hard? You already have the loop. Just put an error message into the loop.

int GetBet ()
{
   int betamt;
   do
   {
      cout << "Enter Bet amount from 0 to 100. Enter 0 to quit" <<endl;
      cin >> betamt;

      // ====== Error message comes from here
      if (betamt > 100)
      {
          cout << "You done entered a bad bet amount. Try again!" << endl;
      }
   }  while (betamt > 100);

   return betamt;
}


Try this -

int GetBet ()
{
   int betamt;
   do
   {
      cout << "Enter Bet amount from 0 to 100. Enter 0 to quit" <<endl;
      cin >> betamt;

      if( betamt > 100 )
      {
          std::cout << "Bet amount must be less than 100" << std::endl ;
      }
      else
      {
          break;
      }
   }while (1);

   return betamt;
}


I Think you want this

int GetBet()
{

int betamt;

cout << "Enter bet amount from 0 to 100."
cin >> betamt;

while(betamt>100)
{
   cout <<"Bet amount must be less than 100"<<endl;
   cin >>betamt;
}

return betamt;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜