mixed variables in a single array
Mar 10, 2010 at 9:31pm UTC
I started having errors when I tried to work with the following code
I instantly noticed I set the array as a string and I was trying to work with integers and doubles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
double itemP;
double envelopeP;
double noteP;
int itemQ;
int envelopeQ;
int noteQ;
int itemSold;
int envelopeSold;
int noteSold;
string envelopeY;
string noteY;
int toBuy;
string colorArray[10][2];
ifstream inSW;
inSW.open("Southwest.txt" );
for (int x=0; x<10; x++) {
getline(inSW, colorArray[x][0]);
inSW >> colorArray[x][1].itemP;
inSW >> colorArray[x][1].enevelopeP;
inSW >> colorArray[x][1].noteP;
inSW >> colorArray[x][1].itemQ;
inSW >> colorArray[x][1].envelopeQ;
inSW >> colorArray[x][1].noteQ;
inSW >> colorArray[x][1].itemSold;
inSW >> colorArray[x][1].envelopeSold;
inSW >> colorArray[x][1].noteSold;
colorArray[x][2].envelopeY = "(N)" ;
colorArray[x][2].noteY = "(N)" ;
colorArray[x][2].toBuy = 0;
}
}
inSW.close();
Do I need to make separate arrays for each type of variable? If not, what am I doing wrong?
Mar 10, 2010 at 10:03pm UTC
Yes. By definition an array consists of elements of the same type.
Although what you're trying to do can be done with an array of objects.
Mar 10, 2010 at 10:11pm UTC
How would I go about doing that?
Topic archived. No new replies allowed.