for loop behaviour

Hi Guys,

I've built a function (well nearly) to accept a text string of numbers and depending on the value switch the corresponding Enumerated Colour to an 2D array.

I'm having troubles incrementing the position of the array to insert the enum.
The first for loop never increments.

Am I using the right choice of loops or does some one know of a more efficient way to do this?

thanks Brendan




Here's my code

inline void Image::Read(istream& in) //fumction to read and convert string to enumerator value an store in array
{

string colourstring; //string to hold input string
getline(in,colourstring); //read input to string
char invalue; //char to hold individual string characters


for( int i = 0; i < VLINES; i++) //while less than row length
{

for( int j = 0; j < HLINES; j++) //while less than column length


for(unsigned int v = 0; v < colourstring.size();v++) //while int less than input string
{

invalue = colourstring[v]; //store next character into char invilue for comparison

// switch statement to select Enumerator according to invalue value

switch (invalue)
{

case '0': //if char = 0 populate the 2d array at index with enum colour BLACK
myArray[i][j]= BLACK;

cout<<BLACK_SQUARE; //print coloured square
break;

case '1': //if char = 1 populate the 2d array at index with enum colour RED
myArray[i][j]= RED;
cout<<RED_SQUARE; // //print coloured square

break;


case '2': //if char = 2 populate the 2d array at index with enum colour GREEN
myArray[i][j]= GREEN;
cout<<GREEN_SQUARE; //print coloured square

break;

case '3': //if char = 3 populate the 2d array at index with enum colour YELLOW
myArray[i][j]= YELLOW;
cout<<YELLOW_SQUARE; //print coloured square

break;

case '4': //if char = 0 populate the 2d array at index with enum colour BLUE
myArray[i][j]= BLUE;
cout<<BLUE_SQUARE; //print coloured square
break;

case '5':
myArray[i][j]= MAGENTA; //if char = 0 populate the 2d array at index with enum colour MAGENTA
cout<<MAGENTA_SQUARE; //print coloured square

break;


case '6':
myArray[i][j]= CYAN; //if char = 0 populate the 2d array at index with enum colour CYAN
cout<<CYAN_SQUARE; //print coloured square
break;

case '7': //if char = 0 populate the 2d array at index with enum colour WHITE
myArray[i][j]= WHITE;
cout<<WHITE_SQUARE; //print coloured square
break;
default: break;

}//end switch

break;

}

}
}//end Read.






Hi,
Please could you use the # button on the right when posting code. This will add a pair of " " tags - put you code in between them and it will appear as
1
2
3
4
5
6
7
8
inline void Image::Read(istream& in) //fumction to read and convert string to enumerator value an store in array
{
   string colourstring; //string to hold input string
   getline(in,colourstring); //read input to string
   char invalue; //char to hold individual string characters

   for( int i = 0; i < VLINES; i++) //while less than row length
   { 

etc. (you may have to indent manually, but it makes the code MUCH easier to read, and so for us to help you:-)
Ok, I'm assuming that you are expecting a sequence of digits 0-7 which is the same length as the number of elements in your 2D array.
So for a 2x3 array you would have 6 digits.
To fix your code for the above you need to get rid of the
 
for(unsigned int v = 0; v < colourstring.size();v++) //while int less than input string  

loop.
Instead declare v at the same point you declare the other local variables (and initialise to zero). Then replace the lone break by v++.
You may also want to check colourstring.size() == VLINES*HLINES before entering the loops.
Thanks alot for that Faldrax,
I made the changes it it's looking alot better, I glad you showed my how to get rid of that extra loop.
They can become so confusing at times.
one again thanks
Brendan
Topic archived. No new replies allowed.