Conversion, atoi (apologies, searches have been done)

This is a discussion on Conversion, atoi (apologies, searches have been done) within the C++ Programming forums, part of the General Programming Boards category; Yes I want to populate an array with 5 numbers (between 0-9) and check that the user only enteres a ...

  1. #16
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    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

  2. #17
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    The problem area is only allowing the user to enter a number between 0 and 90.

    Regards, global

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  4. #19
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Yes, but what if the user enters a letter by mistake?

    Thanks for your help, global

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  6. #21
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by global
    Yes, but what if the user enters a letter by mistake?

    Thanks for your help, 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.

  7. #22
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  8. #23
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    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

  9. #24
    Registered User
    Join Date
    Oct 2004
    Posts
    19
    About making sure the input is only numerical try:

    Code:
    if (isalpha (input))
    {
    //incorrect input
    }
    else
    {
    //correct
    }
    Search isalpha to find the correct library to include.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  11. #26
    Registered User
    Join Date
    Oct 2004
    Posts
    19
    Indeed the isdigit function is the correct choice, sorry for my error.

  12. #27
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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 above code has not been compiled/executed and may contain errors, but the principle is sound.

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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 08:44 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21