Strangely enough this doesn't work. Any ideas why?
[code]
#include <iostream>
using namespace std;
int main()
{
char rob;
cout<<"yes or no?\n";
cin>>rob;
char y;
char n;
y==1;
n==0;
{
if (rob=y)
cout<<"thats correct";
}
return 0;
}
/[code]
Strangely enough this doesn't work. Any ideas why?
[code]
#include <iostream>
using namespace std;
int main()
{
char rob;
cout<<"yes or no?\n";
cin>>rob;
char y;
char n;
y==1;
n==0;
{
if (rob=y)
cout<<"thats correct";
}
return 0;
}
/[code]
Terrible i tell you!Code:#include <iostream> using namespace std; int main() { char rob; cout<<"yes or no?\n"; cin>>rob; char y; char n; y==1; n==0; { if (rob=y) cout<<"thats correct"; } return 0; }
try this:
Code:#include <iostream> using namespace std; int main() { char rob; cout<<"yes or no?\n"; cin>>rob; char y; char n; y=1; n=0; if (rob==y) cout<<"thats correct"; return 0; }
Not very strange at all, really...
>y==1;
>n==0;
>if (rob=y)
Why not read this
http://www.cprogramming.com/cboard/s...threadid=31176
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Someone needs to learn the difference between assignment operators and comparison operators.
1) To compare two items you use ==
2) To assign a value to an item use =
Naturally I'll skip overloading of operators and other compairson functions, because you don't even understand this first item.Code:x = 10; // assign 10 to x if( x == 10 ) // compare x with the 10
On a side note, the second code example, while it will compile, isn't much better. It's ugly code also. One of the main reasons I don't like C++ as much as C is because it promotes such ugly code. (Randomly scattering variable declarations throughout code. Ug.)
Quzah.
Hope is the first step on the road to disappointment.
Of course that all depends on the programmer, even with
basic i put the variables on the top, because it's so much
eayer managing, can't imaging creating variables in the
middle of a program, good tutorials dont promote this kind
of programming
But when you call a function in the middle of main and in that function create a int or something that is actually the same as creating an int in the middle of main
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
Somewhere in Mr. Stroustrup's book it's suggested that declaring of variables should be done as near as practicable to where the variable is used in the code.
Whether it lends itself to readability, or not, (I personally feel it does not) the rationale, as I recall, is that there is wasted overhead involved in declaring/defining variables which may not be used during program execution. Not too hard to envision an application, such as a game, where execution may never involve one, or more, sections of code.
Now all we need is a rock-solid "Rule of Thumb" to guide us in determining when the trade-off in potentially wasted overhead is acceptable where readability and maintainability are concerned.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
In addition to what Quazh said, the == operator returns true or false only, that's why they are used in if() statements.Originally posted by quzah
Someone needs to learn the difference between assignment operators and comparison operators.
1) To compare two items you use ==
2) To assign a value to an item use =
Naturally I'll skip overloading of operators and other compairson functions, because you don't even understand this first item.Code:x = 10; // assign 10 to x if( x == 10 ) // compare x with the 10
On a side note, the second code example, while it will compile, isn't much better. It's ugly code also. One of the main reasons I don't like C++ as much as C is because it promotes such ugly code. (Randomly scattering variable declarations throughout code. Ug.)
Quzah.
none...