Testing a character for Empty

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 696 Views - Last Post: 03 April 2013 - 11:30 AM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Testing a character for Empty

Posted 02 April 2013 - 10:23 AM

This seems like a very simple task, but I can't get it to work.

I have a discount code that is a single character. It can be A, B, C or empty.

I am using a getChar function to pull the character in, which is working properly.

/*********************************
* Gets and returns a Char  *
*********************************/
char getChar(char prompt[])
{
	char buffer[20];
	char code = ' ';

	printf("\n%s",prompt);
	fgets(buffer,20,stdin);
	code = buffer[0];

	return code;
}//END GETCHAR


After I get the character input, I am testing it for validity. The test works fine for A, B and C, but it's nor working properly for an empty character.

First question.. empty and NULL are different things, correct? My problem statement says: "must be A, B, C or blank".. so I should be testing for an empty character, I believe.

I know I can test a char array (string) for empty by testing the first element for '\0', but that isn't working to test my char.
if (testCode == '\0')
		validCode = true;


I also tried
	if (testCode == ' ')
		validCode = true;


and

if (testCode == NULL)
		validCode = true;


Here is the entire test function.. I get the proper results for any character I enter, but not for an empty char.

/*********************************
* Get discount code  *
*********************************/
char getCode()
{
   
    char testCode;
	bool validCode = false;
	testCode = getChar("\nPlease enter the discount code:");

	

	if (testCode == NULL)
		validCode = true;

	while(!validCode)
	{
		testCode = toupper(testCode);

		if(testCode =='A' || testCode == 'B' || testCode == 'C')
			validCode = true;
		else
			testCode = getChar("\nInvalid discount code. Please try again:");
	}//ENDWHILE
		

    return testCode;
}//END GETCODE



I'm obviously doing something wrong. I just don't know what it is.

Is This A Good Question/Topic? 0
  • +

Replies To: Testing a character for Empty

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 02 April 2013 - 10:37 AM

Why not just use getchar() instead of fgets() since you are only getting a single character?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Re: Testing a character for Empty

Posted 02 April 2013 - 10:42 AM

Like this?

char getChar(char prompt[])
{

	char code = ' ';
	printf("\n%s",prompt);
	code = getchar();

	return code;
}//END GETCHAR


It seems to work, haven't tested it empty yet.

I'll try it, hopefully my Professor won't dock me points. He wants us to use gets() for all input. Yesterday when he saw my code for getString, he thought I was pulling my input from a file, because I was using fgets. I explained to him that I was using it because fgets limits the amount of characters, and that you can use it to get keyboard input. His response was
"Oh. Well. I don't know anything about that. We don't worry about that in this class. Just make your buffer big enough."
Sigh.

Okay, it still doesn't work for an empty character. So.. since I am running the getChar function, and the user just presses ENTER if there is no discount code, shouldn't I be testing for '\0' ?

This post has been edited by synlight: 02 April 2013 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5747
  • View blog
  • Posts: 12,555
  • Joined: 16-October 07

Re: Testing a character for Empty

Posted 02 April 2013 - 10:47 AM

Again, trace it:
fgets(buffer, 20, stdin);
printf("buffer='%s'\n", buffer);



What do you see? What does it mean? You did a queer thing before with strlen(s)-1 that threw me and I nearly mentioned, but it does have a purpose.

Hint, the result of an fgets will never be strlen==0. Think about it.
Was This Post Helpful? 1
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Re: Testing a character for Empty

Posted 02 April 2013 - 10:48 AM

So I'm going to need you to come be my professor, Jim.

Or you, Baavgai.

When he was lecturing last night, he said we would be moving into dynamic arrays.

I was all excited, and I said "Oh! Like vectors?"

He said "I don't know about those. I just mean resizable arrays."

ETA;; Hang on Baav.. I'm thinking....

This post has been edited by synlight: 02 April 2013 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 02 April 2013 - 10:49 AM

No it should contain the end of line character '\n' and nothing else because you just pressed enter (the end of line).

Jim
Was This Post Helpful? 1
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 02 April 2013 - 10:57 AM

Quote

"Oh. Well. I don't know anything about that. We don't worry about that in this class. Just make your buffer big enough."


I think this instructor should be exposed for the fraud he is and disposed of non-humanely.

Quote

