C++ Sentinel/Count Controlled Loop beginning programming

Posted by Bryan Hendricks on Stack Overflow See other posts from Stack Overflow or by Bryan Hendricks
Published on 2012-11-03T16:59:14Z Indexed on 2012/11/03 16:59 UTC
Read the original article Hit count: 266

Filed under:
|

Hello all this is my first post. I'm working on a homework assignment with the following parameters.

  1. Piecework Workers are paid by the piece. Often worker who produce a greater quantity of output are paid at a higher rate.

1 - 199 pieces completed $0.50 each 200 - 399 $0.55 each (for all pieces) 400 - 599 $0.60 each 600 or more $0.65 each

Input: For each worker, input the name and number of pieces completed.

Name Pieces

Johnny Begood 265 Sally Great 650 Sam Klutz 177 Pete Precise 400 Fannie Fantastic 399 Morrie Mellow 200

Output: Print an appropriate title and column headings. There should be one detail line for each worker, which shows the name, number of pieces, and the amount earned. Compute and print totals of the number of pieces and the dollar amount earned.

Processing: For each person, compute the pay earned by multiplying the number of pieces by the appropriate price. Accumulate the total number of pieces and the total dollar amount paid.

Sample Program Output:

Piecework Weekly Report

Name Pieces Pay

Johnny Begood 265 145.75 Sally Great 650 422.50 Sam Klutz 177 88.5 Pete Precise 400 240.00 Fannie Fantastic 399 219.45 Morrie Mellow 200 110.00

Totals 2091 1226.20

You are required to code, compile, link, and run a sentinel-controlled loop program that transforms the input to the output specifications as shown in the above attachment. The input items should be entered into a text file named piecework1.dat and the ouput file stored in piecework1.out . The program filename is piecework1.cpp. Copies of these three files should be e-mailed to me in their original form.

Read the name using a single variable as opposed to two different variables. To accomplish this, you must use the getline(stream, variable) function as discussed in class, except that you will replace the cin with your textfile stream variable name. Do not forget to code the compiler directive #include < string > at the top of your program to acknowledge the utilization of the string variable, name . Your nested if-else statement, accumulators, count-controlled loop, should be properly designed to process the data correctly.

The code below will run, but does not produce any output. I think it needs something around line 57 like a count control to stop the loop.

something like (and this is just an example....which is why it is not in the code.)

count = 1; while (count <=4)

Can someone review the code and tell me what kind of count I need to introduce, and if there are any other changes that need to be made.

Thanks.

[code] //COS 502-90

//November 2, 2012

//This program uses a sentinel-controlled loop that transforms input to output.



#include <iostream>

#include <fstream>

#include <iomanip>  //output formatting

#include <string>   //string variables

using namespace std;



int main()

{



double pieces;          //number of pieces made

double rate;            //amout paid per amount produced

double pay;             //amount earned 

string name;            //name of worker

ifstream inFile;

ofstream outFile;



//***********input statements****************************

inFile.open("Piecework1.txt");  //opens the input text file

outFile.open("piecework1.out");  //opens the output text file

outFile << setprecision(2) << showpoint;

outFile << name << setw(6) << "Pieces" << setw(12) << "Pay" << endl;

outFile << "_____" << setw(6) << "_____" << setw(12) << "_____" << endl;

getline(inFile, name, '*');         //priming read

inFile >> pieces >> pay >> rate;    // ,,

while (name != "End of File")           //while condition test

{                                   //begining of loop

    pay = pieces * rate;

    getline(inFile, name, '*');     //get next name

    inFile >> pieces;               //get next pieces

}                                   //end of loop

inFile.close();

outFile.close();

return 0;

}[/code]

© Stack Overflow or respective owner

Related posts about c++

Related posts about homework