Thread: Problem with isdigit()

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Problem with isdigit()

    Alright, so I have my own forum up (SMF) and I added a shop mod to try to get my users a little more active. The shop has been pretty inactive at this point so I decided to write a program that would earn my members shop credits. The program starts off by prompting users to input their forum ID number. All is fine when you put in a number, but I want to prevent bugs when someone feels they need to try and put characters in instead.

    Here is the source pertaining to the problem.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <ctype.h>
    #include "Declare.h"
    
    using namespace std;
    
    // Main
    int getid;
    char input[100];
    int input1;
    
    // Game 1
    int j;
    int a = 2000;
    int k = (((rand() % 20) + rand() % 10) * getid);
    int shop = 10000;
    int wep = getid * 5;
    string start("START http://www.chatshack.us/forum/index.php?action=pm;sa=send;u=1");
    string open("START k.ccs");
    string delfile("if exist k.css del k.ccs");
    
    // End Declarations
    
    int main() {
        
        SetConsoleTitle("Chatshack Public Entertainment First Release!");
        deletefile();
        id();
        cout << "Welcome to the first chatshack.us community game!\nHere you will be playing our game to earn forum shop cash.\n";
        cout << "Lets get started!\n\n";
        cout << "Make a selection:\n\n";
        cout << " 1 - CCS History\n 2 - Guess the Password\n 3 - Game 2\n 4 - Game 3\n 5 -\n";
        cout << " 6 - \n 7 - \n 8 - \n 9 - \n10 - Load Chief's Chat Shack\n" << endl;
        cout << "Selection: ";
        cin >> input1;
        switch ( input1 ) {
        case 1:
        beta();
             break;
        case 2:
        game1();
             break;
        case 3:
        game2();
             break;
        case 4:
        game3();
             break;
        case 5:
        beta();
             break;
        case 6:
        beta();
             break;
        case 7:
        beta();
             break;
        case 10:
        runccs();
             break;
        default:
        system("cls");
        cout << "Error!" << endl;
        cin.get();
        Sleep(1000);
        system("cls");
        main();
                break;
                }
        return 0;
    }
    
    // Start Misc Voids
    
    void id() {
         system("cls");
         cout << "What is your forum ID?\n\nID: ";
         cin >> getid;
         if (isdigit(getid)) {
         cout << "\nID saved!" << endl;
         Sleep(1000);
         system("cls");
         } else {
         system("cls");
         cout << "Error, please try again" << endl;
         Sleep(1000);
         main();
         }
         cout << "Testing" << endl;
    }
    When I put in an integer, I get the "else" statement. When I put in any characters, it just repeats this endless loop of going to id(); and then the else statement. How can I fix this?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Try setting the getid to NULL or something different once it reaches the else statement. It keeps the same value, goes through int main again, hits id(), and goes straight to else everytime since getid is never reset.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But "getid" is a number, not a character, and whilst "isdigit()" will accept an integer, it is only valid for the range -1..255, and is unlikely meaningfull in the above context, as it will be "true" for the value 48 to 57 ('0' to '9'), which is unlikely to be what the original poster wanted.

    "isdigit()" is really supposed to be used on characters, not on integer numbers. C and PHP (for example) are quite different in this sense, as PHP allows you to convert just about any variable to any other variable, and you could quite easily come up with something that lookes similar to this code that would work in PHP, switching between a numeric (integer) representation and a character (string/char array) representation.

    If you want to check if a number is correctly input, do something like this:
    Code:
    if(cin >> someint) ... // it's an OK number 
    else ... // it's not an OK number.
    In the failing case, make sure to call cin.clear() to remove the failied bit or things will continue to fail. - you may also want to check out the FAQ on "How to clear the input".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Quick question, why is it when you input the integer that it skips the if statement?

    Nevermind.

    Well, I'm not looking for any certain number, just to make sure what the user inputs IS a number so as not to get crash the program filling an int with characters.
    Last edited by Mastrchief; 10-14-2007 at 02:21 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Mastrchief View Post
    Quick question, why is it when you input the integer that it skips the if statement?
    Which if-statement are you referring to - the bit in my post or your own code? If you haven't read my post yet, then please do so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    I hadn't seen your post when I made the reply, only scwizzo's.

    I also edited my post a little.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM