Home › Forums › C Programming › Assistance with Question
- This topic has 4 replies, 2 voices, and was last updated 14 years, 10 months ago by BeauLGOffsdwntc.
- AuthorPosts
- February 9, 2010 at 1:56 pm #2238BeauLGOffsdwntcParticipant
I would like some assistance please. I need to complete the following question but I can’t get the program running. it is ending after I ask what the input like is.
The Question:
The quality control team of a sweets factory wants to check if the packets of lollies contain the amount of lollies
that they are supposed to contain according to the standards. There are small packets that should contain 50
lollies and large packets that should contain 100 lollies. Every morning a random number of packets are selected,
opened and the lollies counted. A file is used to keep record of the number of lollies in each of the randomly
selected packets together with an indicator to say whether it is a small or large packet. A different number of
packets is selected every day, so that the total records per file is not known. The letter ‘S’ is used for small
packets and the letter ‘L’ for large packets. Each line in the file represents the size of one packet and the number
of lollies inside that packet.
Use variables of type int to read in the number of lollies, and to calculate all totals, and variables of type
double to calculate averages. Write a program that will compute and output the following information:
– Count the number of small packets and the number of large packets
– Count the number of packets (small and large separately) that contains the required amount of lollies
according to the standards
– Compute the average number of lollies per small and per large packet
It is a requirement that the random selection of packets includes small and large packets. The program should
assert an error if that is not the case. A value of 0 may also not be captured for the number of lollies – assert an
error if this is the case. Prompt the user for the name of the file that contains the data to be processed. Write the
output to a file control.txt.My Coding:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143#include <iostream><br />#include <fstream><br />#include <cstdlib><br />#include <string><br /><br /><br />using namespace std;<br /><br />void checkReport(ifstream& infile)<br />{<br />cout<<endl<< "Displaying contents of control.txt : "<<endl<<endl;<br /><br />while (infile)<br />{<br />string temp;<br />getline (infile,temp, 'n');<br />cout<< temp<<endl;<br />}<br />}<br /><br />void calcAverages (ifstream& infile, ofstream& outfile)<br /><br />{<br />ifstream inout;<br />string outname = "control.txt";<br /><br />int results = 99;<br />char type;<br />int scount = 0;<br />int lcount = 0;<br />int stotal = 0;<br />int ltotal = 0;<br />int s50 = 0;<br />int l100 = 0;<br />double avgs = 0;<br />double avgl = 0;<br /><br />infile >>type>> results;<br />while(infile)<br /><br />{<br />if (type == 'S')<br />{<br />scount ++;<br />stotal += results;<br /><br />if (results == '50')<br />{<br />s50 ++;<br />}<br /><br />}<br />else<br />{<br />lcount ++;<br />ltotal += results;<br /><br />// if (results =='100'<br /><br />// {<br /><br />// l100 ++;<br /><br />// }<br /><br />}<br /><br /><br /><br />infile>>type >> results;<br />}<br /><br />assert (scount !=0 && lcount !=0);<br /><br />avgs = (stotal/scount);<br />avgl = (ltotal/lcount);<br /><br /><br /><br />outfile <<"Number of small packets : " << (scount)<<endl;<br />outfile <<"Number of large packets : " << (lcount)<<endl;<br />outfile <<"Number of small packets containing 50 lollies : " << s50 <<endl;<br />outfile <<"Number of large packets containing 100 lollies : " << l100 <<endl;<br />outfile <<"Average number of lollies per small packet : " << avgs <<endl;<br />outfile <<"Average number of lollies per Large packet : " << avgl <<endl;<br /><br />}<br /><br />int main()<br /><br />{<br /><br />ifstream infile;<br />ifstream inout;<br />ofstream outfile;<br />string outName = "control.txt";<br />string inName = "inputdata.dat";<br /><br />cout<< "Enter the Input File name"<<endl;<br />cin>> inName;<br /><br />infile.open (inName.c_str());<br />outfile.open (outName.c_str());<br /><br /><br />if(infile.fail( ))<br /><br />{<br />cout<<"Input file opening failed.n" ;<br />exit (1);<br />}<br /><br />if(outfile.fail())<br />{<br />cout<<"Output file opening failed.n" ;<br />exit (1);<br />}<br /><br />calcAverages(infile, outfile);<br /><br />outfile.close();<br />infile.close();<br /><br /><br />inout.open(outName.c_str());<br /><br />if(inout.fail( ))<br />{<br />cout<<"Input file opening failed.n" ;<br />exit (1);<br />}<br /><br />checkReport(inout);<br />inout.close();<br /><br />return 0;<br /><br /><br />}<br /><br /><br /><br /></string></cstdlib></fstream></iostream> - February 9, 2010 at 7:48 pm #3627GWILouisaxwzklaParticipant
What does your input file look like? What criteria is used to select samples ( randomly ).
- February 10, 2010 at 6:03 am #3628BeauLGOffsdwntcParticipant
I’m not clear on your question? The input file is an *.dat file.
- February 13, 2010 at 10:21 pm #3629GWILouisaxwzklaParticipant
Could you perhaps post the input file for your program?
- February 14, 2010 at 5:47 am #3630BeauLGOffsdwntcParticipant
thanx for the reply, but I sorted this problem
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.