节目说明: 编写一个程序来读取名为sales.dat的数据文件。 然后,程序应显示比较每个商店销售额的条形图,并将销售条形图输出到文件名results.dat。 通过显示一行星号在条形图中创建每个条形。每个星号应代表100美元的销售额。
它在visual studio,sales.txt和results.txt中使用了2个资源文件。所以基本上,我让程序工作,我提交给我的任务。我今天重新打开它以弄乱它,它完全停止工作。它似乎不是从sales.txt读取。 sales.txt在每一行中都有随机整数。例如,1000,1200,1400。输出将在results.txt和控制台中的每一行显示10,12和14个星号。
问题在于它说 商店1: 商店2: 等等,但没有星号。
int main() {
int storeAmount = 0;
double sales = 0.0;
int starAmount = sales / 100; //gets the amount of stars needed
ifstream inFile("sales.txt"); //opens the file sales.txt which is a resource file
ofstream outFile("results.txt"); //opens the file results.txt which is our output file
string outputStars = ""; //holds the stars for the bar graph
cout << "Please input the amount of stores:" << endl; //input the amount of stores
cin >> storeAmount;
cout << "SALES BAR CHART:" << endl; //header output
cout << "Each * = $100" << endl;
for (int storeNum = 0; storeNum < storeAmount; storeNum++) { //loops to the max store amount
inFile >> sales; //variable sales holds the value of each of the lines in sales.txt
for (int i = 0; i < starAmount; i++) { //adds stars onto the string
outputStars += "*";
}
cout << "Store " << storeNum + 1 << ": " << outputStars << endl; //ouputs in the console
outFile << "Store " << storeNum + 1 << ": " << outputStars << endl; //outputs to the file
if (inFile.eof()) { //stops the duplication of the last line if the store amount is greater than the numbers in sales.txt
break;
}
}
outFile.close(); //closes the files
inFile.close();
system("pause");
return 0;
}
答案 0 :(得分:0)
int starAmount = sales / 100;
分配销售后需要。
答案 1 :(得分:0)
将所有建议结合在一起 - 我稍微更改了您的代码,添加了有关所做更改的注释以及完成程序要完成的一些任务
#include <string>
#include <iostream>
#include <fstream>
//format your code properly
int main() {
std::ifstream inFile("sales.txt"); //opens the file sales.txt which is a resource file
std::ofstream outFile("results.txt"); //opens the file results.txt which is our output file
//todo - check that files are opened
std::cout << "Please input the amount of stores:\n"; //input the amount of stores
int storeAmount = 0; //declare var as close to usage as possible
std::cin >> storeAmount;
//todo use if(std::cin >> storeAmount) instead to check that input was proper
std::cout << "SALES BAR CHART:\n"; //header output
std::cout << "Each * = $100\n";
for (int storeNum = 0; storeNum < storeAmount; storeNum++) { //loops to the max store amount
//declare var as close to usage as possible
double sales = 0.0;
//check the result of input operation before using the value read
//instead of using eof postfactum
if(inFile >> sales) { //variable sales holds the value of each of the lines in sales.txt
//declare variable in the smallest scope possible
int starAmount = sales / 100; //gets the amount of stars needed
//use constructor instead of loop
std::string outputStars(starAmount, '*'); //holds the stars for the bar graph
std::cout << "Store " << storeNum + 1 << ": " << outputStars << '\n'; //ouputs in the console
outFile << "Store " << storeNum + 1 << ": " << outputStars << '\n'; //outputs to the file
}
else {
break;
}
}
outFile.close(); //closes the files
inFile.close();
return 0;
}