2014-12-24 7 views
-3

제가 소유하고있는 우주 시뮬레이션 게임에서 행성에 대한 광란의 스타일 스토리를 생성하는 프로그램을 만들려고합니다. 사용자가 스토리 템플리트의 변수를 백분율 기호 안에 넣은 .txt 파일로 스토리 템플리트를 입력 할 수 있도록 설계되었습니다. 프로그램은 .txt 파일을 읽고 스토리를 가져 와서 변수를 찾은 다음 적절한 임의 정보 (현재 정의한 두 가지 변수 만 있음)로 변수를 채 웁니다.C++ String-> Replace가 예상대로 작동하지 않습니다.

내가 겪고있는 문제는 코드/출력에서 ​​볼 수 있듯이 디버깅하려는 모든 시도가 컴퓨터에서 적절한 텍스트를 생성한다고해도 스토리의 변수를 대체하지 않는다는 것입니다. 변수가 텍스트에 있음을 인식합니다.

문제의 실제 줄은 "GetVar()"함수 정의의 아래쪽에 있습니다.

전체 프로그램 (마이너스 컨트롤 초기화까지 일을 단축하기 위해) :

#pragma once 
namespace SpaceEngineStoryTeller { 
using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 
using namespace System::IO; 
/// <summary> 
/// Summary for Form1 
/// </summary> 
public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 
protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() { 
     delete components; 
    } 
private: 
    System::Windows::Forms::TabControl^ tabControl1; 
    System::Windows::Forms::TabPage^ tabPage1; 
    System::Windows::Forms::Label^ label4; 
    System::Windows::Forms::Label^ label3; 
    System::Windows::Forms::Label^ label2; 
    System::Windows::Forms::Label^ label1; 
    System::Windows::Forms::TabPage^ tabPage2; 
    System::Windows::Forms::Label^ label7; 
    System::Windows::Forms::Label^ label6; 
    System::Windows::Forms::Label^ label5; 
    System::Windows::Forms::ComboBox^ TemperatureBox; 
    System::Windows::Forms::Label^ label11; 
    System::Windows::Forms::TabPage^ tabPage3; 
    System::Windows::Forms::RichTextBox^ ExportBox; 
    System::Windows::Forms::Label^ label10; 
    System::Windows::Forms::PictureBox^ pictureBox1; 
    System::Windows::Forms::RichTextBox^ ImportBox; 
    System::Windows::Forms::Label^ label9; 
    System::Windows::Forms::RichTextBox^ StoryBox; 
    System::Windows::Forms::Label^ label8; 
    System::Windows::Forms::ComboBox^ LifeBox; 
    System::Windows::Forms::Label^ label13; 
    System::Windows::Forms::ComboBox^ TypeBox; 
    System::Windows::Forms::Label^ label12; 
    System::Windows::Forms::TextBox^ AtmosphereBox; 
    System::Windows::Forms::Label^ label15; 
    System::Windows::Forms::TextBox^ GravityBox; 
    System::Windows::Forms::Label^ label14; 
    System::Windows::Forms::Label^ label16; 
    System::Windows::Forms::Button^ button1; 
    System::Windows::Forms::CheckBox^ SubglacialCheck; 
    System::Windows::Forms::CheckBox^ TerrestrialCheck; 
    System::Windows::Forms::CheckBox^ MarineCheck; 
    System::Windows::Forms::CheckBox^ FloatersCheck; 
    System::Windows::Forms::PictureBox^ pictureBox3; 
    System::Windows::Forms::PictureBox^ pictureBox2; 
    /// <summary> 
    /// Required designer variable. 
    int StoriesCtr, SpaceRacesCtr, NonSpaceRacesCtr, varctr, RndHolder, ArrayLength, StoryLength; 
    Random^Rnd; 
    String^ path; 
    static array<String^>^ var=gcnew array<String^>(65535); 
    static array<String^>^ Stories=gcnew array<String^>(65535); 
    static array<String^>^ subStory=gcnew array<String^>(65535); 
    static array<String^>^ SpaceRaces=gcnew array<String^>(65535); 
    static array<String^>^ NonSpaceRaces=gcnew array<String^>(65535); 
    /// </summary> 
    System::ComponentModel::Container ^components; 
#pragma region Windows Form Designer generated code 
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
private: 
    void InitializeComponent(void) { 
     //Omitted Code 
    } 
#pragma endregion 
private: 
    System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { 
     StoriesCtr=0; 
     SpaceRacesCtr=0; 
     NonSpaceRacesCtr=0; 
     varctr=0; 
     ArrayLength=0; 
     Rnd=gcnew Random; 
     path="C:\\Users\\Ben\\Documents\\Visual Studio 2012\\Projects\\SpaceEngine Story Teller\\Resources\\Stories.txt"; 
     StreamReader^ StoryReader = gcnew StreamReader(path, true); 
     while(StoryReader->EndOfStream==false) { 
      Stories[StoriesCtr]=StoryReader->ReadLine(); 
      StoriesCtr++; 
     } 
     StoryReader->Close(); 
     path="C:\\Users\\Ben\\Documents\\Visual Studio 2012\\Projects\\SpaceEngine Story Teller\\Resources\\SpaceRaces.txt"; 
     StreamReader^ SpaceRacesReader = gcnew StreamReader(path, true); 
     while(SpaceRacesReader->EndOfStream==false) { 
      SpaceRaces[SpaceRacesCtr]=SpaceRacesReader->ReadLine(); 
      SpaceRacesCtr++; 
     } 
     SpaceRacesReader->Close(); 
     path="C:\\Users\\Ben\\Documents\\Visual Studio 2012\\Projects\\SpaceEngine Story Teller\\Resources\\NonSpaceRaces.txt"; 
     StreamReader^ NonSpaceRacesReader = gcnew StreamReader(path, true); 
     while(NonSpaceRacesReader->EndOfStream==false) { 
      NonSpaceRaces[NonSpaceRacesCtr]=NonSpaceRacesReader->ReadLine(); 
      NonSpaceRacesCtr++; 
     } 
     NonSpaceRacesReader->Close(); 
     for(int i=0; i<StoriesCtr; i++) { 
      StoryLength=Stories[i]->Length; 
      GetVar(Stories[i]); 
      this->StoryBox->Text+=Stories[i]+"\n"; 
     } 
    } 
    System::Void LifeBox_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { 
     if(this->LifeBox->SelectedIndex!=0) { 
      this->TerrestrialCheck->Enabled=true; 
      this->MarineCheck->Enabled=true; 
      this->SubglacialCheck->Enabled=true; 
      this->FloatersCheck->Enabled=true; 
      this->label16->Enabled=true; 
     } else { 
      this->TerrestrialCheck->Enabled=false; 
      this->MarineCheck->Enabled=false; 
      this->SubglacialCheck->Enabled=false; 
      this->FloatersCheck->Enabled=false; 
      this->label16->Enabled=false; 
     } 
    } 
    void GetVar(String^ str) {//Finds variables in story, and replaces them with random information 
     for(int a=0; a<str->Length; a++) 
      subStory[a]=str->Substring(a, 1); 
     for(int b=0; b<str->Length; b++) { 
      if(subStory[b]=="%") {//Beginning of variable 
       for(int c=(b+1); c<str->Length; c++) { 
        if(subStory[c]=="%") {//End of variable 
         for(int d=(b+1); d<c; d++)//Generates variable from substrings 
          var[varctr]+=subStory[d]; 
         for(int e=0; e<=varctr; e++) {//Replaces instances of variables in story with randomly generated terms. 
          if(var[e]=="SpaceRace") { 
           this->StoryBox->Text+="SpaceRace: "; 
           RndHolder=Rnd->Next()%GetArrayLength(SpaceRaces); 
           this->StoryBox->Text+=RndHolder+"\n"; 
           var[varctr]=SpaceRaces[RndHolder]; 
           this->StoryBox->Text+=var[varctr]+"\n"; 
           if(str->Contains("%SpaceRace%")) 
            this->StoryBox->Text+="Contains SpaceRace"+"\n"; 
           str->Replace("%SpaceRace%", var[varctr]);//Problem line 
          } else if(var[e]=="NonSpaceRace") { 
           this->StoryBox->Text+="NonSpaceRace: "; 
           RndHolder=Rnd->Next()%GetArrayLength(NonSpaceRaces); 
           this->StoryBox->Text+=RndHolder+"\n"; 
           var[varctr]=NonSpaceRaces[RndHolder]; 
           this->StoryBox->Text+=var[varctr]+"\n"; 
           if(str->Contains("%SpaceRace%")) 
            this->StoryBox->Text+="Contains NonSpaceRace"+"\n"; 
           str->Replace("%NonSpaceRace%", var[varctr]);//Problem line 
          } 
         } 
         varctr++; 
         break; 
        } 
       } 
      } 
     } 
    } 
    int GetArrayLength(static array<String^>^ Array) {//Returns how many indexes of array are non-null 
     for(int i=0; i<Array->Length; i++) { 
      if(Array[i]==nullptr) 
       return ArrayLength; 
      ArrayLength++; 
     } 
     return ArrayLength; 
    } 
}; 
} 

