I need assistance correcting my program

Enter name and income.If income<1000 then no tax will be charged but If the income is >1000 but less than 2000 but less than 3000 the tax is 7% of the income.If the income is more than 3000 then tax is 9% Output the name,income,tax amount and income after tax


#include <iostream>
using namespace std;
int main ()
{
char name;
double income;
double tax_amount;
double income_after_tax;
double money;
double tax;


cout<< "Please enter name: " ;
cin>> name;

cout<< "Please enter income: " ;
cin>> income;



if (income<1000)
cout<< "No tax will be charged " ;




else if (income>1000 && income<2000)
cout<< "The income is: " <<income <<std::endl;

cout<< "The tax amount is: " <<tax_amount <<std::endl;
tax_amount= income * 0.05;
cout<< "The income after tax is: " <<income_after_tax <<std::endl;

income_after_tax= income - tax;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream> 
using namespace std;
int main ()
{
char name;
double income;
double tax_amount; 
double income_after_tax;
double money;
double tax;


cout<< "Please enter name: " ; 
cin>> name; 

cout<< "Please enter income: " ; 
cin>> income; 



if (income<1000) 
cout<< "No tax will be charged " ; 




else if (income>1000 && income<2000) // Missing curly brackets for the else if statement

// Don't need std:: for endl because you have using namespace std;
cout<< "The income is: " <<income <<std::endl;

cout<< "The tax amount is: " <<tax_amount <<std::endl;  
tax_amount= income * 0.05; // Should be above the cout statement so you can print the tax_amount.

cout<< "The income after tax is: " <<income_after_tax <<std::endl;

income_after_tax= income - tax; // Above the cout statement so you can print income_after_tax. Tax also never given a value.
Last edited on
Hello Iamyou,

In addition to what Hengr has said I see a problem with your first line of code char name;. It would work better as std::string name; which neds the include file <string>.

The line cin >> name; will only retrieve one character of what the user types leaving whatever is left in the cin input buffer. So the next call to cin will try to put the next character of the name into a numeric variable and cin will fail leaving the rest of the program to run without the proper information in "income".

A better way to get a name from the user is to use std::getline i.e., std::getline(std::cin, name); and "getline" requires the "string" header file.

By the way the variable "money" is never used. Gives a warning on compile, but not a problem.

The std:: qualifier I use is just habit. It does not make any difference if you leave it.

Hope that helps,

Andy

UPDATE:

I also noticed that the line income_after_tax= income - tax; is using the wrong variable tax. It should be tax_amount. Leaving you with money and tax as unused variables.
Last edited on
Topic archived. No new replies allowed.