for循环不通过数组C ++表递增[关闭]

问题描述 投票:0回答:1

我已经分别测试了我的数组,以确保它正在工作并保持所有值,但是由于某种原因,当我通过我的for循环运行它时,它只打印出3行3列中的10.95我不明白为什么它不是从我的表中拉出其余的值。

这是作业:

编写,编译和运行C ++程序,将以下值输入名为price的数组:10.95,16.32,12.15,8.22,15.98,26.22,13.54,6.45和17.59输入数据后,让程序显示3行3列的值。

这是我写的代码:

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

const int COLS = 3;
const int ROWS = 3;

int main()
{
    const int num_items = 9;
    float prices[num_items];

    cout << "Enter the prices of your " << num_items << " items: ";
    cin >> prices[0];
    cin >> prices[1];
    cin >> prices[2];
    cin >> prices[3];
    cin >> prices[4];
    cin >> prices[5];
    cin >> prices[6];
    cin >> prices[7];
    cin >> prices[8];

    float table[ROWS][COLS] = {{prices[0], prices[1], prices[2]},
                                {prices[3], prices[4], prices[5]},
                                {prices[6], prices[7], prices[8]}};

    cout << "The prices you have entered are:\n";

    for (int x = 0; x < ROWS; x++)
    {
        for (int y = 0; y < COLS; y++)
        {
            cout << setw(6) << table[ROWS][COLS] << " ";
        }
        cout << endl;
    }
    return0;
}
c++ arrays for-loop
1个回答
5
投票
cout << setw(6) << table[ROWS][COLS] << " ";

应该

cout << setw(6) << table[x][y] << " ";
© www.soinside.com 2019 - 2024. All rights reserved.