-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?应该是int。(注意fgetc返回int)。如果是,char则表达式ch != EOF可能无法按您的预期工作。

  2. 如果输入超过 16 个字符,我可以很好地看到counter被重置。这取决于您的编译器生成的确切代码,但是如果您将某些内容存储在word[16](第 17 个字节)并且counter紧跟在堆栈上word,您将开始将字符写入包含counter.

于 2013-02-08T20:57:45.983 回答