은 무엇 출력 생겼 :

SpaceRace: 9 
Chevin 
Contains SpaceRace 
NonSpaceRace: 536 
Vors‎ 
Contains NonSpaceRace 
%SpaceRace% is a space-faring species in Star Wars, and %NonSpaceRace% is a non-space-fairing species in Star Wars. 

어떻게 보일지 출력 :

SpaceRace: 9 
Chevin 
Contains SpaceRace 
NonSpaceRace: 536 
Vors‎ 
Contains NonSpaceRace 
Chevin is a space-faring species in Star Wars, and Vors is a non-space-fairing species in Star Wars. 

내용 Stories.txt (참조 용) :

%SpaceRace% is a space-faring species in Star Wars, and %NonSpaceRace% is a non-space-fairing species in Star Wars. 
+0

가능한 복제본 [ASP.net C# 바꾸기 문자열이 작동하지 않음] (http://stackoverflow.com/questions/3854465/asp-net-c-sharp-replace-string-not-working) – DocMax

+0

@DocMax 그게 전부입니다. C#,이 C++입니다. – TheMohawkNinja

+1

이 경우에는 중요하지만 둘 다 .NET 문자열입니다. – DocMax

답변

0

String.Replace은 바뀐 값으로 새 문자열을 반환하며 적절한 위치에서 대체하지 않습니다. 보다는 :

str->Replace("%SpaceRace%", var[varctr]); 

가 더 들어

str = str->Replace("%SpaceRace%", var[varctr]); 

필요 the MSDN entry for String.Replace를 참조하십시오.

+0

글쎄, 그건 문제를 철저히 해결하지 못했지만 도움이됩니다. 덕분에 지금은 더 발전하고 있습니다. – TheMohawkNinja