저는 Cpp에서 행맨 게임을했습니다 ... SoFar
이라는 변수를 작성하여 시작 부분에 대시를 저장하지만 점차 밝혀졌습니다. 내가 SOFAR 인쇄 할 때문자열이 제대로 인쇄/초기화되지 않았습니다
for(i = 0; i <= TheWord.length(); i++)
SoFar[i] = '-';
그래서, 나는 시작의 대시를 포함 할 SoFar
를 초기화 (시도), 그리고 나중에
TheWord
같은 길이, 그냥 비어!
cout << "\nSo far, the word is : " << SoFar << endl;
일체의 조언에 감사드립니다. 여기에 내 전체 프로그램은 참조 용입니다 : 비어
string SoFar;
const int MAXTRIES = 8;
string used = "";
int tries, i;
i = tries = 0;
char guess;
for(i = 0; i <= TheWord.length(); i++)
SoFar[i] = '-';
SoFar
때문에, SoFar[i]
이 정의되지 않은 동작입니다 수정하려고 : 당신은 존재하지 않는 개체를 수정하려는
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
using namespace std;
int main()
{
vector<string> words;
words.push_back("SHAWARMA");
words.push_back("PSUEDOCODE");
words.push_back("BIRYANI");
words.push_back("TROLLED");
srand((unsigned)time(NULL));
string TheWord = words[(rand() % words.size()) + 1];
string SoFar;
const int MAXTRIES = 8;
string used = "";
int tries, i;
i = tries = 0;
char guess;
for(i = 0; i <= TheWord.length(); i++)
SoFar[i] = '-';
while(tries <= MAXTRIES && SoFar != TheWord)
{
/****************************************************************/
/* I/0 */
/****************************************************************/
cout << "\nYou haz " << MAXTRIES - tries << " tries to go!\n" ;
cout << "You've used the following letters : ";
for(i = 0; i <= used.length(); i++)
cout << used[i] << " : " ;
cout << "\nSo far, the word is : " << SoFar << endl;
cout << "\nEnter your guess : " ;
cin >> guess;
/****************************************************************/
/* Processing input */
/****************************************************************/
if(used.find(guess) != string::npos)
continue;
guess = toupper(guess);
if(TheWord.find(guess) != string::npos)
{
for(i = 0; i <= TheWord.length(); i++)
{
if(guess == TheWord[i])
SoFar[i] = guess;
}
}
else
{
cout << "\nSorry, but the word doesn't have a letter like " << guess << " in it...\n";
tries++;
}
used += guess;
}
if(tries == MAXTRIES)
cout << "\nYep, you've been hanged...\n";
else if(SoFar == TheWord)
cout << "\nYou got it! Congratulations...\n(Now Im gonna add some psuedorandomly generated words just for you <3 :P)";
cout << "\nThe word was : " << TheWord;
return 0;
}
내가 무엇을 제안해야합니까? –
fill 생성자'string (size_t n, char c);'를 사용하십시오. –