Sylar Sylar - 1 month ago 8
C++ Question

2d array: how to get value for each day instead of week

for (int j = 0; j < 7; j++)
{
for (int k = 0; k < 3; k++)
{

cout << "Enter meal " << k + 1 << " for day " << j + 1 << ":" << endl;
cin >> menu[j][k];

switch (menu[j][k])
{
case 1:
calories = 1250;
break;
case 2:
calories = 514;
break;
case 3:
calories = 970;
break;
case 4:
calories = 305;
break;
case 5:
calories = 628;
break;
case 6:
calories = 330;
break;
case 7:
calories = 821;
break;
case 8:
calories = 875;
break;
default:
cout << "\nInvalid Menu Input!!" << endl;
}

sum += calories;

}
}
cout<<calories;


This is a portion my code, i need to store 3 meals per day for 1 week which i managed to do, but the problem is the calculation part.. i need to calculate each days calories intake and display it but my codes are calculating the total calories intake for 1 week.. i have no idea on how to calculate it for each day
Can you guys guide me?

Answer

Why not print it each day (every iterate of the outer loop) and set the sum to 0 each day (every iterate of the outer loop).

for (int j = 0; j < 7; j++)
{
     sum = 0;
     for (int k = 0; k < 3; k++)
     {
          cout << "Enter meal " << k + 1 << " for day " << j + 1 << ":" << endl;  
          cin >> menu[j][k];

          switch (menu[j][k])
          {
              // cases here.
          }

          sum += calories;    
     }
     std::cout << sum << std::endl;


}
Comments