找到你要的答案

Q:Using for loops to create a bar graph

Q:使用循环创建条形图

CAN ONLY USE LOOPS & IF/ELSE Statements

So for my assignment I'm given this: Write a program with for loops that asks the user to enter today’s sales for five stores. The program should then display a bar graph comparing each store’s sales. Create each bar in the bar graph by displaying a row of asterisks. Each asterisk should represent $100 of sales.

So it should like:

Store 1: *****

Store 2: ***

Store 3: *******

etc.

I have most of it written up and everything displays properly except for my asterisks. I can't seem to get them to display. Also, I've searched around and there are answers to this, but all of them include arrays and we aren't allowed to use them in this exercise. Here's my code so far:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
        int sales = 0, sale = 0; //Identfy Variables

        for (int i = 1; i <= 5; i++) {
                cout << "Enter today's sales for Store " << i << ":" << endl;
                cin >> sale;
                sales += sale;
                sales /= 100; //in a similar manner to +=, /= can be used for division.
        }

        cout << "SALES BAR CHART:" << endl;
        cout << "Each * = $100" << endl;

                for (int x = 1; x <= 5; x++) {
                        cout << "Store " << x << ": ";
                        for (int s = 0; s < sales; s++) cout << "*";
                        cout << endl;
                }

         return 0;
}

只能使用循环和IF语句

所以我的任务是这样的:写一个程序的循环,要求用户进入今天的销售为五家商店。然后,该程序将显示一个条形图比较每个商店的销售。通过显示一排星号创建条形图的每一条。每个星号代表100美元销售。

所以它应该喜欢:

Store 1: *****

Store 2: ***

Store 3: *******

等.

我有大部分是写一切正常显示除了我的星号。我似乎无法让他们显示。此外,我已经搜索周围有答案,但所有这些都包括数组,我们不允许使用它们在这个练习。这是我的代码:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
        int sales = 0, sale = 0; //Identfy Variables

        for (int i = 1; i <= 5; i++) {
                cout << "Enter today's sales for Store " << i << ":" << endl;
                cin >> sale;
                sales += sale;
                sales /= 100; //in a similar manner to +=, /= can be used for division.
        }

        cout << "SALES BAR CHART:" << endl;
        cout << "Each * = $100" << endl;

                for (int x = 1; x <= 5; x++) {
                        cout << "Store " << x << ": ";
                        为(int = 0;S & lt;销售;+ +) cout << "*";
                        cout << endl;
                }

         return 0;
}
answer1: 回答1:

Try this.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string star = "";   
    int sale;
    for (int i = 1; i <= 3; i++)
    {
        cout << "Enter today's sales for Store " << i << ":" << endl;
        cin >> sale;
        star += "Store " + to_string(i) + string(":");
        for(int j= 0; j<sale/100; j++) {
            star += "*";
        }    
        star += "\n";
    }
    cout<<star;
   return 0;
}

试试这个。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string star = "";   
    int sale;
    for (int i = 1; i <= 3; i++)
    {
        cout << "Enter today's sales for Store " << i << ":" << endl;
        cin >> sale;
        star += "Store " + to_string(i) + string(":");
        for(int j= 0; j<sale/100; j++) {
            star += "*";
        }    
        star += "\n";
    }
    cout<<star;
   return 0;
}
answer2: 回答2:

The problem with your code is that you do not store the data for each store individually. You have one variable called sales, which stores the sales of only the last store the end-user entered. All numbers the user enters before that get written over.

Since you have five stores, sales needs to be an array of five items:

int sales[5], total = 0;

Change your for loop to iterate 0 through 4, inclusive. Print (i+1) to end-users to stay one-based. Read sales numbers into sales[i] array element, then use it in the second loop to print your bar graph:

for (int i = 0 ; i != 5 ; i++) {
    cout << "Enter today's sales for Store " << (i+1) << ":" << endl;
    cin >> sales[i];
    sales[i] /= 100;
    total += sales[i];
}
...
// Now print your bar graph using a loop on i, and sales[i]

您的代码的问题是,您不单独存储每个存储区的数据。您有一个名为“销售”的变量,它只存储最终用户输入的最后一个商店的销售额。所有用户输入之前,得到书面。

因为你有五个商店,销售需要是一个数组的五个项目:

int sales[5], total = 0;

改变你的for循环遍历0到4,包括。打印(I + 1)到最终用户留一个。将销售编号读取到销售i数组元素中,然后在第二个循环中使用它来打印条形图:

for (int i = 0 ; i != 5 ; i++) {
    cout << "Enter today's sales for Store " << (i+1) << ":" << endl;
    cin >> sales[i];
    sales[i] /= 100;
    total += sales[i];
}
...
// Now print your bar graph using a loop on i, and sales[i]
answer3: 回答3:

Your for loop is missing a definition of s:

for (int s; s < sales; s++)

should be:

for (int s = 0; s < sales; s++)

Without this explicit initialization, a variable s is declared, and memory is allocated for it, but its value remains whatever was left in memory from the last time that address was used. It's likely that this left over data was equivalent to an integer larger than sales, and thus your for loop wouldn't run, not even once.

As @dasblinkenlight mentioned, you have a singular variable that stores the "sales" of every store. Each store needs a separate variable, since each store is going to sell a different amount.

for循环缺少S的定义:

为(int s;s & lt;销售;+ +)

应该是:

为(int = 0;S & lt;销售;+ +)

如果没有这个显式初始化,变量s被声明,并且为它分配内存,但是它的值仍然保留在最后一次使用该地址的内存中。很可能这个剩余的数据相当于一个大于销售的整数,因此你的for循环不会运行,甚至不是一次。

“dasblinkenlight提到,你有一个奇异的变量,将“销售”的每一个商店。每个商店需要一个单独的变量,因为每个商店将出售不同的金额。

answer4: 回答4:

You already have the answers, I just want to add two options that would make printing the *'s easier:

  1. using std::fill_n:

    std::fill_n(std::ostream_iterator<char>(cout), sales, '*');
    cout << endl;
    
  2. using std::string's constructor overload (2):

    cout << std::string(sales, '*') << endl;
    

你已经有了答案,我只想添加两个选项,使打印更容易:

  1. 使用std::fill_n:

    std::fill_n(std::ostream_iterator<char>(cout), sales, '*');
    cout << endl;
    
  2. 使用STD::字符串的构造函数重载(2):

    cout << std::string(sales, '*') << endl;
    
c++  loops  for-loop