• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

for loop

Hi , how can I use this for loop, so that when it goes through the second time, it doesn't  ask me for the height but for the weight..Thanks


#include <iostream>
using namespace std;

// program to calculate the BMI 

int main () {

	//initializing the multi-dimensional array 2 values, 5 persons
	int BMIaray[2][5];
	


	//nested for loop for input values
	for (int i=0 ;i<2;i++)
	{
		cout<<"height "<<i<<endl;

			for (int j=0 ;j<5;j++)
			{
				
				cin>>BMIaray[i][j];
				cout<<"person# "<<j<<endl;

			}
	}


	//nested for loop for output values
	for (int i=0 ;i<2;i++)
	{

			for (int j=0 ;j<5;j++)
			{
				cout<< BMIaray[i][j]<<"\t";
			}
			cout<<endl;

	}




	system("pause");
	return 0;
}

Open in new window

0
jhonc66
Asked:
jhonc66
  • 3
  • 2
1 Solution
 
Infinity08Commented:
I would use a different way of storing the data, and would re-organize the loop.

Instead of a 2D array of int's, have an array of structs, where each struct represents the information for one person (height, weight, and whatever else you need).

You can then have a loop that reads the input data for each person, one at a time, and for each person, you ask for both their height and weight, and store it in the appropriate struct in the array.
0
 
jhonc66Author Commented:
it has to be using a two dimensional array.... could you give me an example of how to,,.. thanks..
0
 
Infinity08Commented:
If you really need to use a 2D array, you can still re-organize the for loop, to loop over the persons, and read both their height and weight in the same iteration of the loop.
0
 
Infinity08Commented:
or in pseudo code :

        for every person :
            read the person's height and store it in the array
            read the person's weight and store it in the array
        end for
0
 
jhonc66Author Commented:
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now