【发布时间】:2012-11-03 16:59:14
【问题描述】:
大家好,这是我的第一篇文章。我正在使用以下参数进行家庭作业。
计件工人按件计酬。通常是生产一种 以更高的比率支付更多的产出。
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 的文本文件和存储在 计件1.out 。程序文件名为piecework1.cpp。的副本 这三个文件应该以原始形式通过电子邮件发送给我。
使用单个变量读取名称,而不是使用两个不同的变量 变量。为此,您必须使用 getline(stream, 变量)函数,如课堂上讨论的,除了你将替换 cin 与您的文本文件流变量名称。不要忘记编码 程序顶部的编译器指令#include 确认使用了字符串变量 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 是空的?
标签: c++