I'm not even certain if I am wording my title correctly for this question but this is my predicament. I'm trying to get input from the user, it could possibly be a float value like so ###.## or it could be a character value like DNA, I/P, or F, or it could be a sequence of integers ranging from 1 to 5 of them, so "1 2 3 4 5" or "54, 23, 5123, -50, 12" are all valid. I don't understand how I would go about doing this.

I've noticed that this forum doesn't take too kindly to people asking questions without supplying source code of attempts first, I don't know how to attempt this, nor do I want it hand fed to me, I just need a poke or nudge in the right direction because I can't see how this would work without trying to shove different data types into the wrong variable.

Edited by Acrimonus: n/a

8 Years
Discussion Span
Last Post by Acrimonus

the easiest way to handle all different types of inputs is to receive them as strings and then go through them and figure out what the user entered. there are plenty of functions to help with this. the function isdigit() will test to see if the character is a number. isalpha() will test if its a letter. these functions are defined in the header <locale>. a little demonstration of receiving input as a string and converting it to a number is:

#include <iostream>
#include <locale>
#include <cstdlib>

using namespace std;

int main()
       int i = 0;
       int numberInput;
       char input[80];  // used for the user input
       char converter;
       cout << "please enter a number: ";
       cin.getline(input, "\n");  //  receives the entire input into input
       while (isdigit(input[i]))
              converter[i] = input[i]
       convert[i] = "\n";
       numberInput = atoi(converter);
       cout << "\nyou entered: " << numberInput;
       return 0;

this is just a quick little example of how to get input as strings. of course there are much better ways to do because this wont work with negative numbers. this is just to give you an example how the basics work.

Edited by NathanOliver: n/a

Votes + Comments
Thanks for the assistance
Nice answer.

Get it as a string, then parse the string to find out what kind of data it contains.

[edit]Oops! Too late. Like ^^^ said.

Edited by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.