# Thread: Problem with loop

1. ## Problem with loop

I'm trying to write some code which will take the value from one variable ( multiple guesses ), format it and split it up into diffrent variables (one for each guess). I have a set of if statements which check the length of the variable (to see how many guesses have been entered) and assign a variable a number (the number of guesses). These if statements are below:

Code:
```if ( strlen( guess ) >= 4 )
{
guess_number = 1;
}
if ( strlen ( guess ) >= 8)
{
guess_number = 2;
}
if ( strlen ( guess ) >= 12)
{
guess_number = 3;
}
if ( strlen ( guess ) >= 16)
{
guess_number = 4;
}```
I've made this loop:

Code:
```if (guess_number >=1){
n = 1;
for(i = 0; i < 4; i++){
if ((guess[n] == 'r')||(guess[n] == 'R')){
formattedguess1[i] = COLOR_RED;
}else if ((guess[n] == 'b')||(guess[n] == 'B')){
formattedguess1[i] = COLOR_BLUE;
}else if ((guess[n] == 'y')||(guess[n] == 'Y')){
formattedguess1[i] = COLOR_YELLOW;
}else if ((guess[n] == 'g')||(guess[n] == 'G')){
formattedguess1[i] = COLOR_GREEN;
}else if ((guess[n] == 'w')||(guess[n] == 'W')){
formattedguess1[i] = COLOR_WHITE;
}else if ((guess[n] == 'o')||(guess[n] == 'O')){
formattedguess1[i] = COLOR_ORANGE;
}else {
valid = 1;
printf("The guess you entered isn't valid, please enter it in the format of r = red and o = orange");
break;
}
n++;
}
}```
Which moves the first 4 characters in the variable guess to formattedguess1 after being formatted. "i" being the character number in the formattedguess and "n" the number its on from the guess variable. I'm planning on having a identicle loop after which if the number of guesses is greater than 2 it does the same thing but for the 5,6,7,8 value of guess since n gets incremented each time around.

The problem which i'm having is that it always display's the message in the else statement.

I have apsolutly no idea why this is happening and am hoping someone will have an explination??

Thanks

2. It's easier to help if you...
Originally Posted by Dave_Sinkula
>Post the smallest and simplest compilable program that demonstrates the problem.

3. I'm guessing that n needs to start at 0, not 1. But, guess is all I can do with what you've posted.

Todd

4. Todd Burch, you my friend are a legend, 300 lines of code and all that was wrong was 1 digit, I hate C....

5. Code:
`>					for(i = 0; i < 4; i++){`
I would print out guess at this point to make sure it contains the letters for which you are looking.
Code:
`printf("%s\n", guess)`

6. Originally Posted by sweetorangepie
Todd Burch, you my friend are a legend, 300 lines of code and all that was wrong was 1 digit, I hate C....
Wow. Maybe I should change my signature to quote this...

7. I think you can use 'i' itself instead of 'n' if you set the value of i as 1 in the for loop.

Popular pages Recent additions