Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2003, 01:27 AM   PM User | #1
chrismiceli
Regular Coder

 
Join Date: Sep 2002
Location: Louisiana
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
chrismiceli is an unknown quantity at this point
switch case

i have this code
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?
__________________
DevEdge - MSDN - DevGuru

Last edited by chrismiceli; 01-24-2003 at 02:25 AM..
chrismiceli is offline   Reply With Quote
Old 01-24-2003, 01:44 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 01-24-2003, 02:26 AM   PM User | #3
chrismiceli
Regular Coder

 
Join Date: Sep 2002
Location: Louisiana
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
chrismiceli is an unknown quantity at this point
edited original post. still errors.
__________________
DevEdge - MSDN - DevGuru
chrismiceli is offline   Reply With Quote
Old 01-24-2003, 05:12 AM   PM User | #4
codefox
Regular Coder

 
Join Date: Nov 2002
Location: India
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
codefox is an unknown quantity at this point
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"
codefox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 PM.


Advertisement
Log in to turn off these ads.