C++ 打印错误消息

C++ 打印错误消息,c++,C++,嘿,伙计们,这是我的程序,但是如果用户输入的赌注大于100,我想打印一个错误声明。到目前为止,在我的程序中,我做了一个do-while循环,但实际上我想打印一条错误消息,说明下注金额必须小于100。如果有人能帮忙,那就太好了。谢谢 #include <iostream> #include <string> using namespace std ; int GetBet (); string PullOne (); int GetPayMultiplier (strin

嘿,伙计们,这是我的程序,但是如果用户输入的赌注大于100,我想打印一个错误声明。到目前为止,在我的程序中,我做了一个do-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;
}

为什么这么难?你已经有了循环。只需在循环中输入一条错误消息

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

为什么这么难?你已经有了循环。只需在循环中输入一条错误消息

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;
}
试试这个-

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;
}
试试这个-

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;
}
我想你想要这个

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;
}
我想你想要这个

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

我的格式很奇怪,我无法修复我的问题,无法在上启用代码格式,所以只需选择代码并按Ctrl-K,或单击文本框工具栏上的{}按钮。这看起来像是家庭作业。如果是,您将要添加家庭作业标记。我的格式很奇怪,我无法修复它。要启用代码格式,只需选择代码并按Ctrl-K,或单击文本框工具栏上的{}按钮。这看起来像家庭作业。如果是的话,你会想添加家庭作业标签。打印出来的信息在英语中没有多大意义,但这是正确的。你可能还应该检查少于零的下注金额和非数字下注金额。我试图幽默,特别是如果读到来自辛普森一家的克莱特斯声音。对不起,你没拿到。您可以将消息更改为您的taste@JohnFx:不是OP要求的。应该是我指示的具体对象。该评论是针对OP的,不是针对你。打印出来的信息在英语中没有多大意义,但从这一点可以看出这是正确的。你可能还应该检查少于零的下注金额和非数字的下注金额。我试图幽默,特别是如果用《辛普森一家》中的克莱特斯语调阅读的话。对不起,你没拿到。您可以将消息更改为您的taste@JohnFx:不是OP要求的。应该是我指示的具体对象。那评论是写给OP的,不是你。