【问题标题】:two dimensional array and nested looping [closed]二维数组和嵌套循环
【发布时间】:2013-11-26 01:31:37
【问题描述】:

我正在尝试使用二维数组和嵌套循环打印出三年内 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;
}

【问题讨论】:

  • 你为什么在两个const int变量(monthyear)上使用operator[]?关于不想要“替代品”,您是否希望您的代码编译??
  • 您能告诉我们您遇到的问题或遇到的错误吗?
  • sales * year[z] = v; ??
  • 还有这条线sales * year[z] = v;是错的。你不能那样做任务
  • 我猜他想要sales[year][z] 以及sales[year][x] 在嵌套循环中......

标签: c++ arrays loops nested dimensional


【解决方案1】:

有几点:

1) 您要确保表达式左侧的内容是有效的“左值”——也就是说,它转换为可以存储 RHS 求值结果的位置。像这样的一行

sales *month[x] = v;

不满足。

另一个错误:当你声明数组时

sales[year][month];

您需要确保yearmonth 都存在(已声明)并且具有有效值(可能是312?) - 您有一个名为 date 的数组,但您在

中指的是 month
sales * month[x] = v;

正如我所提到的,您不能只将等式左边的东西相乘。你可以考虑

sales[year][month] = v;

在您的情况下,您的外循环z0 变为2,-这可能是您的year;内部循环从011,所以我假设那是月份。那么你可能会这样做

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

【讨论】:

猜你喜欢
  • 2023-04-01
  • 2018-03-31
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多