Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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;



       }


 
4/10/2011 2:01:44 PM EDT
[#1]
Solution: }asdig^^$%(in488_+{{dkjk8ngn{{)(///449++++87*












*I may of just pressed buttons at random.
4/10/2011 2:04:10 PM EDT
[#2]
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.
4/10/2011 2:07:21 PM EDT
[#3]
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 (!cin_fail() && (move <= 33) && (((move - 1) % 10) < 3))
         break;
    ....
}
4/10/2011 2:11:51 PM EDT
[#4]
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....

4/10/2011 2:13:09 PM EDT
[#5]
damn keyboard didn't let me finish.
4/10/2011 2:16:13 PM EDT
[#6]




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.





4/10/2011 2:21:29 PM EDT
[#7]



Quoted:





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.





top one works. Thanks





 
4/10/2011 2:21:48 PM EDT
[#8]
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?
4/10/2011 2:27:36 PM EDT
[#9]
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.
4/10/2011 2:47:35 PM EDT
[#10]
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.

4/10/2011 2:49:38 PM EDT
[#11]
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.
4/10/2011 2:51:52 PM EDT
[#12]
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.
4/10/2011 2:52:22 PM EDT
[#13]



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...



 
4/10/2011 2:52:48 PM EDT
[#14]
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?


integer or floating point value.  cin is a fairly flexible animal.
4/10/2011 2:52:56 PM EDT
[#15]



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.



That.





 
4/10/2011 3:01:07 PM EDT
[#16]
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...
4/10/2011 3:06:24 PM EDT
[#17]







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.

g++ on Linux.  For Windows, I like Dev-C++.  I have the portable version on my flash drive.  





 

 
4/11/2011 4:14:36 PM EDT
[#18]
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.
4/11/2011 4:16:38 PM EDT
[#19]
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.)
4/11/2011 4:22:19 PM EDT
[#20]
Truth tables.

Know 'em, use 'em, love 'em.
4/11/2011 4:22:53 PM EDT
[#21]
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.

 

 
 
4/11/2011 4:23:56 PM EDT
[#22]



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 

 
4/11/2011 4:36:55 PM EDT
[#23]
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(...))