c - 程序输出不稳定?

标签 c windows dev-c++

这是一个收银程序。请先查看屏幕截图。

<小时/>

我认为循环是问题的根源。更有可能来自带有'j'和'k'的行。用户输入商品代码(i),例如2,根据文本文件搜索价格[2]=2.49,将价格保存到另一个数组ipprice[j]中,如果是输入的第一个项目代码是 ipprice[0],所以稍后我可以打印购买的第一个项目是 ipprice[0],第二个等等,这些 ipprice[j] 预计保持不变,但事实并非如此,请检查截屏。 system cls 使代码看起来很困惑,重复打印基本上只是试图在屏幕上保留一些内容,同时清除不需要的部分。使用 k 是因为 j 涉及常量 j++,输入的每个项目代码都会递增,因此程序知道这是购买的第一个项目,第二个项目依此类推。

完整代码

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


int main()
{
    int i,j=0,code,amt, key,lines=0;
    int id[i],stock[i],k=0;
    char name[i][20],product[100];
    float price[i],sum;
    float total=0;
    char ipname[j][20],ch;
    int quantity[j];
    float ipprice[j];
    float ipsub[j];

    FILE*fp1;

    fp1=fopen("Fruit.txt","r");
    if(fp1==NULL)
    {
        printf("ERROR in opening file\n");
        return 1;
    }
    else
    {
        while((ch=getc(fp1))!=EOF)
        {
            if(ch=='\n')
                lines++;
        }
    }
    fclose(fp1);

    fp1=fopen("Fruit.txt","r");
    if(fp1==NULL){
        printf("ERROR in opening file\n");
        return 1;
    }
    else
    {

        for(i=0;i<lines;i++){
            fgets(product,sizeof(product),fp1);
            id[i]=atoi(strtok(product,","));
            strcpy(name[i],strtok(NULL,","));
            price[i]=atof(strtok(NULL,","));
            stock[i]=atoi(strtok(NULL,"\n"));

        }
    }
    fclose(fp1);

    printf("=============================================================\n");
    for(i=0;i<lines;i++)
    {
        printf("%d:%-10s\t",i+1,name[i]);
    }
    printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
    printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");

    do{
        k=0;
        if(j>0)
        {
            system("cls");
        }
        if(j>0)
        {
            printf("=============================================================\n");
            for(i=0;i<lines;i++)
            {
                printf("%d:%-10s\t",i+1,name[i]);
            }
            printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
            printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
            for(k=0;k<j;k++)
            {
                printf("%-5d%-20s%-15.2f%-10d%.2f\n",
                       k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
            }
        }
        sum=0;
        amt=0;
        printf("\nProduct code:");
        scanf("%d",&code);
        i=code-1;
        printf("Quantity:");
        scanf("%d",&amt);
        system("cls");
        sum=price[i]*amt;
        total=total+sum;
        ipprice[j]=price[i];
        strcpy(ipname[j],name[i]);
        quantity[j]=amt;
        ipsub[j]=sum;
        printf("=============================================================\n");
        for(i=0;i<lines;i++){
            printf("%d:%-10s\t",i+1,name[i]);
        }
        printf("\n\n%-5s%-20s%-15s%-10s%s\n","No."
               ,"Product","Price","Quantity","Subtotal");
        printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
        for(k=0;k<=j;k++)
        {
            printf("%-5d%-20s%-15.2f%-10d%.2f\n",
                   k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
        }
        printf("%d",j);
        printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");
        key=_getch();
        if(key!=27)
        {
            j++;
        }
    }while(key!=27);
    printf("%.2f",total);

    return 0;
}

文本文件内容

18156,Apple,1.49,73
45776,Aprikot,1.59,23
73191,Avocado,2.49,63
72829,Banana,2.99,27
74084,Blueberry,5.49,36
79800,Coconut,3.49,80
16611,Grape,8.99,42
62690,Grapefruit,2.19,34
47089,Guava,4.99,42
70412,Jackfruit,19.99,29
44640,Kiwifruit,2.15,38
94768,Lemon,0.99,42
86240,Mango,3.99,62
78697,Orange,1.69,23
74470,Papaya,4.49,23
23959,Pear,2.49,36
78862,Pineapple,2.99,71
82943,Pomegranate,4.35,19
56180,Pomelo,12.99,72
67712,Starfruit,2.79,59
33974,Strawberry,13.99,31
12354,dildo,29.99,4
45584,Watermelon,6.88,21

Screenshot of error

循环

do{
    k=0;
    if(j>0)//because this is only necessary if it is second loop
    {
        system("cls");
    }
    if(j>0)//same reason, j is later used to display and save item purchased
    {
        printf("=============================================================\n");
        for(i=0;i<lines;i++)
        {
            printf("%d:%-10s\t",i+1,name[i]);
        }
        printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
        printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
        for(k=0;k<j;k++)//i am trying to keep these content on screen while system cls
        {
            printf("%-5d%-20s%-15.2f%-10d%.2f\n",
                   k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
        }
    }
    sum=0;
    amt=0;
    printf("\nProduct code:");
    scanf("%d",&code);
    i=code-1;
    printf("Quantity:");
    scanf("%d",&amt);//amt amount
    system("cls");
    sum=price[i]*amt;
    total=total+sum;
    ipprice[j]=price[i];  //ip is itempurchased
    strcpy(ipname[j],name[i]);
    quantity[j]=amt;
    ipsub[j]=sum;//subtotal
    printf("=============================================================\n");
    for(i=0;i<lines;i++){
        printf("%d:%-10s\t",i+1,name[i]);
    }
    printf("\n\n%-5s%-20s%-15s%-10s%s\n",
           "No.","Product","Price","Quantity","Subtotal");
    printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
    for(k=0;k<=j;k++)
    {
        printf("%-5d%-20s%-15.2f%-10d%.2f\n",
               k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
    }
    printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");
    key=_getch();
    if(key!=27)
    {
        j++;
    }
} while(key!=27);

最佳答案

有效的新代码:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


int main()
{
int i,j=0,code,amt, key,lines=0;
int id[100],stock[100],k=0;
char name[100][20],product[100];
float price[100],sum;
float total=0;  
char ipname[100][20];
int quantity[100], ch;
float ipprice[100];
float ipsub[100];

FILE*fp1;

fp1=fopen("Fruit.txt","r");
if(fp1==NULL)
{
    printf("ERROR in opening file\n");
    return 1;
}
else
{
while((ch=getc(fp1))!=EOF)
    {
        if(ch=='\n')
        lines++;
    }
}
    fclose(fp1);

fp1=fopen("Fruit.txt","r");
if(fp1==NULL){
               printf("ERROR in opening file\n");
               return 1;
               }
               else
               {

               for(i=0;i<lines;i++){
                                    fgets(product,sizeof(product),fp1);
                              id[i]=atoi(strtok(product,","));
                             strcpy(name[i],strtok(NULL,","));
                             price[i]=atof(strtok(NULL,","));
                             stock[i]=atoi(strtok(NULL,"\n"));

                         }
                                }
                                    fclose(fp1);

printf("=============================================================\n");
for(i=0;i<lines;i++)
{
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");

do{  
k=0;
if(j>0)
{
system("cls");
}
if(j>0)
{
    printf("=============================================================\n");
    for(i=0;i<lines;i++)
{
printf("%d:%-10s\t",i+1,name[i]);
}
printf("\n\n%-5s%-20s%-15s%-10s%s\n","No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
    for(k=0;k<j;k++)
    {
printf("%-5d%-20s%-15.2f%-10d%.2f\n",k+1,
ipname[k],ipprice[k],quantity[k],ipsub[k]);
    }
}
sum=0;
amt=0;
printf("\nProduct code:");
scanf("%d",&code);
i=code-1;
printf("Quantity:");
scanf("%d",&amt);
system("cls");
sum=price[i]*amt;
total=total+sum;
ipprice[j]=price[i];
strcpy(ipname[j],name[i]);
quantity[j]=amt;
ipsub[j]=sum;
printf("=============================================================\n");
for(i=0;i<lines;i++){
printf("%d:%-10s\t",i+1,name[i]);
}
    printf("\n\n%-5s%-20s%-15s%-10s%s\n",
"No.","Product","Price","Quantity","Subtotal");
printf("%-5s%-20s%-15s%-10s%s\n","===","=======","=====","========","========");
    for(k=0;k<=j;k++)
    {
        printf("%-5d%-20s%-15.2f%-10d%.2f\n",
k+1,ipname[k],ipprice[k],quantity[k],ipsub[k]);
    }
        printf("%d",j);
printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");
scanf(" %c", &key);
if(key!=27)
{
        j++;
    }
}while(key!=27);
        printf("%d",j);

                                    printf("\n%.2f\n",total);



return 0;
}

================================================== ====

它应该是这样的: http://i30.photobucket.com/albums/c323/dumfuq/Screenshot37_zps81596e1a.png

最后一个数字“11”只是我试图监控 j++ 是否正常运行。

关于c - 程序输出不稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25468629/

相关文章:

c++ - 调用 GetWindowRect 时出现编译器错误

C - 按钮按下一次,注册两次 - MSP430

c - 我们应该在 C 中使用 exit() 吗?

windows - 如何在 Windows 上使用 wxPython 加载 BMP 文件?

c++ - Qt - QTcpserver 工作不正常

c++ - 如何清除控制台

c - 如何让 Dev-C++ 从 C 代码生成英特尔程序集?

c - 二进制翻译

python ctypes - 包装空指针

mysql - 如果 "mysqldump"和 "mysql"命令成功与否,如何检查批处理脚本?