nested if else statement problem

Aug 28, 2014 at 5:08am
everytime i run, the line "please enter a correct canadian province" keeps appearing even though i put a else before it. i guess im not doing it right.
can someone please help me with this problem

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
40
41
42
43
44
  //this program calculates the taxes of a given price per province in canada

#include <iostream>
#include <string>
using namespace std;

int main()

{

	float price, taxes;
	float quebec = 1.1498;
	float yukon = 1.05;

	cout << "enter the price of your item:" << " ";
	cin >> price;

	using std::string;
	string stateprovince;
	cout << "enter the province" << " ";
	cin >> stateprovince;





	if (stateprovince == "yukon")
			cout << "the price of your item including taxes is" << " " << price * yukon << "$" <<
			"\n";

	else if
		(stateprovince == "quebec")
			cout << "the price of your item including taxes is" << " " << price * quebec << "$" <<
			"\n"; 

	else (stateprovince != "quebec" || "yukon")
		cout << "please enter a correct canadian province";




	cin.get();
	return 0;
}
Aug 28, 2014 at 5:10am
line 36 is not an if statement, and should not have (stateprovince != "quebec" || "yukon").

ps: Even if it was an if statement, that statement is wrong.

pps: The $ symbol should be before the value. If you want to distinguish it from other $ using currencies, you can prepend the $ with C or CAD.
Last edited on Aug 28, 2014 at 5:14am
Aug 28, 2014 at 5:14am
But you get what im trying to do, what would be the right statement @Yay295
Aug 28, 2014 at 5:15am
The correct line would be else if ( stateprovince != "quebec" && stateprovince != "yukon" ). We all wish we could do what you wrote, but we can't. :)
Aug 28, 2014 at 5:20am
Thanks for your help but for some reason it still runs the "please enter a correct canadian province"
Aug 28, 2014 at 5:32am
I'm not sure why it'd doing that. This works fine for me. (slightly reformatted)

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
#include <iostream>
#include <string>

using std::cin;
using std::cout;

int main ( )
{
    float price, taxes;
    float quebec = 1.1498;
    float yukon = 1.05;

    cout << "Enter the price of your item: ";
    cin >> price;

    std::string stateprovince;
    cout << "Enter your province: ";
    cin >> stateprovince;

    if ( stateprovince == "yukon" )
        cout << "The price of your item including taxes is $" << price * yukon << '\n';

    else if ( stateprovince == "quebec" )
        cout << "The price of your item including taxes is $" << price * quebec << '\n'; 

    else  if ( stateprovince != "quebec" && stateprovince != "yukon" )
        cout << "Please enter a correct canadian province.";
}
Last edited on Aug 28, 2014 at 5:32am
Aug 28, 2014 at 5:43am
when you run it. and say you put 100 for price and quebec for province.
you don't get the "please enter a correct canadian province". because i copy pasted your exact version and i still get it when i enter a correct province. i think i have a problem with my compiler
Aug 28, 2014 at 5:52am
Are you perhaps entering "Yukon" instead of "yukon"? C++ is case sensitive.
Aug 28, 2014 at 6:25am
I wish that was the problem lol, im going to download a new program tomorrow and get back to you.
Thanks again
Aug 28, 2014 at 8:08pm
@Yay295 yup its working now. for some reason it was not working on visual studio but it worked on codeblocks. again thanks for your help
Aug 28, 2014 at 11:17pm
Your original code did not compile, but you said "everytime i run". It may be that you did not save your file, that your file was not part of the project, that you were compiling another project, or that you did not bother to recompile your code.
Aug 29, 2014 at 8:44am
You dont have to put condition on else statement it will be compared automatically
Aug 29, 2014 at 8:49am
just remove the condition of else.. the code works
Aug 31, 2014 at 3:56am
@ne555 you are right, the day after when I saved and restarted visual studio it worked and @lorence30
i like the else statement, it helps me to remember what's connected together. I have a lot more to do with this program
Aug 31, 2014 at 5:36am
The else is called because it is an else. ELSE is only called if your IF statement does not evaluate to true aaaaaand your ELSE IF statements do not evaluate to true. here is what you want and also what you should aim for in terms of bare minimums of comments. The comments i made suck and do not fully explain what happens in the program which is what you should really aim for.

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
40
41
42
43
44
45
//this program calculates the taxes of a given price per province in canada

//STD includes
#include <iostream>
#include <string>
#include <limits> //needed to keep the window open 

//namespace(s)
using namespace std;

int main()
{
    //variable definitions
    float price, taxes;
    float quebec = 1.1498;
    float yukon = 1.05;

    //user input
    cout << "enter the price of your item: ";
    cin >> price;
    string stateprovince;
    cout << "enter the province: ";
    cin >> stateprovince;

    //logic
    if (stateprovince == "yukon"){
        cout << "the price of your item including taxes is $" << yukon << "\n";
    }
    else if (stateprovince == "quebec"){
        cout << "the price of your item including taxes is $" << yukon << "\n";
    }
    else {
        cout << "please enter a correct canadian province";
    }
    //IF you really want to pause the program before it closes because you 
    //don't know how to run the program from the command line or have
    //an IDE that keeps it open for you (like code::blocks for example)
    //you do so like i have below
    cout << "Press ENTER to continue...";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    //cin.get() is something we were all taught to do and later found out its
    //suuuuuuppppperr bad from our bosses or the legends found on
    //this site.
    return 0;
}


your code had no way of looping back up to the original input so i left it that way.
Aug 31, 2014 at 5:49am
@nubforce

You have a copy-paste mistake in line 30. 'yukon' should be 'quebec'.
Topic archived. No new replies allowed.