编写一个C++程序,该程序将读取三家商店中每一家的今天销售额的输入文件,四舍五入为最接近的1000美元。可以在模块中找到示例输入文件。文件名- sales.txt -应该位于项目的当前目录中。然后,程序应该生成一个条形图,显示每个商店的销售额。通过显示一行星号来创建图形中的每个条形图。每个星号应该代表1000美元的销售额。
download.txt文件为:
10000
8000
5000
我希望文件看起来是这样的:
Today's Sales Chart
Gathering input from file...
Daily Sales (each * = $1000)
Store 1: *********
Store 2: ******
Store 3: *******我尝试了while循环,但我不知道如何包含.txt或条形图
#include <iostream>;
#include <fstream>;
#include <string>;
using namespace std;
int main()
{
//Variables
int store1;
int store2;
int store3;
// Display table heading
cout << "Today's sales Chart\n" << endl;
cout << "Gathering input from file...\n";
cout << "\nDaily Sales (each * = $1000)\n";
ifstream inputfile;
inputfile.open("download.txt");
system("pause");
return 0;
}发布于 2019-10-09 10:36:16
我曾经有过一个类似的问题。您应该能够执行以下操作:
string line;创建一个变量,您将在其中存储行。
while(inputfile >> line){遍历文件中的每一行。
int x = stoi(line)将行(字符串)转换为整数。
在while循环中,你可以对x做你需要做的事情,比如找出你需要多少个星号。所以:
ifstream inputfile;
inputfile.open("download.txt")
string line;
while(inputfile >> line){
int x = stoi(line); //will be 10000 in first loop
int i = x / 1000; //number of asterisks
for(int j = 0;j < i;j++){
cout << "*";
}
cout << endl;
}希望能有所帮助。
编辑:显然,你可以赋值给你的存储变量,并在需要的时候使用它们。
https://stackoverflow.com/questions/58296077
复制相似问题