最近C++を学び始めて、最初の "ゲーム" /プログラムを作りたかったのです。私はいくつかの困難に遭遇しました。C++ - 私は最初のプログラムをコーディングしていますが、いくつかの問題に遭遇しました。
次のように私のエラーは今のところ、次のとおりです。
(rand()%a)
- >「」doesn'tを変更生成された番号が2であり、「」生成された番号がまま1である場合(たとえば、何かを2)。次のコードは動作しません。
while(!(b = c)){ cout << "Enter your guess! \n"; cin >> c; if(c<b){ cout << "Bigger! \n"; } if(c>b){ cout << "Smaller! \n"; } d++; }
次のように私の完全なプログラムは次のとおりです。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Max. Limit
int a;
// Random Number
int b;
// Guess
int c;
// Tries counter
int d = 0;
cout << "Enter highest possible number, setting the max. limit for the program. \n";
cin >> a;
srand(time(0));
b = 0 + (rand()%a);
if(b =! 1){
c = 1;
}
if(b = 1){
c = 2;
}
while(!(b = c)){
cout << "Enter your guess! \n";
cin >> c;
if(c<b){
cout << "Bigger! \n";
}
if(c>b){
cout << "Smaller! \n";
}
d++;
}
if(b=c){
cout << "Congratulations! You have guessed was right! The number was indeed " << b << " !" << endl;
cout << "You needed " << d << " tries to find the number! \n";
}
return 0;
}
while(!(b = c)){'b = c '部分は比較ではなく代入です。 – drescherjm
したがって、b = c – antlersoft
の代わりにb == cを使用すると、 'b =! 'b!= c' – mike510a