trying to run a series of ifs

hidy ho all.

I'm having a little trouble with a program I'm writing for school. Before we go any further, I'm not looking for easy answers or someone to write the program for me.

now that we got that out of the way one of the inputs is giving me a weird answer.

the program is:
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>            
#include <fstream>            
#include <iomanip>
#include <string>

using namespace std;
int main()
{

    double inp;

    cout << "Please type the amount ordered" << endl;
    cin >> inp;

    if (inp >0 && inp <=9){
        cout << "The Cost is " << inp*99 << endl;}

 	if (inp >10 && inp <=19);{
        cout << "The Cost is " << inp*99*(1 - 0.1) << endl;}

    if (inp >20 && inp <=29){
        cout << "The Cost is " << inp*99*(1 - 0.2) << endl;}

    if (inp >30 && inp <=39){
        cout << "The Cost is " << inp*99*(1 - 0.3) << endl;}

    if (inp >40 && inp <=59){
        cout << "The Cost is " << inp*99*(1 - 0.4) << endl;}

    if (inp >60 && inp <=99){
        cout << "The Cost is " << inp*99*(1 - 0.5) << endl;}

    if (inp >100 && inp <=200){
        cout << "The Cost is " << inp*99*(1 - 0.6) << endl;}

    if (inp >200){
        cout << "Invalid input, input greater then 200" << endl;}

return 0;
}

when I input 30 it gives me a blank output:

Please input the amount ordered
30
press any key to end the program...

why is it doing that? and how can i fix it?

edit: it does compile just fine, no errors
Last edited on
Check the conditions for your third if and your fourth if. Do you see any values for which they wouldn't be true?

The same goes for all your ifs. :(

-Albatross
Last edited on
In the future, please post using code tags...it makes your code much easier to read.

As Albatross points out, you have a logical flaw in the construction of your if statements.

To find it, ask yourself this question: when you type in 30, which if block do you expect it to enter? I am guessing you expect it to execute here:

1
2
3
if (inp >30 && inp <=39){
    cout << "The Cost is " << inp*99*(1 - 0.3) << endl;
}


Does 30 satisfy the comparisons on both sides of your && (AND)?
I'm sorry, i'm not sure that i see the problem you guys are.

with:

1
2
3
if (inp >30 && inp <=39){
    cout << "The Cost is " << inp*99*(1 - 0.3) << endl;
}


doesn't 30 fall into that area? or should it say something like:

1
2
3
if (inp >=30 && inp <=39){
    cout << "The Cost is " << inp*99*(1 - 0.3) << endl;
}



also, srry. first post, i think i found the code button
Last edited on
Is 30 greater than 30?

(no, it's not, so you need it to be if (inp >= 30 && inp <= 39), and similarly for the other ifs)
Your second code snippet is correct.

Your original problem is that, for instance, 30 is NOT greater than 30.

Your code looks like:

if( inp > 30 && inp <=39)

If inp = 30, you get:

1
2
    FALSE        TRUE
if(30 > 30 && 30 <= 39)


Since the left side is FALSE, the entire thing is false, and you don't enter your if block.

You need to apply your corrected logic to all the other if blocks as well.
awesome. thanks all!
Topic archived. No new replies allowed.