私はstackoverflowを使い慣れていませんし、ややプログラミングの面でも新しいので、コードのフォーマットが貧弱なことに気をつけてください。私は自分のコードに2つの問題があります。私は何が間違っているのですか?論理中に、そして論理を続けますか?
- 「y」または「Y」と入力するとループを続けるために使用しているMy continue文が機能しません。停止せずに過去の0を行き、私はプログラムのロジックで私のエラーを見ることができない2.Myはカウンターを継続
:それは私を導く、のみが正しく推測を取得した後、プログラムを終了します。
私のロジックに問題はありません。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <random>
using namespace std;
int getNumber(); //random number prototype
double getScore(); //gets score
int chances = 7; //chances to guess with
int main()
{
int guess = 0,
random;
char retry = 'y'; //initialize retry to 'y'
cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
<< "You have 7 chances. Good luck! \n \n" << endl;
random = getNumber(); //give the function a variable
do
{
cout << random << "\n" << "\n";
chances--;
cout << "Enter your guess: ";
cin >> guess;
if (guess == random)
{
cout << "You have won the game! " << "Your score was: " << getScore();
cout << "Would you like to retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue; //player can retry the game
}
else if (chances == 0)
{
cout << "You have no chances left. Retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue;
}
}
return 0;
}
else if (guess != random)
cout << "You got it wrong. \n" << "You have: " << chances << " chances left" << endl << endl;
else
cout << "Incorrect Input. Please type a number." << endl << endl;
} while (guess != random);
return 0;
}
int getNumber()
{
unsigned seed = time(0); //seed the random number
srand(seed);
int randNum = rand() % 10 + 1; //random number in the range of 1-10
return randNum;
}
あなたの貧弱なフォーマットは気になります。新しいものであっても、コードをうまくフォーマットするために時間と労力を費やすことを妨げるものではありません。例については、既存の成功した質問をいくつでも見ることができます。 Stack Overflowは優れたライブポストプレビューを備えているので、ポストを見栄え良くするために必要な時間を費やすことができます。 –
'if(再試行== 'y' || 'Y')'これはあなたが思うことをしません – JackVanier