8 Replies - 808 Views - Last Post: 19 April 2013 - 12:08 PM Rate Topic: -----

#1 TFizz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 13

switch case statement keeps going straight to default option

Posted 19 April 2013 - 12:10 AM

Hi everyone, iv written this code which reads in a kelvin degrees temperature then gives you the option to convert it to either Celsius or Fahrenheit, However my switch case statement for the convert function keeps going straight to the default option and I can't figure out why. I've tried searching around but I can't seem to find an answer specific enough. Can someone please help me? Here is the code:

#include <stdio.h>

/*Gets the temperature input from the user*/
int getInput()
{

	int input;
	
	printf("Enter temperature in Kelvin degrees: ");
	scanf("%d", &input);
	
	return(input);
}

/*Converts the input into a Celsius or Fahrenheit value*/
void convertInput(int input)
{

	int celsius, fahrenheit;
	char input2;
	
	celsius = input - 273;
	fahrenheit = 9 * celsius / 5 + 32; 
	
	
	printf("Press 'c' to convert to Celsius\n");
	printf("Press 'f' to convert to Fahrenheit\n");
	scanf("%c%*c", &input2);
	
	switch(input2)
	{
		case 'c':
		case 'C':
		printf("Value in Celsius is: %d", celsius);
		break;
		case 'f':
		case 'F':
		printf("Value in Farenheit is: %d", fahrenheit);
		default:
		printf("You haven't selected a valid option");
	}
	
		
	return;
}

/*Main calls functions*/
int main()
{
	int input;
	
	input = getInput();
	convertInput(input);
	
	return(0);
}

This post has been edited by jimblumberg: 19 April 2013 - 07:54 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: switch case statement keeps going straight to default option

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 01:31 AM

Please use code tags. You don't have a break statement after this line...

printf("Value in Celsius is: %d", celsius);


So your default case would be executed if "case 'f':" or "case 'F':" were true.

This post has been edited by Aphex19: 19 April 2013 - 01:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 Rahul69  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 34
  • Joined: 10-October 12

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 09:02 AM

Looks like u are facing scanf buffer problems, and u are really facing those ! :smartass:
You used this code:
 scanf("%c%*c", &input2);

but if u are going to use %*c to clear buffer, then use it in previous scanf i.e this one:
 scanf("%d", &input);


or Alternatively u can use fflush() function before scanning input2 i.e.
fflush(stdin);
scanf(%c,&input2);

Hope it helps!
:bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,384
  • Joined: 25-December 09

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 09:07 AM

Quote

or Alternatively u can use fflush() function before scanning input2 i.e.


No you can't use fflush() on an input stream, that leads to undefined behavior. The fflush() function is only defined to be used for output streams.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,634
  • Joined: 16-October 07

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 09:24 AM

Make sure you have what you think you have:
scanf("%c%*c", &input2);
...
printf("%c is not a valid option", input2);



Don't do that wonky %*c thing with scanf. Rather, simply continue to ask until they give you something useful. You have a getInput function for one thing. Try it with another:

/* they don't leave until then enter a valid option */
char getMenuSelection(char *validOptions) { /* your code here */ }
int getCelsius(int n){ /* your code here */ }
int getFahrenheit(int n){ /* your code here */ }

void convertInput(int input) {
	printf("Press 'c' to convert to Celsius\n");
	printf("Press 'f' to convert to Fahrenheit\n");
	
	switch(getMenuSelection("cCfF")) {
		case 'c':
		case 'C':
			printf("Value in Celsius is: %d", getCelsius(input));
			break;
		case 'f':
		case 'F':
			printf("Value in Farenheit is: %d", getFahrenheit(input));
			break; /* you forgot a break here */
		default:
			printf("If you did getMenuSelection correctly, you will never get here");
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 TFizz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 13

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 11:18 AM

Thanks everyone for the help! It seems my problem was solved by adding in the %*c buffer in the first scanf line but why do I need to do this? Could someone please explain why this is necessary so I don't make the same mistake again? Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,384
  • Joined: 25-December 09

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 11:38 AM

Quote

Thanks everyone for the help! It seems my problem was solved by adding in the %*c buffer in the first scanf line but why do I need to do this? Could someone please explain why this is necessary so I don't make the same mistake again? Thank you!


This because the scanf() call leaves the end of line character (the enter key) in the input buffer. So you must extract this character before your next character entry. This is only necessary when dealing with character input, numeric input skips leading whitespace by default. Also note another way or removing this character is to have your character input skip the leading white space as well. You do this by putting a space in front of the character entry:

scanf(" %c", &yourCharacter); // Notice the leading space.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,634
  • Joined: 16-October 07

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 11:46 AM

It is absolutely NOT necessary. You have to be aware of what's going on. There are more characters there than you expect, mostly because of hitting enter. You should allow for that by checking for valid characters.

Quick test program to play with:
#include <stdio.h>

int main() {
	char escapeChar = 'X';
	char ch = 0;
	printf("Type and I will echo back to you\nType '%c' to exit\n", escapeChar);
	while(ch!=escapeChar) {
		scanf("%c", &ch);
		printf("You entered value %d, char '%c'\n", ch, ch);
	}
	
	return 0;
}



Sample output:
Type and I will echo back to you
Type 'X' to exit
Hi!
You entered value 72, char 'H'
You entered value 105, char 'i'
You entered value 33, char '!'
You entered value 10, char '
'
huh?
You entered value 104, char 'h'
You entered value 117, char 'u'
You entered value 104, char 'h'
You entered value 63, char '?'
You entered value 10, char '
'
'X'
You entered value 39, char '''
You entered value 88, char 'X'



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 TFizz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 13

Re: switch case statement keeps going straight to default option

Posted 19 April 2013 - 12:08 PM

Thanks baavgai I understand what your saying, it's just that I wrote the %*c after scanning a character in the second scanf() and left out the %*c for the first scanf() because it was only reading an integer and I didn't think this was necessary, but in doing so this caused my switch case statement to keep going to default until I went back to the first scanf() and put %*c after the %d I just want to understand why that happened?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1