2017-02-07 1 views
0

게임에서 크레이트 열기를 시뮬레이션하는 간단한 프로그램을 만들려고했습니다. 그러나, 그것을 실행하면 (1, 2 또는 3을 먼저 입력 한 다음 100과 같이 많은 수를 입력 함) 거의 모든 것을 올바르게 출력하지만 때로는 (1 ~ 2 회 100 회) 이상한 기호를 인쇄합니다 대신 문자열을 인쇄해야합니다. 왜 그런가요?문자열 대신 이상한 기호가 인쇄됩니다.

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
#include <windows.h> 

using namespace std; 
//This function is only here to make retrieving random numbers more pretty. 
int getrand(int min, int max); 
//This function will return what level of rarity the item is as a string. 
string getrare(); 
//This function will return the certification of the item as a string. 
string getcert(); 
//This function will return the crate being opened to a string, and will error check. 
string cratePrompt(); 
//This function will return the specific item received from a specific crate. 
string getitem(string crateNum); 
int rareCount=0; 
int veryrareCount=0; 
int importCount=0; 
int exoticCount=0; 
int blackmarketCount=0; 
int certCount=0; 


int main(){ 

    unsigned seed; 
    //This group of text here is to get a seed for randomness. 
    seed=time(0); 
    srand(seed); 

    //This is where the magic happens. 
    int count=0; 
    int crateQuantity; 
    string certTest; 
    string crateChosen; 
    crateChosen=(cratePrompt()); 
    cout<<"\nHow many crates do you want to open?\n\n"; 
    cin>>crateQuantity; 
    cout<<endl; 
    string itemActual; 
    while(count<crateQuantity){ 
     certTest=getcert(); 
     if (certTest!="Null"){ 
     cout<<certTest<<" "; 
     } 
     cout<<getitem(crateChosen); 
     cout<<endl; 
     count++; 
    } 
    cout<<"\n\n\nYou got "<<rareCount<<" Rare items!\n"; 
    cout<<"You got "<<veryrareCount<<" Very Rare items!\n"; 
    cout<<"You got "<<importCount<<" Import items!\n"; 
    cout<<"You got "<<exoticCount<<" Exotic items!\n"; 
    cout<<"You got "<<blackmarketCount<<" Black Market items!\n\n"; 
    cout<<certCount<<" of the items were certified!"; 
} 

