Thread: Not a float

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Not a float

    What i am trying to do is to set a value for a. the value should be a number (float).

    but i need to make it so that if a char is entered, then the program will end, or say something like "this is incorrect. please try again".

    this is a sample of my code. if u need more please ask.

    Code:
    cout << "Please enter the value for a, the lower bound  ";
    cin >> a;
    now, if i put a number in there it'll carry on doing whatever it does. but if i put a letter in, i'd like it to stop. so what's the way of saying

    "if a is not a flaot then.....
    Last edited by derek tims; 03-16-2006 at 05:37 PM.

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    There is a difference between tedious and difficult.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <limits>
    
    /* *** CODE HERE *** */
    
    std::cout << "Please enter the value for a, the lower bound  ";
    while(!(cin >> a))     // If the stream fails from bad input
    {
       std::cin.clear();             // Clear the fail state
       std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');   // Clear the buffer
       std::cout << "Invalid statement, please re-enter: ";     // Reprompt
    }
    Last edited by SlyMaelstrom; 03-16-2006 at 06:04 PM.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    awesome..... thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM