8 Replies - 374 Views - Last Post: 18 February 2013 - 06:48 PM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Reading in char[] and error checking

Posted 18 February 2013 - 10:48 AM

I am writing a program that processes income tax.


The user is required to enter the filing status as a 2 character array. I am required to error check their entry.


error checking for filing status only works for "S" (single). I am using a switch to check the status. I am NOT allowed to use cin and cout. If I enter anything other than "S", it says have entered an incorrect status.

printf("\nWhat is the filing status? Enter S, MJ, MS, or SH:");
		gets(status);
		
		status[0] = toupper(status[0]);
		status[1] = toupper(status[1]);

switch(*status)
		{
		case 'S':
			y=1;
			sAccum++;
			break;
		case 'MJ':
			y=2;
			mjAccum++;
			break;
		case 'MS':
			y=3;
			msAccum++;
			break;
		case 'SH':
			y=4;
			shAccum++;
			break;
		default:
			{printf("You have entered an incorrect filing status.");
			printf("\nWhat is the filing status? Enter S, MJ, MS, or SH:");
			gets(status);
			status[0] = toupper(status[0]);
			status[1] = toupper(status[1]);
			}
		}//END SWITCH




Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading in char[] and error checking

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:09 AM

First never use gets() or any other function that doesn't limit the number of characters you can accept to retrieve a C-string.

Next a character constant can only contain one character so this is not allowed: 'MJ'. The single quotes denote a character constant. The double quotes denote a string constant. But if you do switch to using the string constant you can't compare C-strings with the comparison operator==. You will need something like strcmp() to compare C-strings.

Also how did you define you variable status?

I am NOT allowed to use cin and cout. 

Since you seem to be writing C code you should probably stick with functions defined in stdio.h to retrieve the data, like fgets().

Lastly this is wrong:

switch(*status)

Switch statements require the use of integer values, you can't switch on a character string. Trying to use the switch statement using a pointer value will not usually accomplish what you want.


Jim

This post has been edited by jimblumberg: 18 February 2013 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:12 AM

Thanks Jim! Here is how I defined status:

char status[2];


I have to use gets() as per my instructor. Maybe you remember my post a few weeks ago about his crazy requirements LOL.
Was This Post Helpful? 0
  • +
  • -

#4 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 10-August 12

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:15 AM

I think instead of using gets() you should use: status = getchar();

or even: scanf("%c", &status);

Nvm, just saw you post that you had to use gets() sorry lol

This post has been edited by graFight: 18 February 2013 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:19 AM

Thank you guys!

Jim, I also tried this to error check:
//Error check status entry
		/*if(*status !='S' || *status!='MJ' || *status!='MS' || *status!= 'SH')
		{
			printf("You have entered an incorrect filing status.");
			printf("\nWhat is the filing status? Enter S, MJ, MS, or SH:");
			gets(status);
			status[0] = toupper(status[0]);
			status[1] = toupper(status[1]);
	}*/


So, after what you said, I am assuming it didn't work because I am checking 2 characters instead of one?


Here is the example I am supposed to follow:
 char   a_string[40];  
 gets(a_string);


This post has been edited by synlight: 18 February 2013 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:23 AM

Then since it has been several weeks you should have brought this issue to his/her attention. To continue doing things wrong without questioning the instructor does you no benefit. Until you start trying to actually learn the proper methods will only server to cripple and confuse you more. Speak up to save yourself from needing to unlearn these bad habits, and to learn the proper way to program. If talking to your instructor doesn't do any good I suggest you go to the people in charge. If your parents or anybody else is helping you pay for this non-education you should also bring them into the discussion.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:29 AM

View Postjimblumberg, on 18 February 2013 - 01:23 PM, said:

Then since it has been several weeks you should have brought this issue to his/her attention. To continue doing things wrong without questioning the instructor does you no benefit. Until you start trying to actually learn the proper methods will only server to cripple and confuse you more. Speak up to save yourself from needing to unlearn these bad habits, and to learn the proper way to program. If talking to your instructor doesn't do any good I suggest you go to the people in charge. If your parents or anybody else is helping you pay for this non-education you should also bring them into the discussion.

Jim


I have discussed this with him in a previous Java class. He doesn't care. He's been teaching for a million years and if you don't do it his way, your program gets returned for you to fix and resubmit with a possible grade of no more than 80. This happened to me on my first program this semester. I wanted to input a char[], pass it to a function, and have the function return a string. Got my program returned, even though I went above and beyond what was asked for.

I have left my opinions in my reviews of his classes that we complete mid semester. I have approached the department head (during the Java class) and was told that he can teach his class how he chooses to teach it.

I am paying for my education myself.

I really appreciate your opinion, I mean that. I have a great deal of respect for you, and you may not recognize me but you have been helping me out for IDK, at least a year with various questions.

Unfortunately, I have to take this class to graduate in May. His antiquated methods will be a threat to my 4.0 at this rate.

**edited because apparently I can't spell.

This post has been edited by synlight: 18 February 2013 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Reading in char[] and error checking

Posted 18 February 2013 - 11:34 AM

Quote

So, after what you said, I am assuming it didn't work because I am checking 2 characters instead of one?

Yes you can't compare a C-string with comparison operators, you need to use strcmp()!

You also can't use a C-string in a switch statement.


Edit: Then consider going higher up. With instruction like this your degree will be severely degraded, in my opinion.


Jim

This post has been edited by jimblumberg: 18 February 2013 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Reading in char[] and error checking

Posted 18 February 2013 - 06:48 PM

The size of the array is incorrect because a null character will be added at the end of the string.

char status[2];


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1