打印错误消息

时间:2011-02-15 22:47:07

标签: c++

嘿伙计这是我的程序,但是如果用户输入的赌注大于100,我想要打印出一个错误声明。在我的程序到目前为止我做了一个while循环但是我想实际打印一条错误消息说“投注金额必须小于100”。如果有人能提供帮助那就太好了。感谢

#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 winnings)
{
   cout << s1 << "  " << "   " << s2 << "   " << s3 << endl;
   if( winnings==0)
      cout << "Sorry You Lose" << endl;
   else 
      cout << "Congratulations you have won " << winnings << " dollars"<< endl;
}

3 个答案:

答案 0 :(得分:4)

为什么这很难?你已经有了循环。只需将错误信息放入循环中即可。

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;
}

答案 1 :(得分:0)

试试这个 -

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;
}

答案 2 :(得分:0)

我想你想要这个

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;
}