4 Replies - 1181 Views - Last Post: 10 September 2011 - 05:03 PM Rate Topic: -----

#1 Arkeshen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-September 11

C++ Do While Loop Error/Else if

Posted 10 September 2011 - 04:28 PM

I'm having trouble with a Do While Loop with a Series of Else If Statements, I know I'm doing something horribly wrong but I need help finding out what.

int numberGuess()
{
	int guess;
do
{
	cout << "Think of a number between 0 and 100. " << endl;
	cin >> guess >> endl;
	cout << "Is your number greater than, less than or equal to " << guess << "?" << endl;
	if(guess = 14)
	{
		cout << "Equal To" << endl;
	}
	else
	
		if(guess < 14)
		{
			cout << "Less Than " << endl;
		}
		else
			if(guess > 14)
			{
			cout << "Greater Than " << endl;
			}
			else
			{
				cout << "Equal To " << endl;
			}
	
}
while(guess < 14 || guess > 14);

}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Do While Loop Error/Else if

#2 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: C++ Do While Loop Error/Else if

Posted 10 September 2011 - 04:34 PM

Look on the 9th line mate if(guess = 14), in C++ the = operator is for assignment, the check comparison for equality operator is ==.For further information on C++ operators I'd suggest this. Replace line 9 with if(guess == 14) and it should work fine. :)
Was This Post Helpful? 0
  • +
  • -

#3 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ Do While Loop Error/Else if

Posted 10 September 2011 - 04:42 PM

Line 9 should be :
if(guess==14)


= is assignment, == is comparison.

The do while loop the condition could just be while(guess != 14);

The else on line 24 is unnecessary as you already test for it on line 9.

It would be better to set your if-else code out like this, as it makes it easier to read.:

if(guess == 14)
{
   cout << "Equal to" << endl;
}
else if( guess < 14)
{
   cout << "Less than" << endl;
}
else
{
cout << "Greater than" << endl;
}


This post has been edited by simeesta: 10 September 2011 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Arkeshen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-September 11

Re: C++ Do While Loop Error/Else if

Posted 10 September 2011 - 04:54 PM

I did what you suggested, but I still recieve this Error that I'm recieve when I try to build it:

------ Build started: Project: Assignment1, Configuration: Debug Win32 ------
main.cpp
c:\documents and settings\trent\my documents\visual studio 2010\projects\assignment1\assignment1\main.cpp(108): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1053): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1060): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1067): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1074): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1097): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1104): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1111): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(1118): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(179): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(185): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(192): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(199): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(218): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(253): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(272): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(298): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(316): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(334): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(354): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(373): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(392): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(411): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(429): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(447): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 10.0\vc\include\istream(466): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
while trying to match the argument list '(std::basic_istream<_Elem,_Traits>, overloaded-function)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This post has been edited by Arkeshen: 10 September 2011 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ Do While Loop Error/Else if

Posted 10 September 2011 - 05:03 PM

Line 7, remove the >>endl bit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1