首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出绑定到数组的符号(C++)

输出绑定到数组的符号(C++)
EN

Stack Overflow用户
提问于 2021-02-25 13:25:35
回答 1查看 15关注 0票数 0

本质上,我需要它为每一辆售出的汽车显示一个星号,然而,无论在第一个for循环中输入什么,它只输出一次符号,我做错了什么?提前谢谢。

代码语言:javascript
复制
#include 
#include 
using namespace std;

const int NUM_EMPLOYEES = 3;

int main()
{
    int carsSold[NUM_EMPLOYEES];
    int i;
    int totalSales = 0;
    char symbol = '*';

    cout << "Welcome to the sales report program!\n";
    cout << "\nThis program will produce a monthly sales report for the amount of cars sold.\n";

    for (i = 0; i < NUM_EMPLOYEES; i++)
    {
        cout << "Enter the cars sold by employee " << i + 1 << ": ";
        cin >> carsSold[i];

        totalSales += carsSold[i];
    }

    cout << "\nMonthly Sales Sheet";
    cout << "\n(each * = 1 car)\n";

    for (i = 0; i < carsSold[i]; i++)
    {
        cout << "\nEmployee " << i + 1 << ": " << symbol << endl;
    }

    cout << "\nTotal cars sold for the month: " << totalSales;



    return 0;
}

下面是输出:

欢迎来到销售报告节目!

该程序将生成汽车销量的月度销售报告。

输入员工1: 3售出的汽车

输入员工2: 2售出的汽车

输入员工3: 4售出的汽车

每月销售表(每个*=1辆车)

员工1:*

员工2:*

员工3:*

当月汽车总销量:9辆

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-25 13:44:03

使用我上面给你的提示:

代码语言:javascript
复制
#include 
#include 
using namespace std;

#define NUM_EMPLOYEES 3

...

cout << "\nMonthly Sales Sheet";
cout << "\n(each * = 1 car)\n";
for (i = 0; i < NUM_EMPLOYEES; i++)
{
    auto bar = new string(carsSold[i], symbol);
    cout << "\nEmployee " << i + 1 << ": " << *bar << endl;
}
cout << "\nTotal cars sold for the month: " << totalSales << "\n";

...

这将导致以下输出:

代码语言:javascript
复制
This program will produce a monthly sales report for the amount of cars sold.
Enter the cars sold by employee 1: 3
Enter the cars sold by employee 2: 2
Enter the cars sold by employee 3: 4

Monthly Sales Sheet
(each * = 1 car)

Employee 1: ***

Employee 2: **

Employee 3: ****
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66362860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档