He said "I don't know about those. I just mean resizable arrays."

Too bad there is no such thing in C++ as re-sizable arrays.

Jim

This post has been edited by jimblumberg: 02 April 2013 - 10:58 AM

Was This Post Helpful? 1
  • +
  • -

#8 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Re: Testing a character for Empty

Posted 02 April 2013 - 11:02 AM

When I tested it.. like this:

char getChar(char prompt[])
{

	char code = ' ';
	printf("\n%s",prompt);
	code = getchar();

		printf("code='%c'\n", code);

	return code;
}//END GETCHAR


it prints as

code ='
' 


So.. it's a carriage return.. I don't really understand how a carriage return can be in a character.. but isn't it '\0'?

View Postjimblumberg, on 02 April 2013 - 12:49 PM, said:

No it should contain the end of line character '\n' and nothing else because you just pressed enter (the end of line).

Jim


Oh my god I'm an idiot. I only type \n a million times a freaking day.

Thanks Jim.

BTW.. I have a letter written that I'm sending to the head of the computer department, and to the President of the college when I'm done. I graduate 30 days from tomorrow. If I stir the pot right now, I'm scared he will mess with my grade.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 02 April 2013 - 11:03 AM

Quote

So.. it's a carriage return.. I don't really understand how a carriage return can be in a character.. but isn't it '\0'?

Because a carriage return is a character. No '\0' is the end of string character not a carriage return character '\n'. Try printing your character casted to an int.

 printf("code='%d'\n",int( code));


Jim
Was This Post Helpful? 0
  • +
  • -

#10 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Re: Testing a character for Empty

Posted 02 April 2013 - 11:06 AM

View Postbaavgai, on 02 April 2013 - 12:47 PM, said:

Again, trace it:
fgets(buffer, 20, stdin);
printf("buffer='%s'\n", buffer);



What do you see? What does it mean? You did a queer thing before with strlen(s)-1 that threw me and I nearly mentioned, but it does have a purpose.

Hint, the result of an fgets will never be strlen==0. Think about it.


Baav.. will strlen never ==0 because if the delimiter?

View Postjimblumberg, on 02 April 2013 - 01:03 PM, said:

Quote

So.. it's a carriage return.. I don't really understand how a carriage return can be in a character.. but isn't it '\0'?

Because a carriage return is a character. No '\0' is the end of string character not a carriage return character '\n'. Try printing your character casted to an int.

 printf("code='%d'\n",int( code));


Jim

WHAT?!? The carriage return came up as a 10. WHY?!? I don't understand!

Is it the ASCII value?

This post has been edited by synlight: 02 April 2013 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5747
  • View blog
  • Posts: 12,555
  • Joined: 16-October 07

Re: Testing a character for Empty

Posted 02 April 2013 - 11:08 AM

You've already answered this: '\n' is a character and fgets includes it. If you think about it, fgets ALWAYS includes it. So...
Was This Post Helpful? 0
  • +
  • -

#12 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 88
  • View blog
  • Posts: 580
  • Joined: 14-September 11

Re: Testing a character for Empty

Posted 02 April 2013 - 11:14 AM

View Postbaavgai, on 02 April 2013 - 01:08 PM, said:

You've already answered this: '\n' is a character and fgets includes it. If you think about it, fgets ALWAYS includes it. So...


SO the string will never be empty using fgets because \n is a character. SO I need to remove the newline if I want to test for an empty string?
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 02 April 2013 - 11:16 AM

Or consider both the end of line character and the end of string character as an "empty" string.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5747
  • View blog
  • Posts: 12,555
  • Joined: 16-October 07

Re: Testing a character for Empty

Posted 02 April 2013 - 11:25 AM

It's your string. Make it behave as you like. Check the last character. If isspace, make it '\0'. Then, all your tests could be consistent.

Indeed, you could write a trim that consistently rolls '\0' up the whitespace from the end.

Conversely, beware of the getchar solution; it will leave that '\n' waiting for you later.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Testing a character for Empty

Posted 03 April 2013 - 07:17 AM

Quote

If you think about it, fgets ALWAYS includes it. So...

Actually fgets() doesn't "always" include the end of line character. If you try to retrieve a string that is longer than the second parameter you will not have this end of line character. If the end of line character is not present it indicates there is still data left in the input buffer to process.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2