int getrand(int min, int max){ 
    return ((rand()%(max-min+1))+min); 
} 
string getrare(){ 
int rando=(getrand(1,10000)); 
if ((rando>=1)&&(rando<=5472)){ 
    rareCount++; 
    return "Rare"; 
} 
else if ((rando>=5473)&&(rando<=8264)){ 
    veryrareCount++; 
    return "Very Rare"; 
} 
else if ((rando>=8265)&&(rando<=9476)){ 
    importCount++; 
    return "Import"; 
} 
else if ((rando>=9477)&&(rando<=9868)){ 
    exoticCount++; 
    return "Exotic"; 
} 
else if ((rando>=9869)&&(rando<=10000)){ 
    blackmarketCount++; 
    return "Black Market"; 
} 
else{ 
    return "An error has happened"; 
} 
} 
string getcert(){ 
    int iscert=getrand(1,100); 
    int whichcert=getrand(1,15); 
    if ((iscert>=16)&&(iscert<=100)){ 
    return "Null"; 
    } 
    else if ((iscert>=1)&&(iscert<=15)){ 
    certCount++; 
    if (whichcert==1){ 
     return "[Acrobat]"; 
    } 
    if (whichcert==2){ 
     return "[Aviator]"; 
    } 
    if (whichcert==3){ 
     return "[Goalkeeper]"; 
    } 
    if (whichcert==4){ 
     return "[Guardian]"; 
    } 
    if (whichcert==5){ 
     return "[Juggler]"; 
    } 
    if (whichcert==6){ 
     return "[Paragon]"; 
    } 
    if (whichcert==7){ 
     return "[Playmaker]"; 
    } 
    if (whichcert==8){ 
     return "[Scorer]"; 
    } 
    if (whichcert==9){ 
     return "[Show-Off]"; 
    } 
    if (whichcert==10){ 
     return "[Sniper]"; 
    } 
    if (whichcert==11){ 
     return "[Striker]"; 
    } 
    if (whichcert==12){ 
     return "[Sweeper]"; 
    } 
    if (whichcert==13){ 
     return "[Tactician]"; 
    } 
    if (whichcert==14){ 
     return "[Turtle]"; 
    } 
    if (whichcert==15){ 
     return "[Victor]"; 
    } 
    } 
    else{ 
    return "An error has happened"; 
    } 
} 
string cratePrompt(){ 
    string answer; 
    bool valid=0; 

    while (valid==0){ 
    cout<<"Which crate do you want to open?\n\n"; 
    cin>>answer; 
    if ((answer=="1")||(answer=="c1")||(answer=="cc1")||(answer=="C1")||(answer=="CC1")){ 
    return "CC1"; 
    } 
    else if ((answer=="2")||(answer=="c2")||(answer=="cc2")||(answer=="C2")||(answer=="CC2")){ 
    return "CC2"; 
    } 
    else if ((answer=="3")||(answer=="c3")||(answer=="cc3")||(answer=="C3")||(answer=="CC3")){ 
    return "CC3"; 
    } 
    else if ((answer=="4")||(answer=="c4")||(answer=="cc4")||(answer=="C4")||(answer=="CC4")){ 
    return "CC4"; 
    } 
    else{ 
    cout<<"Please enter a valid option (1, 2, 3, or 4)\n"; 
    } 
    } 
} 
string getitem(string crateNum){ 
    string rarity = getrare(); 
    //(Rarity)Select is used to select exactly which item they will get out of the possibilities. 
    int BMSelect= getrand(1,6); 
    int ESelect= getrand(1,2); 
    int ISelect= getrand(1,3); 
    int VRSelect= getrand(1,4); 
    int RSelect= getrand(1,5); 
    if (crateNum=="CC1"){ 
    if (rarity=="Rare"){ 
     if(RSelect==1){ 
     return "(Takumi) Combo"; 
     } 
     else if(RSelect==2){ 
     return "(Breakout) Vice"; 
     } 
     else if(RSelect==3){ 
     return "(Dominus) Pollo Caliente"; 
     } 
     else if(RSelect==4){ 
     return "(Dominus) Arcana"; 
     } 
     else if(RSelect==5){ 
     return "(Breakout) Shibuya"; 
     } 
    } 
    else if (rarity=="Very Rare"){ 
     if (VRSelect==1){ 
     return "(Takumi) Anubis"; 
     } 
     else if (VRSelect==2){ 
     return "(Breakout) Dot Matrix"; 
     } 
     else if (VRSelect==3){ 
     return "(Dominus) Snakeskin"; 
     } 
     else if (VRSelect==4){ 
     return "Chakrams"; 
     } 
    } 
    else if (rarity=="Import"){ 
     if (ISelect==1){ 
     return "Dominus GT"; 
     } 
     else if (ISelect==2){ 
     return "Trinity"; 
     } 
     else if (ISelect==3){ 
     return "Takumi RX-T"; 
     } 
    } 
    else if (rarity=="Exotic"){ 
     if (ESelect==1){ 
     return "Photons"; 
     } 
     else if (ESelect==1){ 
     return "Loopers"; 
     } 
    } 
    else if (rarity=="Black Market"){ 
     if (BMSelect==1){ 
     return "Biomass"; 
     } 
     else if (BMSelect==2){ 
     return "Heatwave"; 
     } 
     else if (BMSelect==3){ 
     return "Hexed"; 
     } 
     else if (BMSelect==4){ 
     return "Slipstream"; 
     } 
     else if (BMSelect==5){ 
     return "Parallax"; 
     } 
     else if (BMSelect==6){ 
     return "Labyrinth"; 
     } 
    } 
    else{ 
     cout<<"AN ERROR HAS OCCURED"; 
    } 
    } 
    if (crateNum=="CC2"){ 
    if (rarity=="Rare"){ 
     if(RSelect==1){ 
     return "(Octane) Dragon Lord"; 
     } 
     if(RSelect==2){ 
     return "(Venom) Nine Lives"; 
     } 
     if(RSelect==3){ 
     return "(Road Hog) Carbonated"; 
     } 
     if(RSelect==4){ 
     return "(Takumi) Whizzle"; 
     } 
     if(RSelect==5){ 
     return "(Merc) Narwhal"; 
     } 
    } 
    else if (rarity=="Very Rare"){ 
     if (VRSelect==1){ 
     return "(Octane) Distortion"; 
     } 
     if (VRSelect==2){ 
     return "(Merc) Warlock"; 
     } 
     if (VRSelect==3){ 
     return "Polygonal"; 
     } 
     if (VRSelect==4){ 
     return "(X-Devil) Snakeskin"; 
     } 
    } 
    else if (rarity=="Import"){ 
     if (ISelect==1){ 
     return "X-Devil Mk2"; 
     } 
     if (ISelect==2){ 
     return "Road Hog XL"; 
     } 
     if (ISelect==3){ 
     return "Pixel Fire"; 
     } 
    } 
    else if (rarity=="Exotic"){ 
     if (ESelect==1){ 
     return "Lightnings"; 
     } 
     if (ESelect==1){ 
     return "Lobos"; 
     } 
    } 
    else if (rarity=="Black Market"){ 
     if (BMSelect==1){ 
     return "Biomass"; 
     } 
     if (BMSelect==2){ 
     return "Heatwave"; 
     } 
     if (BMSelect==3){ 
     return "Hexed"; 
     } 
     if (BMSelect==4){ 
     return "Slipstream"; 
     } 
     if (BMSelect==5){ 
     return "Parallax"; 
     } 
     if (BMSelect==6){ 
     return "Labyrinth"; 
     } 
    } 
    else{ 
     cout<<"AN ERROR HAS OCCURED"; 
    } 
    } 
    if (crateNum=="CC3"){ 
    if (rarity=="Rare"){ 
     if(RSelect==1){ 
     return "(Breakout) Falchion"; 
     } 
     if(RSelect==2){ 
     return "(Breakout) Turbo"; 
     } 
     if(RSelect==3){ 
     return "(Dominus) Mondo"; 
     } 
     if(RSelect==4){ 
     return "(Octane) Shisa"; 
     } 
     if(RSelect==5){ 
     return "(Masamune) Oni"; 
     } 
    } 
    else if (rarity=="Very Rare"){ 
     if (VRSelect==1){ 
     return "(Takumi) Distortion"; 
     } 
     if (VRSelect==2){ 
     return "(Breakout) Snakeskin"; 
     } 
     if (VRSelect==3){ 
     return "Troikas"; 
     } 
     if (VRSelect==4){ 
     return "(Octane) MG-88"; 
     } 
    } 
    else if (rarity=="Import"){ 
     if (ISelect==1){ 
     return "Breakout Type-S"; 
     } 
     if (ISelect==2){ 
     return "Hypernova"; 
     } 
     if (ISelect==3){ 
     return "Dark Matter"; 
     } 
    } 
    else if (rarity=="Exotic"){ 
     if (ESelect==1){ 
     return "Pulsus"; 
     } 
     if (ESelect==1){ 
     return "Discotheques"; 
     } 
    } 
    else if (rarity=="Black Market"){ 
     if (BMSelect==1){ 
     return "Biomass"; 
     } 
     if (BMSelect==2){ 
     return "Heatwave"; 
     } 
     if (BMSelect==3){ 
     return "Hexed"; 
     } 
     if (BMSelect==4){ 
     return "Slipstream"; 
     } 
     if (BMSelect==5){ 
     return "Parallax"; 
     } 
     if (BMSelect==6){ 
     return "Labyrinth"; 
     } 
    } 
    else{ 
     cout<<"AN ERROR HAS OCCURED"; 
    } 
    } 
if (crateNum=="CC4"){ 
    if (rarity=="Rare"){ 
     if(RSelect==1){ 
     return "(Octane) Dragon Lord"; 
     } 
     if(RSelect==2){ 
     return "(Venom) Nine Lives"; 
     } 
     if(RSelect==3){ 
     return "(Road Hog) Carbonated"; 
     } 
     if(RSelect==4){ 
     return "(Takumi) Whizzle"; 
     } 
     if(RSelect==5){ 
     return "(Merc) Narwhal"; 
     } 
    } 
    else if (rarity=="Very Rare"){ 
     if (VRSelect==1){ 
     return "(Octane) Distortion"; 
     } 
     if (VRSelect==2){ 
     return "(Merc) Warlock"; 
     } 
     if (VRSelect==3){ 
     return "Polygonal"; 
     } 
     if (VRSelect==4){ 
     return "(X-Devil) Snakeskin"; 
     } 
    } 
    else if (rarity=="Import"){ 
     if (ISelect==1){ 
     return "X-Devil Mk2"; 
     } 
     if (ISelect==2){ 
     return "Road Hog XL"; 
     } 
     if (ISelect==3){ 
     return "Pixel Fire"; 
     } 
    } 
    else if (rarity=="Exotic"){ 
     if (ESelect==1){ 
     return "Lightnings"; 
     } 
     if (ESelect==1){ 
     return "Lobos"; 
     } 
    } 
    else if (rarity=="Black Market"){ 
     if (BMSelect==1){ 
     return "Biomass"; 
     } 
     if (BMSelect==2){ 
     return "Heatwave"; 
     } 
     if (BMSelect==3){ 
     return "Hexed"; 
     } 
     if (BMSelect==4){ 
     return "Slipstream"; 
     } 
     if (BMSelect==5){ 
     return "Parallax"; 
     } 
     if (BMSelect==6){ 
     return "Labyrinth"; 
     } 
    } 
    else{ 
     cout<<"AN ERROR HAS OCCURED"; 
    } 
    } 
} 
+1

