c++ - 不读取 C++ 输入

标签 c++

我在 C++ 方面只有一点点经验。我正在尝试编写一个程序来打印“销售”数组的每个元素:

#include <iostream>
#include <iomanip>

using namespace std;
void printArray(int, int);

int main()
{
    char chips[5][50] = {"mild", "medium", "sweet", "hot", "zesty"};
    int sales[5] = {0};

    int tempSales, counter;
    const int i = 5;

    for (counter = 0; counter < i; counter++)
    {
        cout << "Please enter in the sales for " << chips[counter] << ": ";
        cin >> tempSales;

        tempSales >> sales[counter][5];
    }

    cout << "{";
    for (int counter = 0; counter < i; counter++)
    {
        cout << chips[counter] << ", ";
    }
    cout << "}" << endl;

    cout << "{";
    for (int counter = 0; counter < i; counter++)
    {
        cout << sales[counter] << ", ";
    }
    cout << "}" << endl;

    return 0;
}

要解决这个问题,我需要有相同的命令和关键字,而且不能是任何高级或奇怪的语法。出于某种原因,我的输入来自:

    cin >> tempSales

没有运作。以下是结果:

{mild, medium, sweet, hot, zesty, }
{0,0,0,0,0, }

而我只想看到第二个数组的 1、2、3、4 和 5。为什么它只打印 0 而不读取我的输入?请帮忙!

最佳答案

就像 rranjik 所说的那样,如果您只列出销售数量,您不应该需要二维数组,这似乎是您根据您提供的内容所做的,不是这种情况吗?

您是否有必要使用移位运算符 >>为你的任务?对于简单的整数赋值,它并不是真正必要的,您可以这样做:

int sales[5] = {0};将数组更改为简单数组而不是 2D。

sales[counter] = tempSales;对第 19 行的整数使用标准赋值

cout << sales[counter] << ", ";相应地更改您的输出。

希望这对您有所帮助!

关于c++ - 不读取 C++ 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279109/

相关文章:

c++ - dbus-send与QDBusAbstractInterface的对应关系

c++ - 使用可变初始值初始化 C++ vector

c++ - 支持多种人类语言

c++ - 对包含 k 个已排序部分的 n 个元素的数组进行排序

c++ - opencl中分配的缓冲区在哪里?

c++ - 结构偏移量不同?

c++ - 无法使用GCC在Ubuntu中编译C++ —包含/库问题(collect2 : ld returned 1 exit status)

c++ - Visual Studio 2013 不会忽略禁用的警告

c++ - 简单的 C++ .Net 控制台应用程序在 64 位 Win7 中崩溃

c++ - 使用多线程编程的调度器