Detect alphabets in integer?

This is a discussion on Detect alphabets in integer? within the C++ Programming forums, part of the General Programming Boards category; I am making a console benchmark program, so I need the user to input an integer. However, I want to ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    43

    Detect alphabets in integer?

    I am making a console benchmark program, so I need the user to input an integer. However, I want to fool-proof it so the program will detect if an alphabet is being entered. Is there a way to do so? I need something similar to isNum in Java

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    isalpha()
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    I tried:

    Code:
                  int z;
                  cin>>z;
                  if (isalpha(z))
                  {
                        cout<<"No alphabets allowed.\n\n";
                        cin.get();
                        goto end;
                  }
    But now what it does is hang and a few seconds later I get a "Windows has encountered an error"... Help.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    I tried using this:

    Code:
                  char * conz;
                  cin>>conz;
                  int z;
                  z = atoi(conz);
                  if (isalpha(z) != 0)
                  {
                        cout<<"No alphabets allowed.\n\n";
                        cin.get();
                        goto end;
                  }
                  else if (z > 1000000)
                  {
                        cout<<"Please do not enter numbers larger than 1,000,000.  \n\n";
                        cin.get();
                        goto end;
                        
                  }
    However it returns an "No alphabets allowed" error even when I enter an integer, but only one that has more than 2 digits...

    How do I get round this?

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by trenzterra
    I tried using this:

    Code:
                  char * conz;
                  cin>>conz;
                  int z;
                  z = atoi(conz);
                  if (isalpha(z) != 0)
                  {
                        cout<<"No alphabets allowed.\n\n";
                        cin.get();
                        goto end;
                  }
                  else if (z > 1000000)
                  {
                        cout<<"Please do not enter numbers larger than 1,000,000.  \n\n";
                        cin.get();
                        goto end;
                        
                  }
    However it returns an "No alphabets allowed" error even when I enter an integer, but only one that has more than 2 digits...

    How do I get round this?

    1st - you should be getting runtime errors because you have not allocated any memory for conz, so cin is overwiting memory it shouldn't be.
    Better would be to use an array: char conz[256] = "";
    Best would be to use std::string conz;

    2nd - atoi will truncate at the first char it reaches, so something like 28gh890sf will end up just being 28. There is then no need to test with isalpha

    3rd - isalpha expects a char input, not an int, though you will end up with an autmatic conversion (with a warning) but it won't give you the results you expect.

    If you are happy with the truncation from my 2nd point then drop the isalpha, however, if you want to keep ALL the numbers and discard ONLY the letters or if you need to invalidate the entire answer if it contains alpha then you need to drop atoi and do an isalpha(char) on each of the chars of your array

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char conz[256]="";
        cin >> conz;
    
        for (int index = 0; index < 256 ; ++index)
        {
            if (conz[index] == '\0')//test for end of null terminated string
                break; 
            if (isalpha(conz[index]))
            {
                cout<<"No alphabets allowed.\n\n";
                cin.get();
                exit(0);
            }
        }
        
        if (atoi(conz) > 1000000)
        {
            cout<<"Please do not enter numbers larger than 1,000,000.  \n\n";
            cin.get();
            exit(0);
    
        }
        
        cout << "Number is OK";
    
    }
    Last edited by Darryl; 04-27-2005 at 07:46 AM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

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