Hi folks! I'm learning C++ on my own using tutorials here and there. I have a question about input.
Say I want to get input from the user but I don't know what type of input they're going to give me, and I want to do something different depending on the type of input I get.
For example, if they give me an int I want to send it to function X, if they give me a double I'll send it to function Y, if they give me a char it goes to function Z, if they give me a string I want to just print it (or whatever -- I just want to do something different depending on the type).
But in order to save their input via cin, I have to store it somewhere, and to store it somewhere I need to know what type it is. But I can't know that until it's been input. Know what I mean?
Closest I can figure is that I'd have the input go to a string, then send the string to a for-loop and examine each character in the string to see if it's a number, alphabet or period... then if it's all numbers it's an int, if it's all numbers and a single period it's a double, if it's one character of anything else it's a char, and if it's anything else it's a string. Is this the most efficient way to do this though? It seems so clunky to do it this way but I can't think of any other way. I'm still looking in the library for something that might work instead of my clunky way.
Here's my clunky way to do this... All my code does is take an input as a string, cycle thru it and check each character to see if it's a number, alphanum, or period(dot)... then it checks to see if there are multiple dots, and assesses whether the input is an int, char, double or string. If it's a single-digit it is assumed it's an int and not a char. If it's all numbers it's assumed it's an int and not a string.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
string something = "";
int number = 0;
double decimal = 0.0;
char character = '0';
bool adigit = false;
bool adouble = false;
bool achar = false;
bool astring = false;
bool adot = false;
bool manydots = false;
cout << "Enter something: ";
cin >> something;
for(int i=0; i<something.length(); i++){
//loop the entire word
cout << "Looping thru the string: " << endl;
//is it a number
if(isdigit(something[i])){ //digit?
adigit = true;
adouble = true;
cout << "Element " << i << " is a digit." << endl;
} else if ((something[i]=='.') && (adot==false) && (manydots == false)){ //double?
adigit = false;
adouble = true;
adot = true;
cout << "Element " << i << " is a dot." << endl;
} else if ( (something[i]=='.') && (adot==true)){
adot = false;
manydots = true;
astring = true;
} else if (!isdigit(something[i])){ //not a number
adigit = false;
adouble = false;
astring = true;
cout << "Element " << i << " is not a digit or a dot." << endl;
}
cout << "Loop is done." << endl << endl;
}
cout << "Assess the results." << endl;
//is it a char?
if( (!adigit) && (something.length() <= 1)){
achar = true;
cout << "It's a single char." << endl;
character = something[0];
} else if ( (adigit) && (something.length() >1 ) ){
achar = false;
cout << "It's not a single char." << endl;
}
//is it a double?
if( (adouble == true) && (adot == false) && (astring == false)){
adouble == false;
cout << "It's an integer." << endl;
number = atoi(something.c_str());
something = "";
character = '\0';
} else if((adouble == true) && (adot == true)){
cout << "It's a double." << endl;
decimal = atof(something.c_str());
something = "";
character = '\0';
} else if( (adigit == true) && (adouble == false)){
cout << "It's an integer." << endl;
number = atoi(something.c_str());
something = "";
character = '\0';
} else if (astring == true){
cout << "It's a string." << endl;
character = '\0';
//it's already a string
} else {
cout << "Error: Wtf?" << endl;
}
cout << "\nFinal info:" << endl;
cout << "Integer: " << number << endl;
cout << "Char : " << character << endl;
cout << "Double : " << decimal << endl;
cout << "String : " << something << endl;
}
|
What is an easier way to do this? Are there any other library functions that could help? Should I do this as a template? Thanks. =)