字符串比较if语句被忽略

时间:2013-12-24 19:43:08

标签: c++ string if-statement console iostream

我正在制作一款战略游戏,用于与星球大战的战斗前线2.这是一个完全不同的程序,所以你需要对游戏一无所知来帮助我。所有你需要知道的是iostream如何工作和字符串比较if语句。

`#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    char Era[]="n";
    int eranumber=0;
    char* content [4] [28];
    char action[]="n";
    char whichplanet[]="n";
    char facilitytype[]="n";

//Jedi civil war
content [0] [0] ="Republic";
content [0] [1] ="Reavens Sith Empire";
content [0] [2] ="rectuitment center";
content [0] [3] ="rectuitment center";

content [0] [4] ="Alderaan";
content [0] [5] ="Bespin";
content [0] [6] ="Coruscant";
content [0] [7] ="Dagobah";
content [0] [8] ="Concord Dawn";
content [0] [9] ="Felucia";
content [0] [10] ="Geonosis";
content [0] [11] ="Harrun Kal";
content [0] [12] ="Hoth";
content [0] [13] ="Tatooine";
content [0] [14] ="Kamino";
content [0] [15] ="Kashyyyk";
content [0] [16] ="Methalyn";
content [0] [17] ="Mustafar";
content [0] [18] ="Mygeeto";
content [0] [19] ="Naboo";
content [0] [20] ="Ord Ilbanna";
content [0] [21] ="Polis Massa";
content [0] [22] ="Rattatak";
content [0] [23] ="Raxus Prime";
content [0] [24] ="Rhen Var";
content [0] [25] ="Utapau";
content [0] [26] ="Yavin";
// Great galactic war
content [1] [0] ="Republic";
content [1] [1] ="Sith empire";
content [1] [2] ="rectuitment center";
content [1] [3] ="rectuitment center";

content [1] [0] ="Coruscant";
content [1] [1] ="Dagobah";
content [1] [2] ="Alderaan";
content [1] [3] ="Felucia";
content [1] [4] ="Tatooine";
content [1] [5] ="Kamino";
content [1] [6] ="Kashyyyk";
content [1] [7] ="Mustafar";
content [1] [8] ="Mygeeto";
content [1] [9] ="Naboo";
content [1] [10] ="Polis Massa";
content [1] [11] ="Utapau";
//clone wars
content [2] [0] ="Republic";
content [2] [1] ="Sepratists";
content [2] [2] ="Cloning Facility";
content [2] [3] ="Driod Factory";

content [2] [4] ="Alderaan";
content [2] [5] ="Bespin";
content [2] [6] ="Coruscant";
content [2] [7] ="Dagobah";
content [2] [8] ="Felucia";
content [2] [9] ="Geonosis";
content [2] [10] ="Harrun Kal";
content [2] [11] ="Hoth";
content [2] [12] ="Tatooine";
content [2] [13] ="Kamino";
content [2] [14] ="Kashyyyk";
content [2] [15] ="Methalyn";
content [2] [16] ="Mustafar";
content [2] [17] ="Mygeeto";
content [2] [18] ="Naboo";
content [2] [19] ="Ord Ilbanna";
content [2] [20] ="Polis Massa";
content [2] [21] ="Rattatak";
content [2] [22] ="Raxus Prime";
content [2] [23] ="Rhen Var";
content [2] [24] ="Utapau";
content [2] [25] ="Yavin";
//Galactic civil war
content [3] [0] ="Rebel Alliance";
content [3] [1] ="Galactic Empire";
content [3] [2] ="rectuitment center";
content [3] [3] ="rectuitment center";

content [3] [4] ="Alderaan";
content [3] [5] ="Bespin";
content [3] [6] ="Coruscant";
content [3] [7] ="Dagobah";
content [3] [8] ="Concord Dawn";
content [3] [9] ="Felucia";
content [3] [10] ="Geonosis";
content [3] [11] ="Harrun Kal";
content [3] [12] ="Hoth";
content [3] [13] ="Tatooine";
content [3] [14] ="Kamino";
content [3] [15] ="Kashyyyk";
content [3] [16] ="Methalyn";
content [3] [17] ="Mustafar";
content [3] [18] ="Mygeeto";
content [3] [19] ="Naboo";
content [3] [20] ="Ord Ilbanna";
content [3] [21] ="Polis Massa";
content [3] [22] ="Rattatak";
content [3] [23] ="Raxus Prime";
content [3] [24] ="Rhen Var";
content [3] [25] ="Utapau";
content [3] [26] ="Yavin";
content [3] [27] ="Endor";

    cout <<"Select era (1.kotor,2.swotor,3.cw,4.gcw):";
    cin >> Era;
    if (strcmp(Era,"kotor"))
    {
        eranumber=0;
    }

    if (strcmp(Era, "swotor"))
    {
        eranumber=1;
    }

    if (strcmp(Era,"cw"))
    {
        eranumber=2;
    }

    if (strcmp(Era, "gcw"))
    {
        eranumber=3;
    }

    cout <<content [eranumber] [0]<< ", choose an action (1.Create facility, 2.Attack, 3.Create fleet)";
    cin >> action;
    if (strcmp(action,"create facility"))
    {
    cout<< "What type of facility(1.Mining site,2.)";
    cin >> facilitytype;
    if (strcmp(facilitytype,"miningsite"))
    {

    }
    }
    if (strcmp(action,"attack"))
    {
    cout <<"1."<<content [eranumber] [4]<<endl;
    cout <<"2."<<content [eranumber] [5]<<endl;
    cout <<"3."<<content [eranumber] [6]<<endl;
    cout <<"4."<<content [eranumber] [7]<<endl;
    cout <<"5."<<content [eranumber] [8]<<endl;
    cout <<"6."<<content [eranumber] [9]<<endl;
    cout <<"7."<<content [eranumber] [10]<<endl;
    cout <<"8."<<content [eranumber] [11]<<endl;
    cout <<"9."<<content [eranumber] [12]<<endl;
    cout <<"10."<<content [eranumber] [13]<<endl;
    cout <<"11."<<content [eranumber] [14]<<endl;
    cout <<"12."<<content [eranumber] [15]<<endl;
    cout <<"13."<<content [eranumber] [16]<<endl;
    cout <<"14."<<content [eranumber] [17]<<endl;
    cout <<"15."<<content [eranumber] [18]<<endl;
    cout <<"16."<<content [eranumber] [19]<<endl;
    cout <<"17."<<content [eranumber] [20]<<endl;
    cout <<"18."<<content [eranumber] [21]<<endl;
    cout <<"19."<<content [eranumber] [22]<<endl;
    cout <<"20."<<content [eranumber] [23]<<endl;
    cout <<"21."<<content [eranumber] [24]<<endl;
    cout <<"22."<<content [eranumber] [25]<<endl;
    cout <<"23."<<content [eranumber] [26]<<endl;
    cout <<"24."<<content [eranumber] [27]<<endl;

    cout<< "Select a planet to attack:";
    cin >> whichplanet;
    }
    return 0;
}`

1 个答案:

答案 0 :(得分:0)

strcmp会返回int类型的值而不是boolean类型。

如果strcmp两者相同,则strings会返回 0 ,否则会返回+ve-ve个数字,具体取决于两个strings

所以你需要写:

if (strcmp(Era,"kotor")==0)
{
    eranumber=0;
}

if (strcmp(Era, "swotor")==0)
{
    eranumber=1;
}

if (strcmp(Era,"cw")==0)
{
    eranumber=2;
}

if (strcmp(Era, "gcw")==0)
{
    eranumber=3;
}

另外请记住strcmp区分大小写(即“A”根据此不等于“a”)。 如果您想忽略这种情况,请改用strcmpi

注意:strcmp

中定义了strcmpi<string.h>两件事