If statement problem

Hey guys, newly registered but regular lurker, I am having a problem with a script I'm writting for uni, the question is as follows:
During the tax season, an accounting firm provides assistance to people who prepare their own tax returns. Their charges are as follows:
a. if a person has low income (<= 25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.
b. for others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.
Write a program that prompts the user to enter the hourly rate, total consulting time, and whether the person has low income. the program should output the billing information.

(Now the rest of the questions says that this all needs to be done by using functions but I am really rusty in C++ as I haven't done it for a while and wanted to try and make this program work another way before I even touch on my functions again because at the moment I cant even get my functions to output the value of an integer variable and its really killing my morale, so any help here guys would be much appreciated, PS not asking people for the answer just want some guiding points towards the right direction)

And this is what I have so far:
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
#include <iostream>
using namespace std;
int main()
{
double hRate = 0, cTime = 0, bAmount = 0; 
char iRate, l, h;
    
cout << "Please enter the hourly rate: ";
cin >> hRate;
cout << "Please enter total consulting time in minutes: ";
cin >> cTime;
cout << "Is the income rate low or high (l - Low | h - High): ";
cin >> iRate;


if (iRate == l && cTime > 30)
bAmount = (hRate * 40) * ((cTime / 60) - 30);
else if (iRate == h && cTime > 20)
bAmount = (hRate * 70) * ((cTime / 60) - 20);


cout << bAmount << endl;

system("pause");
return 0;
}
It looks like l and h are uninitialized.
You cannot compare char variables with numeric values in the way you are trying to.

First you need to validate the input from your three calls to cin to ensure valid characters were entered, and then you need to convert these to numeric values before using them.

If someone enters the character '1' when you call the line 'cin >> hRate;', the raw value of hRate will be the hexidecimal value for the character '1' which I believe is 49 (or 31 in hex). So when you convert it to an int its value will be 49 instead of 1 as you expect :)

Here is a list of asci values that you can use:
http://www.asciitable.com/
Last edited on
Hey filipe what do you mean by uninitialized?

And aatwo why would it hold its hex value in a double declared variable? Wouldn't it only accept numeric values in the same way say that an int variable would? Will changing it to something else like a float change that problem?
piee, filipe means that you did not give a character for l and h

you declared char l, h; but did not tell it what letter is suppose to be in that variable.

to make it run just simply type
char iRate, l('l'), h('h');

in place of

char iRate, l, h;
redstar33309 wanted to say:

char iRate, l('l'), h('h');

is what you have to type instead of

char iRate, l, h;


AND LEARN TO USE [ CODE ] ... [ / CODE ]
Thanks guys, you's have helped out loads, got everything to work but now its time to change it to functions :D
Topic archived. No new replies allowed.