Problem with loop

This is a discussion on Problem with loop within the C Programming forums, part of the General Programming Boards category; I'm trying to write some code which will take the value from one variable ( multiple guesses ), format it ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It's easier to help if you...
    Quote Originally Posted by Dave_Sinkula View Post
    >Post the smallest and simplest compilable program that demonstrates the problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    Todd Burch, you my friend are a legend, 300 lines of code and all that was wrong was 1 digit, I hate C....

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by sweetorangepie View Post
    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...
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    507
    I think you can use 'i' itself instead of 'n' if you set the value of i as 1 in the for loop.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 07:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 05:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 11:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21