我是 C++ 新手,想知道为什么无论生成的随机数是什么,输出总是正面。我假设该函数默认为读取第一个 if 语句,但我不知道如何让它在输出文本之前检查这两个选项。提前致谢。
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int coinToss(int);
int main()
{
 int input;
 int counter;
 int toss;
 int check;
 cout<<"Enter the number of times the coin will be tossed:";
 cin>>input;
 coinToss(input);
 system("PAUSE");
 return 0;
}
int coinToss(int input)
{
 int toss;
 int counter = 1;
 while(counter<=input)
 {
  toss = rand() % 2 + 1;
  int check = toss;
  cout<<check<<endl;
  if (toss = 1)
  {
  cout<<"Heads"<<endl;
  }
  else if (toss = 2)
  {
  cout<<"Tails"<<endl;
  }
  counter += 1;
 }
}