Need help finding problem in program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swtstrawberry
    New Member
    • Oct 2006
    • 10

    #1

    Need help finding problem in program

    This program is supposed to present the buyer with an inventory list and ask if they would like to make a purchase, then enter the item number for purchase, then the amount of that item they would like to purchase. Then it askes whether they would like to make another purchase and if they do the it repeats. When they are finished buying it is supposed to print a bill with all the items that have been purchased, each with its own cost plus the total. But when I run the program at the end of my purchase on the bill it only gives me the last item purchased, the price of the last item purchase, and the total of all items purchased. It doesn't print all items purchased. Can anyone tell me why?

    #include<iostre am.h>
    #include<fstrea m.h>
    #include<stdio. h>
    #include<stdlib .h>

    void main ()
    {
    char cstr[6][30],ch;
    int id[6],i,inum,quant,n pur=0;
    double unit_cost[6],sub_total[10],sum;

    char format1[]="%3i%30s%10.2f ";
    char format2[]="\t\t\t\t\t\t\ t\t\t\t\t\t\t\t Total cost:%10.2f\n";

    ifstream inf;
    inf.open("strm. txt",ios::in);

    for(i=1; i<=5; i++)
    {
    inf>>id[i];
    inf.getline(cst r[i],30);
    inf>>unit_cost[i];
    }

    inf.close();

    printf("The following items are available for purchase\n\n");
    printf(" item# #item item cost/item \n");
    printf(" available \n\n\n");

    for(i=1; i<=5; i++)
    {
    printf("%3i%30s %8.2f\n\n",id[i],cstr[i],unit_cost[i]);
    }

    printf("The above items are avaible for purchase\n\n");
    printf("Do you wish to make a purchase?\n");
    printf("Enter Y for yes and N for no.\n");

    cin>>ch;
    L1:;
    if(ch=='N') exit(-1);
    if(ch!='Y')
    {
    printf("you did not enter a 'y' or 'n' \n");
    printf("Please restart program \n");
    exit(-1);
    }

    FILE*fo;
    fo=fopen("proje ct6.dat","w");

    fprintf(fo,"ite m# item description unit cost");
    fprintf(fo," quantity sub-total \n");
    fprintf(fo,"--------------------------------------");
    fprintf(fo,"---------------------------- \n");

    while(ch=='y');
    {
    printf("enter item number to purchase or 00 to quit\n");
    cin>>inum;
    if(inum>5 || inum<0)
    {
    printf("An incorrect number has been entered\n");
    printf("Please restart the program");
    exit(-1);
    }
    else
    {
    npur=npur+1;
    }

    printf("enter the number of items to be purchased\n");
    cin>>quant;
    sub_total[npur]=quant*unit_cos t[inum];
    fprintf(fo,form at1,inum,cstr[inum],unit_cost[inum]);
    fprintf(fo," %3i %10.2f\n",quant ,sub_total[npur]);

    printf("\n");

    printf("do you wish to make another purchase?\n");
    printf("enter y for yes or n for no\n");
    cin>>ch;goto L1;
    }

    fprintf(fo,"=== =============== =============== ============");
    fprintf(fo,"=== =============== =============== ========\n");
    sum=0.0;
    for(i=1;i<=npur ;i++)
    {
    sum=sum+sub_tot al[i];
    }
    fprintf(fo,form at2,sum);
    fclose(fo);
    }
  • swtstrawberry
    New Member
    • Oct 2006
    • 10

    #2
    here is an example of what should print out on the bill:

    item#-- description of item-- quantity-- unitcost
    =============== =============== =============== =============
    2 -- hammer-- 1-- 5.00
    1-- drill-- 1-- 15.00
    =============== =============== =============== =============
    sub total 20.00


    It shows the item number from the inventory list, a description of the item, the quantity, and cost. And at the end of the bill there is the total. However, when I run the program and make more than one purchase the bill prints out only the last purchase made and the total. Looks like this:

    item#-- description of item-- quantity-- unitcost
    =============== =============== =============== =============
    1-- drill-- 1-- 15.00
    =============== =============== =============== =============
    sub total 20.00

    Comment

    Working...