двумерный массив и вложенные циклы

Я пытаюсь заставить это распечатать продажи за 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;
}

1

Решение

Пара вещей:

1) вы хотите убедиться, что объект в левой части выражения является допустимым «lvalue», то есть он переводит в место, где может быть сохранен результат оценки RHS. Линия как

sales *month[x] = v;

Не встречает это.

Еще одна ошибка: когда вы объявляете массив

sales[year][month];

Вы должны убедиться, что оба year а также month существуют (были объявлены) и имеют действительное значение (возможно 3 а также 12?)
— у вас есть массив с именем date но вы имеете в виду month в

sales * month[x] = v;

Как я уже упоминал, вы не можете просто умножить вещи в левой части уравнения. Вы могли бы рассмотреть

sales[year][month] = v;

В вашем случае ваш внешний цикл z идет от 0 в 2— это, вероятно, ваш year; и внутренний цикл идет от 0 в 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
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector