gpt4 book ai didi

c++ - 五个商店的销售条形图

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:15 24 4
gpt4 key购买 nike

我创建了这个程序,要求 5 家公司输入当天的销售额。似乎工作正常。我想不通的是如何让图表在所有 5 家公司都输入销售后出现。这是我到目前为止的代码。

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int sales = 0;
int store = 0;
float stars;

for (int store = 1; store <= 5; store++)
{
cout << "Enter today's sale for store " << store << ":";
cin >> sales;


stars = sales/100;
cout << "SALES BAR CHART:" << endl;
cout << "(Each * = $100)" << endl;
cout << "Store" << store << ":";


for (int y = 0; y < stars; y++)
{
cout << "*";

}

cout << endl;
}


system("PAUSE");
return EXIT_SUCCESS;
}

最佳答案

您需要将商店的每个值存储在一个数组中,以便稍后打印出来。如果你希望它是动态的,你可以动态分配一个数组:

int stores = 5;
int* stores_stars = NULL;
stores_stars = new int[numberOfStores];

然后在为每个商店分配值后,您可以遍历数组的每个元素并使用您编写的循环打印出每个商店的星级。

如果你不想使用数组,或者没有被教过,你可以只使用单独的变量并使用多个 if 语句,但我建议你使用数组。


因为你不能使用数组(不太喜欢写得不好的作业)

然后您将需要使用多个变量。您可以声明 5 个变量来存储每个星星

int storeStars1,storeStars2,storeStars3,storeStars4,storeStars5;

并根据循环中存储的值分配每一个

if (store == 1)
storeStars1 = //Put your value here
else if (store == 2)
//You can fill in the rest ;)

然后您可以为每个 storeStars 变量复制该循环 5 次。更好的是,将该循环放入一个函数中,并调用该函数 5 次。

关于c++ - 五个商店的销售条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com