Else IF statements

Pages: 123
i keep getting errors under my else's and under my 'dead lift' and the 'pushup' could some please help me

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()
{
	int exercise;

	cout << "Please enter an exercise: ";
	cin >> exercise;
	if( exercise == 'curl' || "chinup");
	{
		cout << "This exercise is for your biceps. ";
	}
	else if(exercise == 'pushup' || "press");
	{
		cout << "This exercise is for your pecs. ";
	}
	else if(exercise == 'row');
	{
		cout << "This exercise is for your back. ";
	}
	else if(exercise == 'dead lift');
	{
		cout << "This exercise is for your whole body";
	}
}



Last edited on
i suggest changing the else if to just if, i dont think the else if is necessary
and you have to restate what varible you are using
then put just an else as a return to go back to the beginning

so like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
     int exercise;
     loop_begin:
cout << "Please enter and exercise: ";//i also think that you should create options just in case you forget but that just extra
cin << exercise;
if (exercise == "curl" || exercise == "chinup");
{
   cout << "biceps. ";//just shorted it doesnt really matter
}
if (exercise == "pushup" || exercise== "press")
{
    //blah blah
}
else 
{
   cout << "I cant understand you."endl; 
   goto loop_begin;
}


there is a better thing to use than loops though!!
i would try looking into getline, thats what im trying to do, i dont know how to use it yet
look at the conversation "Why is my varible not changing"
Last edited on
thank you very much im trying to do this assignment out of my book that i have to submit and i just have to have the output of
please enter an exercise: _______
This exercise is for your ______
if you really need to use the else if, you can always try elif, okay cool, gl in the class
Last edited on
You have to state the conditional completely. Computers don't understand:

1
2
3
4
5
6
7
8
	if( exercise == 'curl' || "chinup");
	{
		cout << "This exercise is for your biceps. ";
	}
	else if(exercise == 'pushup' || "press");
	{
		cout << "This exercise is for your pecs. ";
	}


But they will understand:

1
2
3
4
5
6
7
8
	if( exercise == 'curl' || exercise == "chinup");
	{
		cout << "This exercise is for your biceps. ";
	}
	else if(exercise == 'pushup' || exercise == "press");
	{
		cout << "This exercise is for your pecs. ";
	}


Each side of the logic operator has it's own condition and each condition needs either a Boolean or two items for comparison.

unfortunally i just found out i can't do the loop option it has to be done as written as it is i got the errors with the if statements but its still not accepting the 'pushup' and the 'dead lift'
can you post your exact script, everything you have? so i can check for any import errors
Last edited on
Pushup should not be a problem, as long as it is one word.

"dead lift," is two words and cin stops reading at the first space it encounters.

Usually, deadlifts are one word anyway, not two, so remove the space and test for "deadlift."

You also have no input validation, so if a user does type pushup as two words, of course it's going to not test true anywhere.

You could use getline(cin, exercise) if you want to test multi-word input. It will stop reading at the newline.

first thing i would suggest looking at original is to change the last one, a blankspace, i recently found out, messes up the whole thing. whenever possible avoid it, you can once again look at "Why is my varible not changing"
yea i have it up working on it right now
#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	int exercise;

	cout << "Please enter an exercise: ";
	cin >> exercise;
	if( exercise == 'curl' || "chinup");
	{
		cout << "This exercise is for your biceps. ";
	}
	else if(exercise == 'pushup' || "press");
	{
		cout << "This exercise is for your pecs. ";
	}
	else if(exercise == 'row');
	{
		cout << "This exercise is for your back. ";
	}
	else if(exercise == 'dead lift');
	{
		cout << "This exercise is for your whole body";
	}
}
Damn, I missed this. You declared exercise as an int, but you're comparing it to a string. That's going to fail everytime.

int != string.

ok thx how would i write it then
you need to put #include string
then instead of putting int put string or u could use char but I don't know if that works for more than one letter
its still throwing errors in all of the same places even doing the #include <string>
Yes, #include <string>, declare exercise as a string. If you continue to use two word user inputs, make your input statement:

getline(cin, exercise);

else if you use one word inputs, you can keep the cin, but the values it compares against must be one word also.

Also, string comparisons are case sensitive, so Deadlift != deadlift.

what's the error say?
Post your updated code.
im getting a error
this is the errors
1>e:\cmpsc 101\5c\5c\5c.cpp(11): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(std::string, int)'
1>e:\cmpsc 101\5c\5c\5c.cpp(12): warning C4390: ';' : empty controlled statement found; is this the intent?
1>e:\cmpsc 101\5c\5c\5c.cpp(15): error C2181: illegal else without matching if
1>e:\cmpsc 101\5c\5c\5c.cpp(15): error C2015: too many characters in constant
1>e:\cmpsc 101\5c\5c\5c.cpp(15): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(std::string, int)'
1>e:\cmpsc 101\5c\5c\5c.cpp(16): warning C4390: ';' : empty controlled statement found; is this the intent?
1>e:\cmpsc 101\5c\5c\5c.cpp(19): error C2181: illegal else without matching if
1>e:\cmpsc 101\5c\5c\5c.cpp(19): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(std::string, int)'
1>e:\cmpsc 101\5c\5c\5c.cpp(20): warning C4390: ';' : empty controlled statement found; is this the intent?
1>e:\cmpsc 101\5c\5c\5c.cpp(23): error C2181: illegal else without matching if
1>e:\cmpsc 101\5c\5c\5c.cpp(23): error C2015: too many characters in constant
1>e:\cmpsc 101\5c\5c\5c.cpp(23): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(std::string, int)'
1>e:\cmpsc 101\5c\5c\5c.cpp(24): warning C4390: ';' : empty controlled statement found; is this the intent?
#include <iostream>
#include <string>
using namespace std;

int main()
{
string exercise;

cout << "Please enter an exercise: ";
cin >> exercise;
if( exercise == 'curl' || exercise == "chinup");
{
cout << "This exercise is for your biceps. ";
}
else if(exercise == 'pushup' || exercise == "press");
{
cout << "This exercise is for your pecs. ";
}
else if(exercise == 'row');
{
cout << "This exercise is for your back. ";
}
else if(exercise == 'deadlift');
{
cout << "This exercise is for your whole body";
}
}
Some of your literals are in single quotes. They need to be in double quotes.
Pages: 123