Help a newbie in Visual C++
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?!
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'.
Also, consider using a 'while' loop instead of an 'if' and a 'goto'.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement