...

View Full Version : switch case



chrismiceli
01-24-2003, 01:27 AM
i have this code


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void main() {
char c;
printf("Please make your treat selection:\n");
scanf("%s",&c);
c=toupper(c);
switch(c)
{
case 'H':
printf("Beverage\nThat will be $2\n");
break;
}
}

but if i changed 'H' to 'HE' my gcc compiler give me an error. could someone explain?

Spookster
01-24-2003, 01:44 AM
Probably because you are switching on a variable of type character which can only hold one character. You will probably want to use the String datatype.

chrismiceli
01-24-2003, 02:26 AM
edited original post. still errors.

codefox
01-24-2003, 05:12 AM
Well, switch case allows you to use only integers, and character (which again are processed as integers internally) datatypes. 'H' is a character and "HE" is a string, so it wont work. You have to use if (strcmp(somevar, "HE") == 0) to check if the somvar contains "HE" :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum