你好,这是我的第一篇文章。我正在使用以下参数进行家庭作业。C++哨兵/计数控制循环开始编程
工件工人由片支付。通常产生更多产出的工人以更高的费率获得报酬。
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
输入:对于每个工人,输入已完成件的名称和数量。
Name Pieces Johnny Begood 265 Sally Great 650 Sam Klutz 177 Pete Precise 400 Fannie Fantastic 399 Morrie Mellow 200
输出:打印适当的标题和列标题。应该有 是每个工人的一个详细信息行,其中显示名称, 件的数量以及获得的金额。计算并打印数量为 的碎片总数和所赚取的美元金额。
处理:对于每个人,通过将 件数乘以适当的价格来计算所获得的工资。累计总数 件数和支付的总金额。
样本程序输出:
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
您需要代码,编译,链接,和运行将输入到在上述附着所示的输出规格 一个标记控制 循环程序。输入项目应输入 一个名为piecework1.dat的文本文件和 piecework1.out中存储的输出文件。程序文件名是piecework1.cpp。这三个文件的副本 应以原始格式通过电子邮件发送给我。
使用单个变量读取名称,而不是使用两个不同的 变量。为了达到这个目的,你必须使用类中讨论的getline(流, 变量)函数,除了你将用你的文本流变量名替换 cin。不要忘记在您的程序的顶部编写 编译器指令#include < string> 以确认字符串变量name的使用情况。您的 嵌套的if-else语句,累加器,计数控制循环,应正确设计 以正确处理数据。
下面的代码将运行,但不会产生任何输出。我认为它需要像第57行一样的计数控件来停止循环。
类似的信息(这只是一个例子....这就是为什么它是不是在代码中)。
count = 1;
while (count <=4)
有人可以检查代码,并告诉我,我需要什么样的算介绍,以及是否还有其他需要改动的地方。
谢谢。
//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;
}
当你说它不会产生任何输出时,你的意思是在运行你的程序后,piecework1.out是空的? –