0

2 次元配列とネストされたループを使用して、これを 3 年間の 12 か月の売上を出力しようとしています。私は混乱しています。これらのメソッドを使用してコードで何が間違っているかを教えてください。私は代替手段を望んでいません。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int x = 0;
int v = 0;
int y = 0;
int sum = 0;
const int year = 3;
const int month = 12;

int _tmain(int argc, _TCHAR* argv[])
{
    int sales[year][month];
    char * date[12] = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
    for(int z = 0; z < 3; z++)
    {
        {
            cin >> v;
            sales * year[z] = v;
        }

        for(int x = 0; x < 12; x++)
        {
            cout << "Please enter the sales for month " << date[x] << ":\n";
            cin >> y;
            sales * month[x] = y;
            sum += y;
        }
    }
    cout << "There are the sales of the c++ crook: \n";

    cout << sales[3][12] << endl;
    //cout << "Month 1 = " << year[0] << "   " << month[0] << endl;
    //cout << "Month 2 = " << year[0] << "   " << month[1] << endl;
    //cout << "Month 3 = " << year[0] << "   " << month[2] << endl;
    //cout << "Month 4 = " << year[0] << "   " << month[3] << endl;
    //cout << "Month 5 = " << year[0] << "   " << month[4] << endl;
    //cout << "Month 6 = " << year[0] << "   " << month[5] << endl;
    //cout << "Month 7 = " << year[0] << "   " << month[6] << endl;
    //cout << "Month 8 = " << year[0] << "   " << month[7] << endl;
    //cout << "Month 9 = " << year[0] << "   " << month[8] << endl;
    //cout << "Month 10 = " << year[0] << "   " << month[9] << endl;
    //cout << "Month 11 = " << year[0] << "   " << month[10] << endl;
    //cout << "Month 12 = " << year[0] << "   " << month[11] << endl;

    //cout << "Month 1 = " << year[1] << "   " << month[0] << endl;
    //cout << "Month 2 = " << year[1] << "   " << month[1] << endl;
    //cout << "Month 3 = " << year[1] << "   " << month[2] << endl;
    //cout << "Month 4 = " << year[1] << "   " << month[3] << endl;
    //cout << "Month 5 = " << year[1] << "   " << month[4] << endl;
    //cout << "Month 6 = " << year[1] << "   " << month[5] << endl;
    //cout << "Month 7 = " << year[1] << "   " << month[6] << endl;
    //cout << "Month 8 = " << year[1] << "   " << month[7] << endl;
    //cout << "Month 9 = " << year[1] << "   " << month[8] << endl;
    //cout << "Month 10 = " << year[1] << "   " << month[9] << endl;
    //cout << "Month 11 = " << year[1] << "   " << month[10] << endl;
    //cout << "Month 12 = " << year[1] << "   " << month[11] << endl;
    //cout << "The annual sales for c++ crook is: " << sum << " ;]";
    cin.get();
    cin.get();


    return 0;
}
4

1 に答える 1

2

いくつかのこと:

1) 式の左側にあるものが有効な「左辺値」であることを確認したい場合。つまり、RHS の評価結果を格納できる場所に変換されます。みたいな一行

sales *month[x] = v;

それに合わない。

別の間違い: 配列を宣言するとき

sales[year][month];

yearとの両方monthが存在する (宣言されている) ことと、有効な値 (多分312?) があることを確認する必要があります - 呼び出された配列dateがありますが、参照していmonthます

sales * month[x] = v;

先ほども述べたように、方程式の左辺だけを掛け算することはできません。あなたは考えるかもしれません

sales[year][month] = v;

あなたの場合、外側のループzは から0, 2- それはおそらくあなたのyear; です。内側のループは0to から11なので、それが月だと思います。それからあなたはするかもしれません

sales[z][x] = y;

「これらの販売数が適用される年」を実際に記録したい場合は、配列を作成する必要があります。

salesYears[3];

そこに年の値を格納します。入力を期待しているときにユーザーにプロンプ​​トを表示することは本当に良い考えです -

std::cout << "Please enter the year of the sales" << std::endl;

これらはほんの一部の指針です。あなたのコードは本当にかなり混乱しています。覚えて:

Declare all variables
Make sure all arrays are the right size
Prompt for inputs
Check that inputs are valid
Address 2D arrays with arrayName[index1][index2]
Thing on left hand side of equation must be "valid lvalue"
You might need additional variables to store both the year and the sales
于 2013-11-26T01:51:10.953 に答える