ChatGPT3写的,有问题,求找错
  • 板块灌水区
  • 楼主HBr_FHM
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/4/18 21:09
  • 上次更新2023/10/23 18:06:05
查看原帖
ChatGPT3写的,有问题,求找错
750120
HBr_FHM楼主2023/4/18 21:09
#include<bits/stdc++.h>
using namespace std;
class GuessingGame{
	private: string word;
	string guess;
	int maxGuesses;
	int numGuesses;
	public: GuessingGame(string w, int mg){
		word = w;
		maxGuesses = mg;
		numGuesses = 0;
		for(int i = 0; i < word.length(); i++){ 
			guess += "_"; 
	}
}
void play() {
    while (numGuesses < maxGuesses && guess != word) {
        cout << "Guess the word: " << guess << endl;
        char letter;
        cout << "Enter a letter: ";
        cin >> letter;
        if (word.find(letter) != string::npos) {
            cout << "Correct!" << endl;
            for (int i = 0; i < word.length(); i++) {
                if (word[i] == letter) {
                    guess[i] = letter;
                }
            }
        }
		else{
            cout << "Incorrect!" << endl;
            numGuesses++;
        }
    }
    if (guess == word)cout << "Congratulations! You guessed the word: " << word << endl;
	else cout << "Sorry, you ran out of guesses. The word was: " << word << endl;
}
int main(){
	srand(time(NULL));
	string words[6] = {"apple", "banana", "cherry", "orange", "pear"};
	int numWords=sizeof(words)/sizeof(words[0]);
	int randomIndex = rand() % numWords;
	string word = words[randomIndex];
	GuessingGame game(word, 5);
	game.play();
	return 0;
}
2023/4/18 21:09
加载中...