Switch function

Not getting an output?

Page 1 of 1

6 Replies - 31310 Views - Last Post: 15 October 2006 - 05:12 AM Rate Topic: -----

#1 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Switch function

Posted 14 October 2006 - 06:29 PM

This is my 1st switch program, and I am unable to get it to give me an output. I know with the switch it need to call the char(for this program). However when I try to declair my variable I keep getting errors. I'm not sure what I am doing wrong. Any guidance would be appreciated..My code is listed below.

#include <stdio.h>
#include <conio.h>

int main()
{
	char marcode single='s', married='m',widowed='w',divorced='d';

	
	printf("Enter a marital code: ");
	scanf("%c",&marcode);
	

	switch (marcode)
{
	case 1: 
		printf("Single%c");
		break;
	case 2:
		printf("Married%c");
		break;
	case 3:
		printf("widowed%c");
		break;
	case 4:
		printf("divorced%c");
		break;
	if (marcode)
		printf("\nAn invalid code was entered.\n");
		break;
	getch ();
}


return 0;
}

This post has been edited by jayman9: 14 October 2006 - 08:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Switch function

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Switch function

Posted 14 October 2006 - 07:09 PM

You've got the concept of vaiables wrong. A char holds one letter not a string. For strings, you need to use arrays although in your programs case it is not required.
Change
char marcode single='s', married='m',widowed='w',divorced='d'


To this
char marcode;


And Similarly do the changes in your switch statement:
Case 'm'
//Code here


Also see the printfs. There are some changes for that also.

#include <stdio.h>
#include <conio.h>

int main()
{
	char marcode;


	printf("Enter a marital code: ");
	scanf("%c",&marcode);


	switch (marcode)
	{
		case 's':
			printf("Single %c",marcode);
			break;
		case 'm':
			printf("Married %c",marcode);
			break;
		case 'w':
			printf("widowed %c",marcode);
			break;
		case 'd':
			printf("divorced %c",marcode);
			break;
		default:
			printf("\nAn invalid code was entered.\n");
			break;
	}

	getch ();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Switch function

Posted 14 October 2006 - 07:10 PM

There are a few issues to be resolved here...

1. To properly declare a variable named marcode, you will need a comma between the declaration of marcode and the declaration of single so the compiler realizes there are two separate declarations of type char.
2. You prompt the user for a marriage code, and are placing that imput into a variable of type char. From that, I'm assuming you are expecting the user to enter one of the for letters = s,m,w,d. You are then switching on the value of marcode, which should be one of those values, but your case statements are using integers...they should be using characters.
3. You have no deafuly case statement - not a requirement, simply a wise precaution.
4. You are printing out a word, and then indicate that you will be printint out an addtional character, but you do not supply the character.
5. Your if statement is merely checking that marcode is true...as long as theuser has entered something, this will evaluate to true, and throw the statement.

Below is a simplified version that may be an example of what you are trying to achieve:
#include <stdio.h>

int main()
{
char marcode;


printf("Enter a marital code: ");
scanf("%c",&marcode);

switch (marcode)
{
case 's':
printf("Single\n");
break;
case 'm':
printf("Married\n");
break;
case 'w':
printf("widowed\n");
break;
case 'd':
printf("divorced\n");
break;
default:
printf("\nAn invalid code was entered.\n");
break;
}


return 0;
}



EDIT: Slower typer than louisda16th! :)
Was This Post Helpful? 0
  • +
  • -

#4 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Switch function

Posted 14 October 2006 - 07:39 PM

Thanks for the help Louisda16th, Amadeus that help was great. I really confused with this. I forgot I had to have it so the input could be in caps as well as lower case. I am getting the outputs I need. here is how I have the case statements as I'm not sure if I can post the completed code. Please let me know if this is good code structure for this type of project.

	switch (marcode)
{
	case 's': case 'S':
		printf("single\n",marcode);
		break;
	case 'm': case 'M':
		printf("married\n",marcode);
		break;
	case 'w': case 'W':
		printf("widowed\n",marcode);
		break;
	case 'd': case 'D':
		printf("divorced\n",marcode);
		break;
	default:
		printf("\nAn invalid code was entered.\n");
		break;
	getch ();
}


Thanks again for the help :D

This post has been edited by jayman9: 14 October 2006 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Switch function

Posted 14 October 2006 - 08:52 PM

If you are going to ouput a value from a variable using printf, then you need a format specifier in you string literal to show placement of the value. You will need to fix the remaining printf statements in your case statements.
printf("single %c\n",marcode);


Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Switch function

Posted 14 October 2006 - 11:23 PM

To check for both cases, instead of checking whether the character is in either case, you can convert it to uppercase or lowercase and check only for one condition.

So you can use this:
	switch (toupper(marcode))
{
   case 'S':
		printf("single\n",marcode);
		break;
   case 'M':
		printf("married\n",marcode);
		break;
   case 'W':
		printf("widowed\n",marcode);
		break;
   case 'D':
		printf("divorced\n",marcode);
		break;
	default:
		printf("\nAn invalid code was entered.\n");
}


This will improve readability and a little bit of speed as the number of conditions are reduced by half.
toupper() and tolower() requires inclusion of ctype.h file.

Also remember that it's pointless to use a break statement after the default keyword. Since it's the last case, it will break out of the switch construct on it's own, so there's no need to explicitly specify that.
Was This Post Helpful? 0
  • +
  • -

#7 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Switch function

Posted 15 October 2006 - 05:12 AM

View Postborn2c0de, on 15 Oct, 2006 - 11:53 AM, said:

Also remember that it's pointless to use a break statement after the default keyword. Since it's the last case, it will break out of the switch construct on it's own, so there's no need to explicitly specify that.

Lol I didn't realise that. Nice one Sanchit!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1