Yes I want to populate an array with 5 numbers (between 0-9) and check that the user only enteres a number between 0 and 9, then read it out backwards.
Best regards, global
Yes I want to populate an array with 5 numbers (between 0-9) and check that the user only enteres a number between 0 and 9, then read it out backwards.
Best regards, global
The problem area is only allowing the user to enter a number between 0 and 90.
Regards, global
ooops. sorry 'bout that. Thank you misplaced.
You don't need to convert the input to a char to check that user input only values 0-9. You can use an array of int. Use a loop to get the ints. Check that input is in the range you want. Store each input in the array in order they are received. Print them out backward using a loop starting at last valid index and decreasing loop counter each loop through.
Yes, but what if the user enters a letter by mistake?
Thanks for your help, global
cin goes into an error state. You can use clear and ignore to reset it and clear out the faulty buffer.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Originally Posted by global
*i believe there is a function/macro call isalpha()...
if you're using an ide with documentation look in the help index for it
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
Input validation by accepting input as a string and converting after validation or by using the cin.fail(), cin.good(), cin.ignore(), cin.clear() methods are both valid. Whichever way you feel more comfortable should work.
How would i convert it after validation? If i want to input 5 numbers as characters. check convert it, check that it is above 0 and below 9 - how would this be done. I thought this would be easy, but people just say 'convert' - but i have yet to achieve this.
I do appreciate your help.
Regards, global
About making sure the input is only numerical try:
Search isalpha to find the correct library to include.Code:if (isalpha (input)) { //incorrect input } else { //correct }
You want isdigit() and hope for a positive response. All punctuation and similar characters, such as &, fail the isalpha test and still aren't valid for what you want.
The correct header for the C-mode macro is <ctype.h> in C and <cctype> in C++, the C++-locale-aware version is in <locale>.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Indeed the isdigit function is the correct choice, sorry for my error.
The above code has not been compiled/executed and may contain errors, but the principle is sound.Code://loop to enter four digits for(int i = 0; i < 4; ++i) { //enter one digit at a time, as a char cout << "input digit # " << i << endl; cin >> ch; //repeat the process for each digit if ch //isn't between '0' and '9', inclusive while(ch < '0' || ch > '9') { cout << "input error" << endl; cout << "input digit # " << i << endl; cin >> ch; } //now that input is validated enter digit into array intAsString[i] = ch; } //make the array of char a string by adding //terminating null char intAsString[4] = '\0'; //convert string to int using atoi int result = atoi(intAsString); //display number forward cout << "the four digit number you entered was" << result << endl; //display the number backward for(i = 3; i > -1; --i) cout << intAsString[i];
The logic operators equals, greater than, and less than work for char types because to the computer all chars are just ints anyway. The exact int value of char can differ depending on the char set, but all char sets I've seen have all the digits with consecutive values, whatever they are. Therefore if user enters an 'A' or a '*' or whatever, the int value of those char will be either greater than the int value of '9' or less than the value of '0'. Thus the while loop just keeps going.
The alternative of using isdigit(ch) instead of creating your own validation procedure is eminently reasonable. Just use
while(!isdigit(ch))
instead of
while(ch < '0' || ch > '9')
in the above code, and make you sure you include the necessary header file for isdigit().