Friday, July 31, 2009

Integers and C+?

I need to loop a cin until the use inputs no number. what does the if statement have to be to do this? i tried if input==NULL but with integers, NULL = 0. i don't want to have that because 0 is such a common number. is this possible?





thus, i've written:





bool check;


int input;





while(check)


{


cin %26gt;%26gt; input;


if(input==????)


check=false


else


list.insertNode(input);


}

Integers and C+?
Hi :)





Strings have nothing to do with your question, ignore that previous answer.





I would suggest making your sentinel the number "-1".





You are working with a loop to insert numbers into a list, right?





Here's my suggestion. You don't need to use a boolean value to control the loop, though you can if you want. Instead, you could just simply test the number with a conditional statement (like


!=). Conditional statements return boolean variables anyways - For example:





int input;





cin %26gt;%26gt; input; // priming read





while (input != -1)


{


list.insertNode(input)


cin%26gt;%26gt; input; // internal read


} // end while





The first cin reads an integer in, and then decides what to do with it. The second one does the same for the rest of the cases.





Hope that helps :)





Rob
Reply:did you try...





IF(!input)
Reply:What kind of user input are you getting? C string or C++ string? If you're using a C string you can use the isdigit property and cycle through each character until finding a digit -- http://www.cppreference.com/stdstring/is...





Or if you don't want to use the properties you can just check each individual char and check if it's 0-9. Just do a For loop inside a For loop with a switch statement inside.


No comments:

Post a Comment