0

I am trying to get this to print out the sales of 12 months for three years using a two dimensional array and nested looping. I am confused. Could someone please show me what I am doing wrong with my code using these methods. I do not want an alternative.

#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;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
AEGIS
  • 162
  • 1
  • 3
  • 17

1 Answers1

2

A couple of things:

1) you want to make sure that the thing on the left hand side of an expression is a valid "lvalue" - that is, it translates to a location where the result of evaluating the RHS can be stored. A line like

sales *month[x] = v;

Doesn't meet that.

Another mistake: when you declare array

sales[year][month];

you need to make sure that both year and month exist (have been declared) and have a valid value (maybe 3 and 12?) - you have an array called date but you are referring to month in

sales * month[x] = v;

As I mentioned, you cannot just multiply things on the left hand side of an equation. You might consider

sales[year][month] = v;

In your case, your outer loop z goes from 0 to 2, - that is probably your year; and the inner loop goes from 0 to 11 so I assume that is the month. Then you might do

sales[z][x] = y;

It is possible that you actually want to record the "year that these sales numbers apply to", in which case you need to create an array

salesYears[3];

And store the value of year there. It is a really good idea to prompt the user when you are expecting input -

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

etc.

These are just some pointers. Your code is really quite a mess. Remember:

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
Floris
  • 45,857
  • 6
  • 70
  • 122
  • https://github.com/GuangchuangYu/CxxPrimerPlus/tree/master/chapter5 I found a solution to the problem. My code was really messy. I think I need to review the past five chapters, and do some extra practice. I just started learning c++ from this book. Thanks for your help. – AEGIS Nov 26 '13 at 02:22