2014-02-18 2 views
0

저는 Cpp에서 행맨 게임을했습니다 ... SoFar이라는 변수를 작성하여 시작 부분에 대시를 저장하지만 점차 밝혀졌습니다. 내가 SOFAR 인쇄 할 때문자열이 제대로 인쇄/초기화되지 않았습니다

for(i = 0; i <= TheWord.length(); i++) 
      SoFar[i] = '-'; 

그래서, 나는 시작의 대시를 포함 할 SoFar를 초기화 (시도), 그리고 나중에

TheWord 같은 길이, 그냥 비어!

cout << "\nSo far, the word is : " << SoFar << endl; 

Picture showing output

일체의 조언에 감사드립니다. 여기에 내 전체 프로그램은 참조 용입니다 : 비어

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; 
} 

답변

2

당신은 SoFar과 같이 정의 SoFar[i] = '-';을 실행하면 정의되지 않은 동작이 발생합니다.

시도 :이 이미 대시의 오른쪽 번호를 포함 SoFar 정의

std::string SoFar(TheWord.length(), '-'); 

. 당신이 SOFAR가 문자열로 이렇게 이중 사용하려고한다는 지정한

TROLLED 
------- 
2

. 그래서 모든, 당신이 그것에 쓸 때

for(i = 0; i <= TheWord.length(); i++) 
     SoFar[i] = '-'; 

SoFar은 여전히 ​​0의 길이 :

string SoFar;   

... 당신이 그것에 작성하려고 :

+0

내가 무엇을 제안해야합니까? –

+0

fill 생성자'string (size_t n, char c);'를 사용하십시오. –

1

:

#include <string> 
#include <iostream> 

int main(){ 
    std::string TheWord{"TROLLED"}; 

    std::string SoFar(TheWord.length(), '-'); 

    std::cout << TheWord << "\n"; 
    std::cout << SoFar << "\n"; 
} 

적어도 나를 위해,이 결과의 정확한 길이를 가져 오는 것 같습니다 : 여기

빠른 데모입니다 가치에 대한 견적

for(i = 0; i <= TheWord.length(); i++) 
SoFar[i] = "-"; 
관련 문제