Thread: Character Array comparison

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Character Array comparison

    I was wondering how I would go about checking a character array for a specific character that keeps changing.

    What I want to do is compare the User input to the string. If the user does not pick one of the 6 letters, then they are prompted to do so again.
    Code:
    int i, j;
    char Scenario[6]= {'A','B','C','D','E','F'};
    char UserScenario='Z';
    
    
    	while(UserScenario!=TGnScenario[i]) /*Obviously not right, but basically what I need, but how would I go about doing this?*/
    	{
    		printf("TGn scenario (A to F)?\t");
    		scanf("%c", &UserScenario);
    	}
    I was thinking about using something like strchr, but my program ended up not doing anything. I don't know if it applies to this situation since I am comparing a character array, not really a string. This is probably really easy, but I am stuck on it.
    Last edited by magda3227; 07-08-2008 at 07:53 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I don't know if it applies to this situation since I am comparing a character array, not really a string. This is probably really easy, but I am stuck on it.
    You're right, strchr() requires a string. So why not make it one?
    Code:
    char Scenario[] = "ABCDEF";
    ...
    strchr(Scenario, UserScenario)
    Additionally, if your letters are always A-F, or something like that, this will work:
    Code:
    if(UserScenario >= 'A' && UserScenario <= 'F')
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Hm. It's still not working. I tried using

    Code:
    if(UserScenario >= 'A' && UserScenario <= 'F')
    and that didn't work. I also tried making my character array into a string and searching it. It doesn't work. I am getting an infinite loop or the program is bypassing it completely depending on small changes that I make. Any other suggestions?

    Code:
    int i, j;
    char Scenario[6]= "ABCDEF";
    char UserScenario='Z';
    char *p;
    
    p=strchr(Scenario,UserScenario);
    
    
     while(p!=NULL)
      {
    	printf("scenario (A to F)?\t");
    	scanf("%c", &UserScenario);
            p=strchr(p+1,UserScenario);
      }
    
    	}
    Am I not using strchr correctly? This is most likely the case...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char Scenario[6]= "ABCDEF";
    That is an array that has exactly 6 elements. It is not guaranteed to be followed by a zero, so the strchr() may well continue reading beyond the end of your string. Either change the 6 to 7, or just remove it altogether.

    Also strchr() returns NULL if you don't find something, so searching for Z and doing while(p != NULL) will lead to the loop never being entered.

    Should you enter the above loop:
    Code:
    p=strchr(p+1,UserScenario);
    would probably crash if p is NULL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Thanks. This works...


    Code:
    char Scenario[7]="ABCDEF";
    char UserScenario;
    char *p=NULL;
    
     while(p==NULL)
      {
    	printf("Scenario (A to F)?\t");
    	scanf("%c", &UserScenario);
    	p=strchr(Scenario,UserScenario);
      }
    For some reason, if the user enters a letter not A-F, Scenario (A to F)? is printed twice though. Any idea as to why?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by magda3227 View Post
    For some reason, if the user enters a letter not A-F, Scenario (A to F)? is printed twice though. Any idea as to why?
    The scanf() function reads one character. When you type A + Enter, the Enter is fed into the input buffer as a "newline" ('\n'). Next time you get to the scanf() it reads that newline, which of course isn't 'A'..'F', so it's rejected and you stay in the loop.

    Putting a getchar() after scanf() would solve that problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Thank you very much matsp.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I recommend writing
    char Scenario[7]="ABCDEF";
    ...as...
    char Scenario[]="ABCDEF";
    To avoid subtle problems to determine the correct length and if you accidentally increase the length when forgetting to increase the size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. Initialzing a 2D character array
    By mike_g in forum C Programming
    Replies: 3
    Last Post: 08-16-2007, 02:05 AM
  4. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM

Tags for this Thread