473,725 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statements with <?

2 New Member
I have a program where I ask a user to input their income and then it evaluates the
tax using switch statements. The program ends where I enter the income and fails to show me the tax.

Here's some of the code:

case 1: if (income < 1000) tax = income*.3;
printf("\nYou have this much: %lf in taxes", tax);
break;

Thanks
Oct 27 '07 #1
6 4302
oler1s
671 Recognized Expert Contributor
This program is very small in size. Post the entire thing.

Read up on switch statements. You should see that the integral value is evaluated in the switch(...) syntax, and the case statements deal with the various possibilities.

Here's a hint. You can consider a boolean an integral value. A < comparison is either true or false. False is considered to be 0 and 1 is considered to be true. So, what would you be putting in the switch input?
Oct 27 '07 #2
lumpybanana247
134 New Member
try this [i just compiled it and it works fine]

#include <iostream>
using namespace std;
double income=31320;
double tax;
int main()
{
if (income < 1000){ tax = income*.3;}
else if (income < 2000){ tax = income*.4;}
else if (income < 3000){ tax = income*.5;}
else if (income > 3999){ tax = income*.9;}
printf("\nYou have this much: %lf in taxes", tax);
cin.get();
return 0;
}
Oct 27 '07 #3
dmc333
2 New Member
I had income as the switch variable. I think I remember my instructor talking about
using an A variable. If A= even, then A is true, or A=odd , then A is false. Something like that. I know how to make this program with if statements, but I need to use switch. If someone could give me a link to an example similar to my program, that would be great. I tried google, but I found nothing using something similar. And nothing in my book either.

Here is what tried:

#include <stdio.h>

int main(void)
{

int income;
double tax;

printf("Please input your income here->");
scanf("%d", &income);

switch(income)
{
case 1: if (income < 1000) tax = income*.2;
printf("\nYou have this much: %lf in taxes", tax);
break;
case 2: if (income >= 1000 && income < 2000) tax = income*.3;
printf("\nYou have this much: %lf in" "taxes\n", tax);
break;
case 3: if (income >= 2000) tax = income*.4;
printf("\nYou have this much: %lf in taxes\n", tax);
break;
}
}

And my output:

Please input your income here->2100
It ends not getting the answer.
Oct 27 '07 #4
thenath24
10 New Member
I had income as the switch variable. I think I remember my instructor talking about
using an A variable. If A= even, then A is true, or A=odd , then A is false. Something like that. I know how to make this program with if statements, but I need to use switch. If someone could give me a link to an example similar to my program, that would be great. I tried google, but I found nothing using something similar. And nothing in my book either.

Here is what tried:

#include <stdio.h>

int main(void)
{

int income;
double tax;

printf("Please input your income here->");
scanf("%d", &income);

switch(income)
{
case 1: if (income < 1000) tax = income*.2;
printf("\nYou have this much: %lf in taxes", tax);
break;
case 2: if (income >= 1000 && income < 2000) tax = income*.3;
printf("\nYou have this much: %lf in" "taxes\n", tax);
break;
case 3: if (income >= 2000) tax = income*.4;
printf("\nYou have this much: %lf in taxes\n", tax);
break;
}
}

And my output:

Please input your income here->2100
It ends not getting the answer.
Reply:

The program is not giving you any output because your variable does not meet one of your case conditions.

For example for you program to evaluate a salary under 1000 at the moment you would have to enter 1 when asked to enter your income, clearly this is not what you are trying to achieve.

In a switch case statement the variable you provide (in this case income) is what dictates which case is processed.

There may be a better way of doing it but off the top of my head something like this would work;

if(income <1000)
{
//set flag
dummyInt = 1;
}

//code to calcualte tax

switch(income)
{
case 1:
printf("\nYou have this much: %f in taxes", tax);
break;

}
Oct 27 '07 #5
oler1s
671 Recognized Expert Contributor
Asides: a reminder to use CODE tags around portions of code. It makes the code readable, which in turn allows us to help you. If you present code in an unreadable mess here, we will simply ignore your post, because we will not attempt to read unreadable code.

lumpybanana: You miss the point of dmc's assignment. The idea is to use switch statements, which obviously your code doesn't. Besides, do not spoon feed code to other people. That defeats the purpose of your presence here.

thenath: You made crippling mistakes in your attempt, but your idea is far more complicated than it should be.

Now, on to dmc's issue at hand. Dmc, there is no direct example here. This isn't a syntax question anymore. Well it is, but really, it's a logic game, and there's no point in us giving you the answer. So, as frustrating as it might be, we'll push you to figure out the answer on your own. My first reply had a major hint. I'll explain further.

I had income as the switch variable.
Ok. Now consider the values income can be. It can be 1. It can be 20. It can be 1000. It can be 500000. It can be anything. What you want to do is determine if income is one of three ranges (<1000, 1000-2000, and >=2000), which is equivalent to saying you have three different cases. Do you see where I'm going with this? Testing the input doesn't make sense, as you don't actually care what value the input is. Let's say the income is less than 1000. Does it matter if it is 999 vs 998? No. So obviously, testing input directly gets you nothing. If you followed what I said above, it's obvious your three cases are the three ranges income can fall into. It's easy to write them as if statements, but the question how can you represent three if comparisons as switch statements.

Now, look at my first reply. Consider 5 < 10. What is the value of 5 < 10? It's true. What about 7 < 5? False. What about representing true and false as integral values. Remember true and false can be thought of as 1 and 0. And also recall that the switch variable must be an integral value. Now, what could you possible use for the switch, given that you want to check for those three cases?
Oct 28 '07 #6
sridharvaradarajan
1 New Member
void main()
{
int income,choice = 0;
double tax;

printf("1.Tax calculation \n 2.addition ");
printf("\n enter the choice:");
scanf("%d",&cho ice);

switch(choice)
{
case 1:
printf("\nenter the income:");
scanf("%d",&inc ome);
if (income < 1000)
tax = income * 0.3;
printf("\nYou have this much: %lf in taxes", tax);
break;
case 2:
printf("\naddit ion");
default:
printf("\nthe nu,ber u have entered is invalid");
}
}

U just use this program it will work fine.. Because switch statements wont allo to evaluate any expression in case conditions. it needs only constants.
Nov 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
10906
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
10
9583
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
17
2818
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can anybody point me to the innards of it please? Sorry if this is OT on clc and more relevant to comp.compilers. I am confused here too.
10
12320
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a program to demonstrate how a switch without breaks behaves vs. how I expected it to behave. The code includes: (1) a switch statement with breaks (2) the if/else statements that have the same results as (1) (3) a switch statement without breaks...
13
4527
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only known at run time. Therefore a lot of template based trick isn't too useful. Considering datafile float x(3) 3.5, 2.5, 8.9 double y(3) 2.7, -2.3, 1.2 int z(3) 5, 2, 3
1
2528
by: CyberSoftHari | last post by:
How can i write switch case statement like Case <= 20 : I tried switch (e.X) { case is <=100: // My Statements break; case is >100:
5
1769
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the alert comes up. Why? I checked the following watch expressions at the blah blah point. id = 5843 reportType = "MANDATE"
11
4963
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the statements in switch #2 enter a 3rd switch. I don't receive any compile errors except whenever I attempt to add default switches to switches 2 or 3.
13
11826
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.