Jan 28, 2002 #1 Guest_imported New member Joined Jan 1, 1970 Messages 0 If i have a user enter 3 integers, how do I check to make sure that they did use only integers and not characters or strings...
If i have a user enter 3 integers, how do I check to make sure that they did use only integers and not characters or strings...
Jan 28, 2002 #2 lGOOODl Programmer Joined Dec 4, 2001 Messages 31 Location DK You could probaly use. char string[3] = "123"; bool number1; bool number2; bool number3; if(string[0] = '0' || string[0] = '1' || string[0] = '2' || string[0] = '3' || string[0] = '4' || string[0] = '5' || string[0] = '6' || string[0] = '7' || string[0] = '8' || string[0] = '9') { number1 = true; } if(string[1] = '0' || string[1] = '1' || string[1] = '2' || string[1] = '3' || string[1] = '4' || string[1] = '5' || string[1] = '6' || string[1] = '7' || string[1] = '8' || string[1] = '9') { number2 = true; } if(string[2] = '0' || string[2] = '1' || string[2] = '2' || string[2] = '3' || string[2] = '4' || string[2] = '5' || string[2] = '6' || string[2] = '7' || string[2] = '8' || string[2] = '9') { number3 = true; } if(number1 && number 2 && number 3) { //place your code here } there's probaly a easy'er way to do this, but it is a solution I hope you can use it! ..:::GOOOD:::.. Upvote 0 Downvote
You could probaly use. char string[3] = "123"; bool number1; bool number2; bool number3; if(string[0] = '0' || string[0] = '1' || string[0] = '2' || string[0] = '3' || string[0] = '4' || string[0] = '5' || string[0] = '6' || string[0] = '7' || string[0] = '8' || string[0] = '9') { number1 = true; } if(string[1] = '0' || string[1] = '1' || string[1] = '2' || string[1] = '3' || string[1] = '4' || string[1] = '5' || string[1] = '6' || string[1] = '7' || string[1] = '8' || string[1] = '9') { number2 = true; } if(string[2] = '0' || string[2] = '1' || string[2] = '2' || string[2] = '3' || string[2] = '4' || string[2] = '5' || string[2] = '6' || string[2] = '7' || string[2] = '8' || string[2] = '9') { number3 = true; } if(number1 && number 2 && number 3) { //place your code here } there's probaly a easy'er way to do this, but it is a solution I hope you can use it! ..:::GOOOD:::..
Jan 28, 2002 #3 Leibnitz Programmer Joined Apr 6, 2001 Messages 393 Location CA thread181-27567 Upvote 0 Downvote
Jan 28, 2002 #4 Leibnitz Programmer Joined Apr 6, 2001 Messages 393 Location CA The previous link is from the 'C' Forum,i think that you might find the explanation of what you are looking for in it. Upvote 0 Downvote
The previous link is from the 'C' Forum,i think that you might find the explanation of what you are looking for in it.
Jan 28, 2002 #5 Leibnitz Programmer Joined Apr 6, 2001 Messages 393 Location CA Sorry it was the wong link this one is the right one: Thread205-196473 Upvote 0 Downvote
Jan 29, 2002 #6 Zyrenthian Programmer Joined Mar 30, 2001 Messages 1,440 Location US The easiest way would be to read into a character array and convert the value using atoi. To make sure it is all integers you would use strspn char buffer[32]; cout<<enter a number; cin>>buffer if(strspn(buffer,"1234567890" == strlen(buffer)) { // valid } <Matt Upvote 0 Downvote
The easiest way would be to read into a character array and convert the value using atoi. To make sure it is all integers you would use strspn char buffer[32]; cout<<enter a number; cin>>buffer if(strspn(buffer,"1234567890" == strlen(buffer)) { // valid } <Matt