I am having difficulty with nested loops. I am attempting to create a program that has the user enter monthly data during the course of a three year period. here is what I have so far.

#include <iostream>
const int Months = 12;
const int Years = 3;
int main()
{
using namespace std;
const char * months[Months]=
{"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"}
int i = 0;
int j = 0;
int temp = 0;
int sales[Years][Months];
cout << "Enter the sales data for each month during 2005-2007.\n";
for (i; i < 12; i++)
{cout << months[i] << ":\t";
for (j; j < Years; j++)
{cin >> sales[j];}
}
return 0;
}

The problem that is occurring is that the user will be able to enter data for Jan, but after that it won't continue and allow the user to input data for the rest of the months. If anyone can add any insight into this it would be greatly appreciated.

#include <iostream>

using namespace std;

const int Months = 12;
const int Years = 3;

int main(){
 
 const char * months[Months]={"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
 
 int temp = 0;
 int sales[Years][Months];
 
 cout << "Enter the sales data for each month during 2005-2007.\n";
 
 for (int i = 0; i < 12; i++){
     cout << months[i] << ":\t";
     for (int j = 0; j < Years; j++){   
         cin >> sales[j][i];
         cin.ignore();
     }
 }
 return 0;
}

That help at all, i've just cleaned it up a bit seems to work ok...

thank you so much, I really appreciate it. It is hard attempting to learn this without the benefit of an instructor. Thanks

This article has been dead for over six months. Start a new discussion instead.