473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

switch statements with <?

2
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 4254
oler1s
671 Expert 512MB
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 100+
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
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
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 Expert 512MB
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
void main()
{
int income,choice = 0;
double tax;

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

switch(choice)
{
case 1:
printf("\nenter the income:");
scanf("%d",&income);
if (income < 1000)
tax = income * 0.3;
printf("\nYou have this much: %lf in taxes", tax);
break;
case 2:
printf("\naddition");
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
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...
10
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 ...
17
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...
10
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...
13
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...
1
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 ...
5
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...
11
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...
13
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.