-2

私のカウンター変数に注意してください。2 番目の while ループの上で 0 に設定する方法に注目してください。何らかの理由で、私の printf(counter) ステートメントは、カウンターがゼロにリセットされないことを示しています。ファイルの最後まで ++ing を続けるだけです。これは、このプログラムの私のロジックを完全に台無しにします。何か助けはありますか?

ch=fgetc(fp);
while(ch != EOF)
{
    counter = 0;
    while(ch != '\n' && ch!=EOF)
    {
        char word[32] = "";

        // keeps track of the current run thru of the
        //  loop so we know what input we're looking at.
        counter = counter+1;
        while(ch != ' ' && ch!='\n' && ch!=EOF)
        {
            // the following block builds up a character
            //  array from the current "word" (separated
            //  by spaces) in the input file.
            int len = strlen(word);
            word[len] = ch;
            word[len+1] = '\0';
            ch = fgetc(fp);
        }

        // the following if-else block sets the variables
        //  TextA, TextB, and TextC to the appropriate Supply Types.
        //  this part may be confusing to read mentally, but not to
        //  trace; all it does is logically set TextA, B, and C.
        if(counter==1)
        {
            if(strlen(TextA)==0)
            {
                strcpy(TextA,word);
            }
            else if(strlen(TextB)==0 && strcmp(word,TextA)!=0 && strcmp(word,TextC)!=0)
            {
                strcpy(TextB,word);
            }
            else if(strlen(TextC)==0 && strcmp(word,TextA)!=0 && strcmp(word,TextB)!=0)
            {
                strcpy(TextC,word);
            }
        }

        printf("TextA:  %s, TextB:  %s, TextC:  %s  word:   %s \n",TextA,TextB,TextC,word);
        printf("i equals:  %d",counter);

        switch(counter)
        {
            case 1:
                printf("Got in case 1.");
                if(strcmp(TextA,word)==0)
                {
                    SubTypeOption = 1;
                }
                else if(strcmp(TextB,word)==0)
                {
                    SubTypeOption = 2;
                }
                else if(strcmp(TextC,word)==0)
                {
                    SubTypeOption = 3;
                }
                break;

            case 2:
                // We actually ultimately don't need to keep track of
                // the product's name, so we do nothing for case i=2.
                // Included for readibility.
                break;

            case 3:
                WholesalePrice = atof(word);
                break;

            case 4:
                WholesaleAmount = atoi(word);
                break;

            case 5:
                RetailPrice = atof(word);
                break;

            case 6:
                RetailAmount = atoi(word);
                break;
        } //End switch(counter)

        if(ch='\n')
            counter = 0;

        ch = fgetc(fp);

    }//End while(ch != '\n')

    //The following if-else block "tallys up" the total amounts of SubTypes bought and sold by the owner.

    if(SubTypeOption == 1)
    {
        SubType1OwnersCost = SubType1OwnersCost + (WholesalePrice*(float)WholesaleAmount);
        SubType1ConsumersCost = SubType1ConsumersCost + (RetailPrice *(float)RetailAmount);
    }
    else if(SubTypeOption == 2)
    {
        SubType2OwnersCost = SubType2OwnersCost + (WholesalePrice*(float)WholesaleAmount);
        SubType2ConsumersCost = SubType2ConsumersCost + (RetailPrice *(float)RetailAmount);
    }
    else if(SubTypeOption == 3)
    {
        SubType3OwnersCost = SubType3OwnersCost + (WholesalePrice*(float)WholesaleAmount);
        SubType3ConsumersCost = SubType3ConsumersCost + (RetailPrice *(float)RetailAmount);
    }

}//End while((ch = fgetc(fp))!= EOF)
4

1 に答える 1

3

いくつかの考え:

  1. の型はch何ですか? である必要がありますintfgetc( を返すことに注意してくださいint)。そうである場合、期待どおりにchar式が機能しない可能性があります。ch != EOF

  2. 入力が 16 文字を超える場合、counterリセットされていることがよくわかります。コンパイラが生成した正確なコードに依存しますが、何かをword[16](17 番目のバイト) に格納counterし、 の直後のスタックにwordある場合、 を含むメモリに文字を書き始めますcounter

于 2013-02-08T20:57:45.983 に答える