HELP: determineType with cin

I am new to c ++ and what I am trying to achieve is cin a value and make the overloaded functions determine the type of value . Here is what I have so far. I just need some guidance in the right direction please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


void determineType(int x)
{
	cout << x << " this is an integer." << endl;
}
void determineType(char x)
{
	cout << x << " this is an Character." << endl;
}
void determineType(float x)
{
	cout << x << " this is an Float." << endl;
}

int main()
{
	
	cout << "Enter whatever you would like: "<< endl;

	cin >> determineType(x); 
	cout << endl;

	system("pause");
	return 0;
}
Last edited on
google "typeid" c++
I would normally explain it but this one needs you to read more than I care to type.

ok thanks
its going to be tricky even so. It may be that all you can do is read it as a string and run some sort of determination code over it. The typid stuff was meant for internal to program use... not for user input really.

At the end of the day, there are only 4 or so basic types. Its an integer type, a floating point type, a string type, a Boolean type, an array of one of those, and .. what else? Not a lot from the user's perspective, unless the user is strangely aware of your classes etc.


Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
#include <iomanip>

std::string trim( std::string str )
{
    // remove trailing white space
    while( !str.empty() && std::isspace( str.back() ) ) str.pop_back() ;

    // return residue after leading white space
    std::size_t pos = 0 ;
    while( pos < str.size() && std::isspace( str[pos] ) ) ++pos ;
    return str.substr(pos) ;
}

bool is_integer( std::string str ) // trimmed string
{
    std::size_t pos  = 0 ;
    try
    {
        std::stoll( str, std::addressof(pos), 0 ) ;
        if( pos == str.size() ) return true ;
    }

    catch( const std::exception& ) {}
    return false ;
}

bool is_floating_point( std::string str ) // trimmed string
{
    std::size_t pos  = 0 ;
    try
    {
        std::stold( str, std::addressof(pos) ) ;
        if( pos == str.size() ) return true ;
    }

    catch( const std::exception& ) {}
    return false ;
}

enum type_t { INTEGER, FLOATING_POINT, CHARACTER, STRING, NOTHING };

std::ostream& operator<< ( std::ostream& stm, type_t type )
{
   static const std::string desc[] = { "INTEGER", "FLOATING_POINT", "CHARACTER", "STRING", "NOTHING" };
   if( type < INTEGER || type > NOTHING ) type = NOTHING ;
   return stm << desc[type] ;
}

type_t type_of_input( std::string str )
{
    str = trim(str) ;

    if( is_integer(str) ) return INTEGER ;
    else if( is_floating_point(str) ) return FLOATING_POINT ;
    else if( str.empty() ) return NOTHING ;
    else if( str.size() == 1 ) return CHARACTER ;
    else return STRING ;
}

int main()
{
    std::stringbuf sbuf( "+1 1 -1 -1234 +0x1b2f -1.234e+5 +1234. 1.23e-4 a b a1 1abcd hello" ) ;
    std::cin.rdbuf( std::addressof(sbuf) ) ;

    std::string input ;
    while( std::cin >> input )
        std::cout << std::quoted(input) << ' ' << type_of_input(input) << '\n' ;

    std::cout << std::quoted( "    " ) << ' ' << type_of_input( "    " ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/856e5bfe4c2b2ad3
http://rextester.com/ISKE88184
Topic archived. No new replies allowed.