C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2008, 07:51 PM   #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.
magda3227 is offline   Reply With Quote
Old 07-08-2008, 08:06 PM   #2
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
Quote:
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)
Cactus_Hugger is offline   Reply With Quote
Old 07-09-2008, 06:19 AM   #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...
magda3227 is offline   Reply With Quote
Old 07-09-2008, 06:30 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 07-09-2008, 06:43 AM   #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?
magda3227 is offline   Reply With Quote
Old 07-09-2008, 06:48 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 07-09-2008, 06:58 AM   #7
Registered User
 
Join Date: Jun 2008
Posts: 45
Thank you very much matsp.
magda3227 is offline   Reply With Quote
Old 07-09-2008, 08:36 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
character, compare, string

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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