i need to find the user I/p is an String or char or int or float
in C++ lang .

My Goal
is to Get 3 inputs from user
and Find the Greatest of all
and list out it
but matter of fact the Big deal is
the INPUT may be int or may be float or char
1st i need figurout the data struct of input and the find greatest .

This is my point,
Any Suggestions codes .

Dani AI

Generated

Reading the input as a token (string) first is the right idea — credit to for that — but use the standard parsing helpers to be robust. A simple, reliable policy is:

  • try to parse the whole token as an integer (use std::stoll with the pos result),
  • otherwise try to parse it as a floating-point (std::stold with pos),
  • otherwise if the token length is 1 treat it as a char,
  • otherwise treat it as a string.

That avoids fragile character-by-character rules and handles signs and scientific notation correctly. Note the tiny typo in the earlier sample (DotReached = ture should be true) and be careful about overflow (stoll/stold throw std::out_of_range).

The code below implements the classifier and then reports the largest numeric value when any numeric inputs are present; if there are no numeric inputs it falls back to a lexicographic comparison. Change the comparison policy if you want chars versus numbers compared differently.

#include <iostream>
#include <string>
#include <vector>
#include <limits>

struct Item {
    enum Type { Integer, Floating, Character, Text } type;
    std::string raw;
    long double num = 0.0L;   // numeric value when applicable
    long long ival = 0;       // integer value when applicable
    char ch = 0;
};

bool parseInteger(const std::string &s, long long &out) {
    if (s.empty()) return false;
    size_t pos = 0;
    try { out = std::stoll(s, &pos); }
    catch (...) { return false; }
    return pos == s.size();
}

bool parseFloat(const std::string &s, long double &out) {
    if (s.empty()) return false;
    size_t pos = 0;
    try { out = std::stold(s, &pos); }
    catch (...) { return false; }
    return pos == s.size();
}

int main() {
    std::vector<Item> items;
    for (int i = 0; i < 3; ++i) {
        std::string tok;
        std::cout << "Input #" << (i+1) << ": ";
        if (!(std::cin >> tok)) return 0;
        Item it; it.raw = tok;
        long long iv; long double fv;
        if (parseInteger(tok, iv)) { it.type = Item::Integer; it.ival = iv; it.num = (long double)iv; }
        else if (parseFloat(tok, fv)) { it.type = Item::Floating; it.num = fv; }
        else if (tok.size() == 1) { it.type = Item::Character; it.ch = tok[0]; }
        else { it.type = Item::Text; }
        items.push_back(it);
    }

    bool anyNumeric = false;
    long double best = -std::numeric_limits<long double>::infinity();
    int bestIdx = -1;
    for (size_t i = 0; i < items.size(); ++i) {
        if (items[i].type == Item::Integer || items[i].type == Item::Floating) {
            anyNumeric = true;
            if (items[i].num > best) { best = items[i].num; bestIdx = (int)i; }
        }
    }

    if (anyNumeric) {
        std::cout << "Greatest numeric input: " << items[bestIdx].raw << " (value=" << best << ")\n";
    } else {
        int idx = 0;
        for (int i = 1; i < 3; ++i) if (items[i].raw > items[idx].raw) idx = i;
        std::cout << "Greatest (lexicographic): " << items[idx].raw << '\n';
    }
    return 0;
}

Tips: handle std::out_of_range if you need to detect overflow; use getline + trimming if inputs can contain spaces; for non‑throwing parsing in newer toolchains consider std::from_chars. If you want a single universal comparison (e.g., compare chars by ASCII against numbers), decide that rule up front and convert inputs to a common key before taking the max. Thanks to and for earlier pointers — this version focuses on correctness and practical edge cases.

Recommended Answers

All 11 Replies

Any One Help ..... please......

Input to a string, then call a function to check whether it the input string has only digits (and '.', '+' and '-' as first only). If it really has only digits, it's a number. If it has a dot, it's a floating-point number, else it's an integer.
If you reach a character that isn't one of the above, it's simply a string. Furthermore, if the string only has one character, it's a char.

After determining what's the user input, you can use the atoi (ASCII to int) or atof (ASCII to float) to convert the string to a number (if the input is a number, of course)

Can U Povide some part of codes
plz unbeatable0

#include<iostream>
#include<string>

using namespace std;

#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4

int main(void)
{
string Input;
cout<<"Input a string/character/number:\n";
cin>>Input;
short int input_type=0;
int x;
bool DotReached=false;
if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
input_type=IS_CHARACTER;
else
  for(x=0;x<Input.length();x++)
  {
  if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.') 
     {input_type=IS_STRING; break;}
   else if(Input[x]=='.')
     {if(!DotReached) DotReached=ture; else
        {input_type=IS_STRING; break;}
     }
  }
// continue from here
}
commented: Good , wat i needed is Got exactly ........ +1

any one plz Explain these above code ??????
pllzzzzz .....

include<iostream>
include<string>

using namespace std;

define IS_STRING 1
define IS_CHARACTER 2
define IS_INTEGER 3
define IS_FLOAT 4

int main(void)
{
string Input;
cout<<"Input a string/character/number:\n";
cin>>Input;
short int input_type=0;
int x;
bool DotReached=false;
if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
input_type=IS_CHARACTER;
else
for(x=0;x<Input.length();x++)
{
if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.')
{input_type=IS_STRING; break;}
else if(Input[x]=='.')
{if(!DotReached) DotReached=ture; else
{input_type=IS_STRING; break;}
}
}

please use code tags, and avoid red text many people will be repelled by it!

define IS_STRING 1
define IS_CHARACTER 2
define IS_INTEGER 3
define IS_FLOAT 4

This is to make a it easy for you to understand, basically writing IS_STRING is exactly the same as writing 1.

bool DotReached=false;

Create a boolean value can have the value of true or false

if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))

Checks to see if the input is only one char long, if so checks thats its bewteen 0 and 9. Otherwise it must be a Char, cannot be a floating point. If it is between 0 and 9 then it is a integer.

if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.')
{input_type=IS_STRING; break;}

Checks to see if the character is between 0-9 or is a '.' if not then its a string and breaks the loop since we have no reason to carry on.

{if(!DotReached) DotReached=ture; else
{input_type=IS_STRING; break;}

Checks to see if Dotreached has been set, if it has and we are at this point again then we know its a string. If it hasn't then it could still be a floating point.

Chris

commented: Thk Yu Chris........U r Kind Enough to teach me.......I m Happy ....... +1

THank You Chrisssssss...............

Plz Give me A good C++ compiler.........
links to download it

Okay SIr
I GOT IT

FIANLLY PROBLEM SOLVED.........

THANKS TO ALL .................

See if you are planning to give the arguements through command line then this could be the foloowing steps you can do;
(1) Check the number of arguements first.
(2) Check whether the argument values are NON Null
(3) Check the type of the data whether it is is string, integer or float.

i will give you the code shortly if you need!!!
Command line parsing would be much more easier and generic!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.