具有相同索引的数组未经重新分配而更改

时间:2012-07-03 01:28:03

标签: c++ arrays loops

我遇到了这个程序的问题。它的目的是读取一个单词列表,然后输入单词的长度,从这个单词的长度,计算其中所有字母的总数,按最高频率对它们进行排序,并询问用户为频率最高的用户。在它要求之前,它会检查是否已经要求该字母循环,尽管数组是普遍存在的。我的问题是,如果我输入'yesletter'是真的,那么在内联注释“问题发生在这里”之后,该数组的内容会发生变化。我放入的测试循环中q的值不会改变,但值本身会改变。

我知道main中的循环现在是无限的,但程序还没有完成。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int i(0),n(0),counter(0),limit(0),length,mastercount(0);
int acount(0),bcount(0),ccount(0),dcount(0),ecount(0),fcount(0),gcount(0),hcount(0);
int icount(0),jcount(0),kcount(0),lcount(0),mcount(0),ncount(0),ocount(0),pcount(0);
int qcount(0),rcount(0),scount(0),tcount(0),ucount(0),vcount(0),wcount(0),xcount(0),ycount(0),zcount(0);
int letters[2][26];
char prevguess[26];
char words[60000][30];


void initarray() {

    int len(0);
    string line;
    char temp;

    ifstream wordlist ("words.txt");

    if (wordlist.is_open())
    {
        while (wordlist.good())
        {
            getline (wordlist,line);
            len=line.length();

            for (n=0;n<30;n++)
            {
                if (n<len){
                    temp=line.at(n);
                    words[i][n]=temp;
                }
                else{
                    words[i][n]='*';
                }
            }

            i++;
            counter++;
        }
    }
    else
    {
        cout<<"file not opened";
    }
    wordlist.close();
}

void selectlength()
{
    int x(0),y(0);
    bool shorter(false),longer(false);

    cout <<"length of word"<<endl;
    cin >> length;

    limit=counter;
    counter=0;

    for (i=0;i<limit;i++){

        shorter=false;
        longer=false;

        for (n=0;n<length;n++){
            if (words[i][n]=='*')
            {
                shorter=true;
                break;
            }
        }

        if (words[i][length] != '*')
        {
            longer=true;
        }

        if (!longer && !shorter)
        {
                n=0;
                for (y=0;y<30;y++)
                {
                    if (n<length){
                        words[x][y]=words[i][n];
                        n++;
                    }
                    else{
                        words[x][y]='*';
                    }
                }
                x++;
                counter++;

        }

    }
}

void mostletters(){
    char temp;

    for (i=0;i<counter;i++){
        for (n=0;n<=length;n++){

            temp=words[i][n];
            switch (temp){
                case 'a':
                    acount++;
                    break;
                case 'b':
                    bcount++;
                    break;
                case 'c':
                    ccount++;
                    break;
                case 'd':
                    dcount++;
                    break;
                case 'e':
                    ecount++;
                    break;
                case 'f':
                    fcount++;
                    break;
                case 'g':
                    gcount++;
                    break;
                case 'h':
                    hcount++;
                    break;
                case 'i':
                    icount++;
                    break;
                case 'j':
                    jcount++;
                    break;
                case 'k':
                    kcount++;
                    break;
                case 'l':
                    lcount++;
                    break;
                case 'm':
                    mcount++;
                    break;
                case 'n':
                    ncount++;
                    break;
                case 'o':
                    ocount++;
                    break;
                case 'p':
                    pcount++;
                    break;
                case 'q':
                    qcount++;
                    break;
                case 'r':
                    rcount++;
                    break;
                case 's':
                    scount++;
                    break;
                case 't':
                    tcount++;
                    break;
                case 'u':
                    ucount++;
                    break;
                case 'v':
                    vcount++;
                    break;
                case 'w':
                    wcount++;
                    break;
                case 'x':
                    xcount++;
                    break;
                case 'y':
                    ycount++;
                    break;
                case 'z':
                    zcount++;
                    break;

            }
        }
    }
}

void guessmost(){
    int x,y,temp,temp2,q;

    for (x=0;x<26;x++){

        letters[0][x]=x;

        switch (x){
            case 0:
                letters[1][x]=acount;
                break;
        case 1:
            letters[1][x]=bcount;
            break;
        case 2:
            letters[1][x]=ccount;
            break;
        case 3:
            letters[1][x]=dcount;
            break;
        case 4:
            letters[1][x]=ecount;
            break;
        case 5:
            letters[1][x]=fcount;
            break;
        case 6:
            letters[1][x]=gcount;
            break;
        case 7:
            letters[1][x]=hcount;
            break;
        case 8:
            letters[1][x]=icount;
            break;
        case 9:
            letters[1][x]=jcount;
            break;
        case 10:
            letters[1][x]=kcount;
            break;
        case 11:
            letters[1][x]=lcount;
            break;
        case 12:
            letters[1][x]=mcount;
            break;
        case 13:
            letters[1][x]=ncount;
            break;
        case 14:
            letters[1][x]=ocount;
            break;
        case 15:
            letters[1][x]=pcount;
            break;
        case 16:
            letters[1][x]=qcount;
            break;
        case 17:
            letters[1][x]=rcount;
            break;
        case 18:
            letters[1][x]=scount;
            break;
        case 19:
            letters[1][x]=tcount;
            break;
        case 20:
            letters[1][x]=ucount;
            break;
        case 21:
            letters[1][x]=vcount;
            break;
        case 22:
            letters[1][x]=wcount;
            break;
        case 23:
            letters[1][x]=xcount;
            break;
        case 24:
            letters[1][x]=ycount;
            break;
        case 25:
            letters[1][x]=zcount;
            break;
        }
    }


    for (y=0;y<26;y++){



        //problem occurs here (I think)

        for (q=mastercount-1;q>=0;q--){
                    cout<<"for array index:"<<q;
                    cout << " the value of prevguess is "<<prevguess[q]<<endl;
        }





        for (x=26;x>=1;x--){
            if (letters[1][x]>letters[1][x-1])
            {
                temp=letters[1][x-1];
                letters[1][x-1]=letters[1][x];
                letters[1][x]=temp;

                temp2=letters[0][x-1];
                letters[0][x-1]=letters[0][x];
                letters[0][x]=temp2;
            }
        }

    }
}

void letterguess(){
    int x(0),z;
    char guess;
    bool goodletter(false),yesletter(false),alreadyguess(false);

    while (!goodletter){

        guess=letters[0][x]+97;

        if (mastercount==0){
            alreadyguess=false;
        }
        else{
            for (z=mastercount-1;z>=0;z--){
                if (guess==prevguess[z]){
                    alreadyguess=true;
                }
            }
        }

        if (!alreadyguess){
            cout<<"is your letter "<< guess<<endl;
            cin >> yesletter;
            prevguess[mastercount]=guess;
        }

        if (yesletter && !alreadyguess){
            goodletter=true;
        }
        else {
            cout<<"wrong"<<endl;
            x++;
        }

        mastercount++;

        if (mastercount>26){
                    break;
                }
    }

}

int main() {
    bool found(false);
    initarray();
    selectlength();
    while (!found){
        mostletters();
        guessmost();
        letterguess();
        if (mastercount>26){
            break;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题的开始比你的评论要低一些。

for (x=26;x>=1;x--){
       if (letters[1][x]>letters[1][x-1])

x为26时,letters[1][x]将超出范围。