# Using switch/case in C

• (2 Pages)
• 1
• 2

## 16 Replies - 3949 Views - Last Post: 07 November 2010 - 04:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=198716&amp;s=c96b95b8a538cec34adccdb5f4c56578&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

# Using switch/case in C

Posted 06 November 2010 - 12:33 PM

```#include<stdio.h>
#include<conio.h>
#include<math.h>
float j,k,l;
int i,choice;
void main()
{
printf("enter 1 for addition 2 for subtraction 3 for multiplication and 4 for division");
scanf("%d",&i);
i=choice;
if(choice==1)
{
printf("enter two numbers");
scanf("%d",&j,&k);
}
else if(choice==2)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
else if(choice==3)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
else if(choice==4)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
switch(choice)
{
case 1: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j+k;
printf("\nthe addition of %f and %f is: %f ",j,k,l);
break;
case 2: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j-k;
printf("\nthe subtraction of %f and %f is: %f",j,k,l);
break;
case 3: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j*k;
printf("\nthe multiplication of %f and %f is: %f",j,k,l);
break;
case 4: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j/k;
printf("\nthe division of %f and %f is: %f",j,k,l);
break;
default:printf("\ninvalid number");
}
getch();
}

```

Is This A Good Question/Topic? 0

## Replies To: Using switch/case in C

### #2 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Using switch/case in C

Posted 06 November 2010 - 12:54 PM

Think about what your doing here, what do you think the value of choice is at this point.
```i = choice;
```

### #3 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:03 PM

saurav_22, on 06 November 2010 - 11:33 AM, said:

```#include<stdio.h>
#include<conio.h>
#include<math.h>
float j,k,l;
int i,choice;
void main()
{
printf("enter 1 for addition 2 for subtraction 3 for multiplication and 4 for division");
scanf("%d",&i);
i=choice;
if(choice==1)
{
printf("enter two numbers");
scanf("%d",&j,&k);
}
else if(choice==2)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
else if(choice==3)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
else if(choice==4)
{
printf("enter two numbers");
scanf("%d%d",&j,&k);
}
switch(choice)
{
case 1: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j+k;
printf("\nthe addition of %f and %f is: %f ",j,k,l);
break;
case 2: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j-k;
printf("\nthe subtraction of %f and %f is: %f",j,k,l);
break;
case 3: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j*k;
printf("\nthe multiplication of %f and %f is: %f",j,k,l);
break;
case 4: printf("enter two numbers");
scanf("%f%f",&j,&k);
l=j/k;
printf("\nthe division of %f and %f is: %f",j,k,l);
break;
default:printf("\ninvalid number");
}
getch();
}

```

stayscrisp: the value of i will we the the value tht we will enter from the keyboard and the same value will be assigned to choice.
hey can you come online on gtalk if you dont mind there we can chat better and discuss this topic

### #4 Djabby

Reputation: 37
• Posts: 131
• Joined: 02-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:06 PM

```i=choice;

```

That will not assign i to choice! i gets the value of choice. stayscrisp is totally right here.

### #5 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Using switch/case in C

Posted 06 November 2010 - 01:06 PM

I get what you are attempting to do with i but if you step through your program you will see where the problem is. Think logically about what you have done wrong here.

You should keep the discussion in the forums not gtalk.

This post has been edited by stayscrisp: 06 November 2010 - 01:07 PM

### #6 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:11 PM

Djabby, on 06 November 2010 - 12:06 PM, said:

```i=choice;

```

That will not assign i to choice! i gets the value of choice. stayscrisp is totally right here.

so dj abby wht i will have to do here
using i directly in switch will it work?

### #7 Djabby

Reputation: 37
• Posts: 131
• Joined: 02-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:14 PM

Just try: choice = i; and you are done! Very simple.

Edit:
Quite interesting. Here you haven't had the error:
http://www.dreaminco...-c-programming/ Why did you change that?

This post has been edited by Djabby: 06 November 2010 - 01:16 PM

### #8 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:17 PM

Djabby, on 06 November 2010 - 12:14 PM, said:

Just try: choice = i; and you are done! Very simple.

thanks dj abby u r right it works
thanks again bro

### #9 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:25 PM

```#include <stdio.h>
#define TRUE 1
void clear_buffer()
{
int ch;
while( ( ch = getchar() ) != '\n' && ch != EOF );
}
int main()
{
unsigned int ch;
while( TRUE )
{
printf("\nsingle char please - " );
ch = getchar();
clear_buffer();
printf("%c - %d", ch, ch );
}
}
```

this is a program to get ascii value of a character
is there any other process to do the same task abby

• Saucy!

Reputation: 6248
• Posts: 24,014
• Joined: 23-August 08

## Re: Using switch/case in C

Posted 06 November 2010 - 01:31 PM

Please do NOT give your topics a title that matches the name of the forum; give it a title that describes the question being asked. We KNOW it's either a C or C++ Programming question, it's in the C/C++ Forum!!!

Topic title changed.

### #11 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:31 PM

hey dj abby can u please tell can i use i directly without assigning it to anybody what do you think

### #12 Djabby

Reputation: 37
• Posts: 131
• Joined: 02-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:38 PM

Please write full sentences, with full english words. And don't call me dj abby.

Yes, of course you can use i directly.

### #13 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 01:54 PM

Djabby, on 06 November 2010 - 12:38 PM, said:

Please write full sentences, with full english words. And don't call me dj abby.

Yes, of course you can use i directly.

so i will call you abby happy!
well can you please help me on the new program that i posted on ascii value though it run correctly but i want to know is there any other way to do the same program.

### #14 saurav_22

Reputation: -4
• Posts: 17
• Joined: 04-November 10

## Re: Using switch/case in C

Posted 06 November 2010 - 02:02 PM

stayscrisp can you tell me how can i do a program to know the leap years in a century that means in a range
i will wait for you reply

### #15 no2pencil

• Professor Snuggly Pants

Reputation: 6727
• Posts: 31,157
• Joined: 10-May 07

## Re: Using switch/case in C

Posted 06 November 2010 - 06:31 PM

saurav_22, on 06 November 2010 - 03:02 PM, said:

how can i do a program to know the leap years in a century that means in a range
i will wait for you reply

Rather than waiting for a reply, why not check the C/C++ tutorials & snippets? If you think you are the 1st person to ever have that assignment you are dreaming.

Use the resources available to you, to get a start on your project rather than waiting for a reply from one member to do your work for you.