Else IF statements

Pages: 123
Feb 24, 2012 at 3:55am
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 Feb 24, 2012 at 4:09am
Feb 24, 2012 at 4:16am
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 Feb 24, 2012 at 4:20am
Feb 24, 2012 at 4:21am
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 ______
Feb 24, 2012 at 4:22am
if you really need to use the else if, you can always try elif, okay cool, gl in the class
Last edited on Feb 24, 2012 at 4:24am
Feb 24, 2012 at 4:23am
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.

Feb 24, 2012 at 4:28am
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'
Feb 24, 2012 at 4:29am
can you post your exact script, everything you have? so i can check for any import errors
Last edited on Feb 24, 2012 at 4:30am
Feb 24, 2012 at 4:33am
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.

Feb 24, 2012 at 4:34am
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"
Feb 24, 2012 at 4:34am
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";
	}
}
Feb 24, 2012 at 4:39am
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.

Feb 24, 2012 at 4:40am
ok thx how would i write it then
Feb 24, 2012 at 4:44am
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
Feb 24, 2012 at 4:47am
its still throwing errors in all of the same places even doing the #include <string>
Feb 24, 2012 at 4:47am
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.

Feb 24, 2012 at 4:48am
what's the error say?
Feb 24, 2012 at 4:51am
Post your updated code.
Feb 24, 2012 at 4:52am
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?
Feb 24, 2012 at 4:52am
#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";
}
}
Feb 24, 2012 at 4:56am
Some of your literals are in single quotes. They need to be in double quotes.
Pages: 123