Different variable types in same array?

I'm trying to program an array that will store the latitude and longitude of a coordinate.

Is it possible to store the N/S component and degrees in the same array?

1
2
3
4
5
6
const int arraysize = 10;
double firstLongitude[arraysize]={0,0,0,0,0,0,0,0,0,0};

cout << "Enter the longitude of the first coordinate:" << endl;
cin >> firstLongitude[1] >> firstLongitude[2];
cout << firstLongitude[1] << " " << firstLongitude[2] << endl;


In fact, it's acting so weird. Can anyone help & explain?

Enter the longitude of the first coordinate:
N 45.0
0 0
Press any key to continue . . .

Enter the longitude of the first coordinate:
N 45.0
0 0
Press any key to continue . . .

Enter the longitude of the first coordinate:
N 45.0
0 0
Press any key to continue . . .


So I can store int & doubles in the array. But how do I store a char, and not mess up the values afterwards? Thanks!
Last edited on
You can use an array of structs
eg:
1
2
3
4
5
6
7
8
9
struct Position
{
    char direction;
    double degrees;
};

Position firstLongitude[arraysize] = { {' ',0}, {' ',0} ... // initialize direction to ' ' and degrees with 0. 
cin >> firstLongitude[0].direction >> firstLongitude[0].degrees;
cout << firstLongitude[0].direction << " " << firstLongitude[0].degrees << endl;


http://www.cplusplus.com/doc/tutorial/structures/
Yes! Thank you! That's exactly what I needed!

1
2
3
4
5
6
7
8
9
10
11
12
struct coordinate {
	char direction;
	double degrees;
	double minutes;
	double seconds;
	double alpha;
	double beta;
} first, second;

cout << "Enter the longitude of the first coordinate:" << endl;
cin >> first.direction >> first.degrees;
cout << first.direction << " " << first.degrees << endl;

Enter the longitude of the first coordinate:
N 34.5
N 34.5
Press any key to continue . . .

Now, can I have a structure within a structure? Do they behave like tables in Lua?

Additionally, I was a bit confused by the tutorial as to how to pass a structure to a function.

void convert_to_radians (coordinate first);

Is how I would do it?
Last edited on
Update - moved to a different thread. Thanks for your help Bazzy!
Topic archived. No new replies allowed.