? asked in 電腦與網際網路程式設計 · 1 decade ago

我這個二維陣列印不出來 幫我的忙

#include<iostream.h>

#include<string.h>

int main()

{

char movie[5][100]={{\"The Davinci Code\"},{\"The Hill Have eyes\"},{\"Mission Impossible 3\"},{\"Silent Hill\"},{\"Ice age 2\"}};

char *p;

//movie[0]= \"The Davinci Code\";

//movie[1]= \"The Hill Have eyes\";

//movie[2]= \"Mission Impossible 3\";

//movie[3]= \"Silent Hill\";

//movie[4]= \"Ice age 2\";

int c=0;

p=movie[5][100];

for (int k=0; k<5; k++)

{

for(int i=0; i<strlen(movie[k]); i++)

{

if ( *p== \'c\'||*p==\'C\' )

{

cout<<\"c\"<<endl;

c=c++;

}

}

}

cout<<\"c總計出現有\"<<c<<\"次\"<<endl;

return 0;

}

4 Answers

Rating
  • 1 decade ago
    Favorite Answer

    char *p; // <--- 你要的是char,不是 char*,請改為下面的樣子

    char p;

    p=movie[5][100]; // <--- 這行沒有意義,請移除

    p = movie[k][i]; // 這是另外加上的

    if ( *p== 'c'||*p=='C' ) // 修改這行成為下面那行,並在這行上方加入上面那行

    if ( p== 'c'||p=='C' )

    最後應該像這樣:

    #include<iostream.h>

    #include<string.h>

    int main()

    {

    char movie[5][100]={"The Davinci Code","The Hill Have eyes","Mission Impossible 3","Silent Hill","Ice age 2"};

    char p;

    //movie[0]= "The Davinci Code";

    //movie[1]= "The Hill Have eyes";

    //movie[2]= "Mission Impossible 3";

    //movie[3]= "Silent Hill";

    //movie[4]= "Ice age 2";

    int c=0;

    p=movie[5][100];

    for (int k=0; k<5; k++)

    {

    for(int i=0; i<strlen(movie[k]); i++)

    {

    p = movie[k][i];

    if ( p== 'c'||p=='C' )

    {

    cout<<"c"<<endl;

    c=c++;

    }

    }

    }

    cout<<"c總計出現有"<<c<<"次"<<endl;

    return 0;

    }

  • Meteor
    Lv 4
    1 decade ago

    當我們宣告char movie[5][100];時系統並不會分配movie[5][100]這個值,最多只有到movie[4][99]

  • 1 decade ago

    二個都很厲害 不知道要選誰 所以交給投票了

  • ?
    Lv 5
    1 decade ago

    因為沒有提到說哪邊是規定不允許修改的,我假設char movie[5][100]和char *p的宣告不能改的話:

    int main()

    {

    char movie[5][100]={{"The Davinci Code"},{"The Hill Have eyes"},{"Mission Impossible 3"},{"Silent Hill"},{"Ice age 2"}};

    char *p;

    //movie[0]= "The Davinci Code";

    //movie[1]= "The Hill Have eyes";

    //movie[2]= "Mission Impossible 3";

    //movie[3]= "Silent Hill";

    //movie[4]= "Ice age 2";

    int c=0;

    p=(char *)movie;

    for (int k=0; k<5; k++)

    {

    for(int i=0; i<strlen(movie[k]); i++)

    {

    // if (*(*(movie+k)+i)=='c' || *(*(movie+k)+i)=='C') ←假設沒規定要用宣告的p直接用movie的話可以這樣寫

    if (*(p+k*100+i)=='c' || *(p+k*100+i)=='C')

    {

    cout<<"c"<<endl;

    c=c++;

    }

    }

    }

    cout<<"c總計出現有"<<c<<"次"<<endl;

    return 0;

    }

Still have questions? Get your answers by asking now.