어떤 기호가 있습니까? 디버거를 사용해 보셨습니까? 문제가 발생한 시드를 찾고 '시드'와 함께 시드를 사용하여 재현 할 수있는 오류를 얻을 수 있습니다. –

+0

더 짧은 예제를 만들어야합니다. – synchronizer

+0

시드 505를 사용하고 1과 100의 입력을 사용하면 아래쪽에서 몇 가지 결과를 볼 수 있습니다. – user2840634

답변

0

반환하지 않고 종료 할 수있는 문자열을 반환하는 함수가 있습니다. 결과적으로 C++이 주목할만한 "정의되지 않은 동작"이 발생합니다.

반환 값없이 함수 범위를 끝내면 C++에서 반환 값을 생성하지 않습니다. 위의 설명 에서처럼 경고를 추가하면이를 지적해야합니다. 다른 절에서

, 당신은 당신이

#include <assert.h> 

... 

string get_stuff() { 
    if (something) { 
     ... 
    } else if (something_els) { 
     ... 
    } else { 
     assert(!"Default case hit"); 
    } 
} 

이것은 대부분의 환경에서 디버거에서 프로그램을 중단 할 것을 확인하고이 포함 된 유용한 메시지를 인쇄해야

cout << "AN ERROR HAS OCCURRED"; 

의 변화를 기본 사례가 맞았던 행 번호

위의 조언과 마찬가지로, 디버거를 사용하여 돌아가서 함수의 끝에서 코드가 끝나기 때문에 직접 살펴볼 수도 있습니다.

관련 문제