Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,265 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,513 people online right now. Registration is fast and FREE... Join Now!




Switch function

 
Reply to this topicStart new topic

Switch function, Not getting an output?

jona431
14 Oct, 2006 - 05:29 PM
Post #1

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
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.

CODE

#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 Oct, 2006 - 07:47 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Switch Function
14 Oct, 2006 - 06:09 PM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
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
CODE

char marcode single='s', married='m',widowed='w',divorced='d'

To this
CODE

char marcode;

And Similarly do the changes in your switch statement:
CODE

Case 'm'
//Code here

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

CODE

#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;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch Function
14 Oct, 2006 - 06:10 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
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:
CODE

#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! smile.gif
User is offlineProfile CardPM
+Quote Post

jona431
RE: Switch Function
14 Oct, 2006 - 06:39 PM
Post #4

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
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.

CODE

    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 biggrin.gif

This post has been edited by jayman9: 14 Oct, 2006 - 07:49 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Switch Function
14 Oct, 2006 - 07:52 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
CODE

printf("single %c\n",marcode);

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Switch Function
14 Oct, 2006 - 10:23 PM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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:
CODE

    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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Switch Function
15 Oct, 2006 - 04:12 AM
Post #7

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE(born2c0de @ 15 Oct, 2006 - 11:53 AM) *

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!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month