你好,這是我的第一篇文章。我正在使用以下參數進行家庭作業。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是空的? –