need help with simple problem

This is a discussion on need help with simple problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys if i enter a letter in whilst running this program it outputs zero. I know num is an ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    need help with simple problem

    Hey guys

    if i enter a letter in whilst running this program it outputs zero. I know num is an int and they cant deal with characters . I am trying to make it so my program refuses everything apart from numbers. Any help appreciated cheers!


    [code]
    int main(void)
    {
    char num=0;

    cout << "Enter a number!" << endl;
    cin >> num;

    cout << num << endl;

    return(0);

    }
    [code\]

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There is a function called IsNumeric(expression) that will return boolean indicating whether the statement can be evaluated as a number. Also look up isalpha( ) and stuff like that. Look them up at MSDN.

    http://msdn.microsoft.com

    P.S. - You were pretty close to getting the code tags right. The bottom tag "ending" the code block should be

    [ / C O D E ] without the spaces...
    Last edited by MrWizard; 06-18-2002 at 03:49 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Thanks MrWizard,

    I had a look at MSDN although I found it hard to get the results i needed . I have tried to implement the function you mentioned in a simple example but i think i havent included the necesarry library , or i am just using it wrong . Thanks for the code tags advice .Hope i get it right now.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    	int num=IsNumeric(2); 
    	
    	cout << num << endl;
    
    	return(0);
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Unhappy

    Actually that may have been a visual basic function. OOPS! Sorry about that. I have never used it but I thought it may be helpful. I know isalpha is C++. Try something like this...
    Code:
    #include <iostream>
    using namespace std;
    
    int main( void )
    {
    	char x = ' ';
    	cout << "Enter a character" << endl;
    	cin >> x;
    	if( isalpha( x ) ) 
    		cout << "A Character..." << endl;
    	else
    		cout << "Not a character..." << endl;
    
    	return 0;
    }
    Not sure if that helps.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Try this

    Code:
    char input[10];
    int number;
    
    cout << "Enter a number";
    cin.getline(input,9); //this will allow for a 9 digit number,which is bigger than an int anyways, so this should be fine
    number = atoi(input);
    cout << "You inputted " << number << endl;
    Make sure to include <stdlib.h> for atoi

    Treat all input as chars, and then use conversion functions to change the input to the desired type

    From http://www.cplusplus.com/ref/

    int atoi ( const char * string );
    Convert string to integer.
    Parses string interpreting its content as a number and returns an int value.

    Parameters.

    string
    String representing an integer number. The number is considered until a non-numeric character is found (digits, and signs are considered valid numeric characters for this parameter as specified in format). The format used is:
    [whitespaces][+|-][nnnnn]
    (where whitespaces are any tab or space character and nnnnn may be any number of digits)
    Return Value.
    The converted integer value of the input string.
    On overflow the result is undefined.
    If an error occurs 0 is returned.

    Portability.
    Defined in ANSI-C.

    There is also atol is you want a long, or atof if you want a float

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Thanks guys you've been a great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

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