473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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<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
1 1780
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

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

Similar topics

2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
2
by: svbala | last post by:
hi, we have written a program in VC++ using arrays .The program is just basic C,but since the array size is too big we are unable to run the program so it is a must to use pointers .We also tried...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.