-2

Still new to this. What is wrong with this code? I'm trying to make and use a 2 dimensional array. Is my general idea correct? To step through it with nested for loops? What exactly is wrong with my code? It won't compile.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
const double NUM_MONKEYS = 3;
const double NUM_DAYS = 5;
double monkeys[NUM_MONKEYS][NUM_DAYS];
int row, column;

for (row = 0, row < NUM_MONKEYS, row++)
{
    for (column = 0, column < NUM_DAYS, column++)
    {

    cout << "Input amount of food eaten by monkey: " << row + 1;
    cout << " and day: " << column + 1 << endl;
    cin >> monkeys[row][column];
    }

}
return 0;

}

There's something I'm not getting, thanks!

1 Answers1

1

First of all - size of array should be of integer type and you have defined it as double. Second - Syntax of for loop is incorrect, there should be ';' instead of ',' in your for loop.

#include <iostream>
#include <iomanip>



int main()
{
    const int NUM_MONKEYS = 3;
    const int NUM_DAYS = 5;
    double monkeys[NUM_MONKEYS][NUM_DAYS];
    int row, column;

    for (row = 0; row < NUM_MONKEYS; row++)
    {
        for (column = 0; column < NUM_DAYS; column++)
        {

            std::cout << "Input amount of food eaten by monkey: " << row + 1;
            std::cout << " and day: " << column + 1 << endl;
            std::cin >> monkeys[row][column];
         }

    }
    return 0;

}

Though you can store double type values in your array. Also as said try and avoid 'using namespace std;' see here.

  • 1. Get rid of `using namespace std;` - google why this is pad practice. Also use `std::size_t` as per the comments above. – Ed Heal Jul 18 '17 at 18:22
  • true for namespace but please read the last line , i am pointing that array can be used to store double values but size should be integer – Deepanshu Kapoor Jul 18 '17 at 18:28
  • `const int NUM_MONKEYS = 3;` -Would be better to use `std::size_t` not int. Also if you know writing `using namespace std` is bad then do not write it. Bad habits are hard to get rid of – Ed Heal Jul 18 '17 at 18:30