Using switch/case in C

switch case

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3949 Views - Last Post: 07 November 2010 - 04:18 PM Rate Topic: -----

#1 saurav_22   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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();
}


please help switch case is not executing properly only default statement is

Is This A Good Question/Topic? 0
  • +

Replies To: Using switch/case in C

#2 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • 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;

Was This Post Helpful? 0
  • +
  • -

#3 saurav_22   User is offline

  • New D.I.C Head

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

Re: Using switch/case in C

Posted 06 November 2010 - 01:03 PM

View Postsaurav_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();
}


please help switch case is not executing properly only default statement is


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
Was This Post Helpful? 0
  • +
  • -

#4 Djabby   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 saurav_22   User is offline

  • New D.I.C Head

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

Re: Using switch/case in C

Posted 06 November 2010 - 01:11 PM

View PostDjabby, 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?
Was This Post Helpful? 0
  • +
  • -

#7 Djabby   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 saurav_22   User is offline

  • New D.I.C Head

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

Re: Using switch/case in C

Posted 06 November 2010 - 01:17 PM

View PostDjabby, 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
Was This Post Helpful? 0
  • +
  • -

#9 saurav_22   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 saurav_22   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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
in the earlier program please help bro
Was This Post Helpful? 0
  • +
  • -

#12 Djabby   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 saurav_22   User is offline

  • New D.I.C Head

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

Re: Using switch/case in C

Posted 06 November 2010 - 01:54 PM

View PostDjabby, 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.
Was This Post Helpful? -1
  • +
  • -

#14 saurav_22   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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
Was This Post Helpful? -2
  • +
  • -

#15 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

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

Re: Using switch/case in C

Posted 06 November 2010 - 06:31 PM

View Postsaurav_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.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2