By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,345 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

Need help finding problem in program

P: 10
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<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>

void main ()
{
char cstr[6][30],ch;
int id[6],i,inum,quant,npur=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(cstr[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("project6.dat","w");

fprintf(fo,"item# 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_cost[inum];
fprintf(fo,format1,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_total[i];
}
fprintf(fo,format2,sum);
fclose(fo);
}
Nov 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: 10
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
Nov 14 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.