Posted: 4/10/2011 1:59:02 PM EDT
|
Why is this not working? I set the integer variable "move" to 11 and it still goes into loop. while (cin.fail()||(move != 11)||(move != 12)||(move != 13)||(move != 21)||(move != 22)||(move != 23)||(move != 31)||(move != 32)||(move != 33)) { cout <<"Invalid Input: Enter 11, 12, 13, 21, 22, 23, 31, 32, or 33: "; cin.clear(); cin.ignore(1024,'\n'); cin >>move; } |
|
Quoted:
You're saying "do this while move is not 11 or is not 12 or is not 13...". If it's 11 then it's not 12 and it's not 13 and so on. You want something like (move != 12) && (move != 13) etc. Statement with || and != will always evaluate to true if both statements cannot be true. int a = 1; if ((a != 1) || (a != 2)) always will evaluate to true. I always try to make things a little tighter:
|
|
Quoted:
Quoted:
You're saying "do this while move is not 11 or is not 12 or is not 13...". If it's 11 then it's not 12 and it's not 13 and so on. You want something like (move != 12) && (move != 13) etc. Statement with || and != will always evaluate to true if both statements cannot be true. int a = 1; if ((a != 1) || (a != 2)) always will evaluate to true. I always try to make things a little tighter: while (1){ if (move <= 33 && (((move - 1) % 10) < 3) && }As we say at work often..... That happened to me the first time I did it.... |
|
Quoted: Quoted: You're saying "do this while move is not 11 or is not 12 or is not 13...". If it's 11 then it's not 12 and it's not 13 and so on. You want something like (move != 12) && (move != 13) etc. Statement with || and != will always evaluate to true if both statements cannot be true. int a = 1; if ((a != 1) || (a != 2)) always will evaluate to true. Right, and to be clear on what we mean, the rewrite is either: while (cin.fail()||((move != 11)&&(move != 12)&&(move != 13)&&(move != 21)&&(move != 22)&&(move != 23)&&(move != 31)&&(move != 32)&&(move != 33))) or: while (cin.fail()||!((move == 11)||(move == 12)||(move == 13)||(move == 21)||(move == 22)||(move == 23)||(move == 31)||(move == 32)||(move == 33))) Either way you're saying to do the thing if we have a failure or move is not one of the listed values. |
|
Quoted: top one works. ThanksQuoted: Quoted: You're saying "do this while move is not 11 or is not 12 or is not 13...". If it's 11 then it's not 12 and it's not 13 and so on. You want something like (move != 12) && (move != 13) etc. Statement with || and != will always evaluate to true if both statements cannot be true. int a = 1; if ((a != 1) || (a != 2)) always will evaluate to true. Right, and to be clear on what we mean, the rewrite is either: while (cin.fail()||((move != 11)&&(move != 12)&&(move != 13)&&(move != 21)&&(move != 22)&&(move != 23)&&(move != 31)&&(move != 32)&&(move != 33))) or: while (cin.fail()||!((move == 11)||(move == 12)||(move == 13)||(move == 21)||(move == 22)||(move == 23)||(move == 31)||(move == 32)||(move == 33))) Either way you're saying to do the thing if we have a failure or move is not one of the listed values. |
|
You also need to be aware of OR short-circuiting. If the cin.fail() returns true, it will stop evaluating the rest of the values.
As a matter of style, this is why you make a boolean function that evaluates this, so you get something like if(!cin.fail() && moveInRange(move)) Which is not as likely to cause the poor programmer that has to read your code (probably you, six months later) a headache. |
|
Quoted:
You also need to be aware of OR short-circuiting. If the cin.fail() returns true, it will stop evaluating the rest of the values. As a matter of style, this is why you make a boolean function that evaluates this, so you get something like if(!cin.fail() && moveInRange(move)) Which is not as likely to cause the poor programmer that has to read your code (probably you, six months later) a headache. This. If you gave me code that looked like what you wrote, I'd tell HR to move your resume to the round file. And no, there is never a time to write crappy code for an assignment. Learn to do it so that it can be maintained from day one. |
|
Quoted:
In a related note. Can someone point me to a really good C++ compiler? I am getting into programming and I want a really good compiler. http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2 I figure you're looking for an IDE, not a compiler specifically –– that download has both. |
|
Quoted: Quoted: You also need to be aware of OR short-circuiting. If the cin.fail() returns true, it will stop evaluating the rest of the values. As a matter of style, this is why you make a boolean function that evaluates this, so you get something like if(!cin.fail() && moveInRange(move)) Which is not as likely to cause the poor programmer that has to read your code (probably you, six months later) a headache. This. If you gave me code that looked like what you wrote, I'd tell HR to move your resume to the round file. And no, there is never a time to write crappy code for an assignment. Learn to do it so that it can be maintained from day one. Well, I am a ME student... For some reason I have to take basic programming. The professor hasn't even covered cin.fail or expects us to understand it. I looked it up on my own. I figure I am doing pretty good considering. Anyway how would cin.fail() not work in my situation? I tried making it fail and it still worked... |
|
Quoted: That.Quoted: You also need to be aware of OR short-circuiting. If the cin.fail() returns true, it will stop evaluating the rest of the values. As a matter of style, this is why you make a boolean function that evaluates this, so you get something like if(!cin.fail() && moveInRange(move)) Which is not as likely to cause the poor programmer that has to read your code (probably you, six months later) a headache. This. If you gave me code that looked like what you wrote, I'd tell HR to move your resume to the round file. And no, there is never a time to write crappy code for an assignment. Learn to do it so that it can be maintained from day one. |
|
Quoted:
In a related note. Can someone point me to a really good C++ compiler? I am getting into programming and I want a really good compiler. Any ANSI certified C/C++ compiler is "good". You got MS C/C++ compiler on the Windows side and g++ on Linux. The bells and whistles are in the development environment, mostly the editor and debugger. At work we use Visual Studio 10 most of the time. We also compile on Linux with g++. We have common code and then shim the OS functions into OS specific modules. Besides the Linux stuff, I'm not too up to speed on what you can get for free though... |
|
Quoted:
I'm not a programmer, so I have a question: Does cin() return an integer value or would it return the ASCII values for the number entered? cin() attempts to covert the ASCII characters typed into the target data type. If target is an int, and you type in "123@", the value taken will be 123. I don't think it throws an exception. In the real world, no one uses the conversions because they are not robust. Programmers typically input everything as text and then perform data integrity and sanity checks before even attempting the conversion to the target type. The use of cin() to a converted type is normally something you do in school, but not in the real world. |
|
Quoted:
In a related note. Can someone point me to a really good C++ compiler? I am getting into programming and I want a really good compiler. The MS Visual Studio Express versions are pretty damn good for free tools. The integrated debugger is especially useful for beginners (and experienced programmers as well.) |
Here is the line I ended up with. Just to make the programmers cringe. while (set_value(board, row, column, player)||cin.fail()||((row != 1)&&(row != 2)&&(row != 3))||((column != 1)&&(column != 2)&&(column != 3))) The program works flawlessly btw. It was a tic-tac-toe game. |
|
Quoted: Here is the line I ended up with. Just to make the programmers cringe. while (set_value(board, row, column, player)||cin.fail()||((row != 1)&&(row != 2)&&(row != 3))||((column != 1)&&(column != 2)&&(column != 3))) { cout <<"Invalid Input or Space Taken: Re-Enter Row(1,2, or 3): "; cin.clear(); cin.ignore(1024,'\n'); cin >>row; cout<<"And Column(1,2, or 3): "; cin >>column; } The program works flawlessly btw. It was a tic-tac-toe game. I'll read it once you put it in the code block |
|
For the love of all that is holy, refactor your while condition, it burns my eyes! How about adding a couple of methods (pseudo code):
isValidRow(int row) : Boolean isValidColumn(int column) : Boolean Then AND it all together: while(setValue(...) && isValidRow(...) && isValidColumn(...)) |