checking user input for "type"

This is a discussion on checking user input for "type" within the C++ Programming forums, part of the General Programming Boards category; howdy, this code is an atempt to get user input and check to make sure it is the correct type. ...

  1. #1
    In The Light
    Join Date
    Oct 2001

    checking user input for "type"

    this code is an atempt to get user input and check to make sure it is the correct type.

    #include <iostream.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <typeinfo>

    float ftoi();
    int main()

    float ts;
    // ftoi();
    ts = ftoi();
    cout <<"ts = " <<ts<<"\n";
    cout <<"ts doubled = "<<ts*2<<"\n";


    float ftoi()
    //the first thing is to convert from feet to inches
    system ("clear");
    int feet = 0; //set feet to zero
    double inches = 0; //set inches to zero
    double total_span = 0;
    char t;

    cout <<"Enter FEET of bridge span: "; //whole feet of bridge span
    cin >> feet;
    t = feet;
    if (typeid(t).name() != "int"){
    //cout<<"You must enter feet as a digit\n";
    cout <<"\nEnter INCHES with fraction(ie 6.38)of bridge span: "; //inches span
    cin >> inches;

    feet = feet*12; //convert feet to inches
    total_span = feet + inches; //add feet & inches
    cout <<"\nThe Total span is "<<total_span<<" inches\n";

    return total_span;

    the "typeid(t).name()" thing seems to catch the char types however it just falls through and exits.
    what is a better way of checking user input for correct type.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    This is not really what RTTI was put in the language for. Its there for downcasting really.
    For your example you are using cin >> to get an int and are worriedabout anything other than an int being input.... you have 2 choices....

    1) dont use cin>> but use cin.getline() instead to get input as a string. You can then parse the string and act accordingly. isdigit() can check a char for being a digit.atoi() is the function that converts a string "1234" into its int value 1234.

    2) use istream member functions cin.good(),cin.bad(),,cin.clear() and cin.ignore().
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM

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