![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 45
| Character Array comparison 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);
}
Last edited by magda3227; 07-08-2008 at 07:53 PM. |
| magda3227 is offline | |
| | #2 | |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| Quote:
Code: char Scenario[] = "ABCDEF"; ... strchr(Scenario, UserScenario) 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) | |
| Cactus_Hugger is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 45
| Hm. It's still not working. I tried using Code: if(UserScenario >= 'A' && UserScenario <= 'F') 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);
}
}
|
| magda3227 is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: char Scenario[6]= "ABCDEF"; 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); -- 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. |
| matsp is offline | |
| | #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);
}
|
| magda3227 is offline | |
| | #6 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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. | |
| matsp is offline | |
| | #7 |
| Registered User Join Date: Jun 2008
Posts: 45
| Thank you very much matsp. |
| magda3227 is offline | |
| | #8 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| character, compare, string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using a character array in a switch question. | bajanElf | C Programming | 10 | 11-08-2008 08:06 AM |
| compare between a array of character and a pointer to an array of characters | steve1_rm | C Programming | 7 | 05-11-2008 10:57 AM |
| Initialzing a 2D character array | mike_g | C Programming | 3 | 08-16-2007 02:05 AM |
| Character Array - almost? works | voltson4 | C Programming | 3 | 03-04-2003 06:03 PM |
| Array of Character Arrays | Unregistered | C Programming | 3 | 02-09-2002 06:07 PM |