How would I only enter numbers and not characters

This is a discussion on How would I only enter numbers and not characters within the C++ Programming forums, part of the General Programming Boards category; Hi sorry to bother, you, I cant find this information on your boards, all I would like to know is, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    How would I only enter numbers and not characters

    Hi sorry to bother, you, I cant find this information on your boards, all I would like to know is, how would I do an if statement, so that only numbers are accepted and no other characters: so for example only a number is enterd like 98, instead of a character like t, I tried a number of times, but i keep getting an infinate (i spell it right?) loop.
    Its something I am doing for my own pleasure thanks.

    Code:
    while ( bankBalance >= 1 ){
    		cout << "Please enter a wager, or enter -1 to quit: " <<endl;
    		cin >>wager;
    		if (wager == -1)
    			return 0;
    		if (wager > bankBalance){
    			cout << "That wager is higher then what you got in the bank" << endl <<endl;;
    			continue;}
    		cout << endl; 
    	
    		cout << "Ok here we go!" <<endl <<endl;
    		bankBalance = onegame();
    		
    		cout << "Your new Bank Balance is " << bankBalance << " pounds" << endl << endl;
    			
    	}
    
    	cout << "Sorry, your broke, Game Over" << endl;
    ps hope i got them tabs right.
    This is so fun

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To my knowledge there are two approaches to consider. First you can test the fail bit of cin objects after input, and if input failed use clear() the fail bit, then notify user to retry. Second, you can assign all input as a string, then evaluate the string to see if it meets the criteria you desire. If it does then you can change the string into the numeric value by using atoi(), atol(), and related functions.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Ok thank you for your advice.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Hi, ok I still cant get it to work quite right, as I have had no experience doing this on C++ before I done this
    Code:
    cout << "Please enter a wager, or enter -1 to quit: " <<endl;
                                   rewind(stdin);		
                                    cin >>wager;
    		cin.clear()
    However now even though it will ignour it, if I enter a letter it still adds a wager, while it should ignour it all together and put something like, I know I need an if statement to make it work corectly, but I would like to know, what exactly I need to do all my C++ books, dont make it completely clear from where I am on them at the moment.
    Code:
    cout << "Please renter value" << endl;
    I admit I dont understand this very well, what is the corect way to solve this problem, also what does rewind(stdin) do?
    Thanks for your help I apreciate it very much.
    Chris.
    Last edited by chrismax2; 04-21-2004 at 03:09 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    if ( cin>> wager ) {
      // It's a number
    }
    else {
      cout<<"Please enter a number\n";
      cin.clear();
      cin.ignore ( 80, '\n' ); // 80 is arbitrary, but simpler than the better method
    }
    >also what does rewind(stdin) do?
    ...aren't you the one that wrote it? Why code something that you don't know what it does? In any event, it basically seeks to the beginning of the stream if stdin is "seekable". Otherwise it does nothing.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    OK, thank you I apreciate it a lot.

Popular pages Recent additions subscribe to a feed

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