Need help with "if else" program

Oct 4, 2016 at 7:04pm
Need a little help with this program, any help or advice would be appreciated! Here is the question it asks..

Suppose that sale and bonus are double variables. Write an if.
..else statement
that assigns a value to bonus as follows: If sale is greater than $20,000, the value assigned to
bonus is 0.10; If sale is greater than $10,000 and less than or equal to $20,000, the value
assigned to bonus is 0.05; otherwise, the value assigned to bonus is 0. Print out the sale and
bonus values

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
#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	int x = 10;
	if (x > 20000)
	{
		printf_s("x is .10!\n");
	}
	else
	{
		printf_s("x is 0.10!\n"); // this statement will not be executed
	}

	x = 1;
	if (10000 < x <= 20000)
	{
		printf_s("x is 0.05!\n"); // this statement will not be executed
	}
	else
	{
		printf_s("x is not 0.05!\n");
	}

	return 0;
}
Oct 4, 2016 at 7:12pm
I think the thing that is messing with me is that the only applications I've made so far is where the user inputted a value. Is that not the case for this problem?
Oct 4, 2016 at 7:33pm
alright, this is where I'm at now..
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
#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	double sale;
	double bonus;

	if (sale > 20000)
	{
		cout << "The bonus is 0.10" << endl;
	}
	else if (10000 < sale <= 20000)
	{
		cout << "The bonus is 0.05" << endl;
	}
	else if (sale == 30)
	{
		cout << "The bonus is 30" << endl;
	}
	else
	{
		cout << "Value of sale is not matching" << endl;
	}
	cout << "Exact value of a is : " << sale << endl;

	return 0;
}


Oct 4, 2016 at 8:55pm
Line 11: sale is an uninitialized variable (garbage).

Line 18: You can't write an if statement like that. That statement evaluates as:
1
2
  if ( (10000 < sale) <= 20000)
       ^^^^^^^^^^^^^ evaluates to true or false


Is that not the case for this problem?

The problem description doesn't specify where the value of sale comes from. You can input it. Or you can set it as a constant.
Last edited on Oct 4, 2016 at 8:58pm
Topic archived. No new replies allowed.