Sign in to follow this  

Help a newbie in Visual C++

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I was using C++ language (from the Microsoft Visual Studio 2005) to create a simple console application as following: #include <iostream> using namespace std; int main() { int value = 0; statement_01: cout << "Type in any integer value: "; cin >> value; if(value != 1 || value != 2) cout << "The value in the variable 'value' is not 1 ou 2"; else goto statement_01; cout << endl << endl << endl; } When I run it it does not work correctly because it should repeat all these statements if I typed "1" ou "2" values. But when I type in these either these values it finishs. Can someone help me?!

Share this post


Link to post
Share on other sites
Think about the logic in your 'if' statement, if needed choose a number for 'value' and work everything out by hand, step by step, and you'll see your problem.

Also, consider using a 'while' loop instead of an 'if' and a 'goto'.

Share this post


Link to post
Share on other sites
Your program doesn't work correctly because Your IF is logically wrong.
Look...

when You type "1" then you have :

IF ( 1 !=1 || 1 !=2 )
-------|----------|-------
-----false--||---true


after that You have

IF ( false or(||) true )
false || true gives you true

after that
IF (true) ...

As we know the IF executes when in () is TRUE.

So the conclusion is : Change the logical operations as OrangyTang said.

greetz

Share this post


Link to post
Share on other sites

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this