I have problem in getting outputs
plz give me suggetion or correct error if any and make it working.......
plz help.........

there is no error........in this program.........when i compiled.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
//using std::cout;
//using std::cin;
//using std::endl;
using namespace std;
int main(void)
{
string s;
cout<<"Input a string/character/number:\n";
cin>>s;
short int input_type=0;
int x;
bool DotReached=false;
if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
{
input_type=IS_CHARACTER;
cout<< "its a char";
}
else
for(x=0;x<s.length();x++)
{
if((s[x]>'9' || s[x]<'0') && s[x]!='.')
{
input_type=IS_STRING; break;
cout<<" Its an integer ";
}
else if(s[x]=='.')
{if(!DotReached) DotReached= true; else
 {
 input_type=IS_STRING; 
 cout<<"ITs a Float";
 break;
 }
}
}
cin.get();
cin.get();
return 0;
}

Dani AI

Generated

This thread shows the typical loop-and-classify pattern and the exact pitfalls that tripped up . The root problems were (a) printing after a break (making some messages unreachable), and (b) deciding integer vs. float while still inside the scan loop. correctly recommended moving the final numeric decision outside the loop and restoring the dot-count logic. The final posts mostly fix that, but there are two remaining issues worth noting: a branch that assigns IS_STRING yet prints "Its a flt" (a print/state mismatch that confuses debugging), and the scanner does not allow common numeric forms such as a leading sign or scientific notation.

Practical, robust changes to apply:

  • During the character scan only detect invalid characters and count special markers (dots, exponent flags). If an invalid character appears, mark as STRING and break immediately.
  • Replace manual comparisons like (c > '9' || c < '0') with character-class helpers (prefer std::isdigit((unsigned char)c)), which is clearer and safer.
  • Permit an optional leading +/-, accept forms like .5 or 5., and treat more than one . as STRING.
  • Perform the final mapping after the loop: invalid -> STRING; else if exponent seen or dot_count>0 -> FLOAT; else -> INTEGER.
  • Make diagnostic text match the assigned type and prefer an enum instead of macros for readability.

A compact scan flow (high level): obtain the token (use getline if spaces matter); if length==1 and not digit -> CHARACTER; otherwise iterate: digits OK; . increments dot_count; leading +/- allowed; detect e/E and validate exponent digits; any other char -> STRING and stop. After the loop, map flags to the final type.

For production use, prefer well-tested conversion/parsing that reports where parsing stopped: see std::isdigit for safe character tests and std::from_chars (C++17) or C parsers like strtod to validate that the entire token was consumed rather than relying on ad-hoc character checks.

Recommended Answers

All 8 Replies

for(x=0;x<s.length();x++)
{
    if((s[x]>'9' || s[x]<'0') && s[x]!='.')
   {
   input_type=IS_STRING; break;
   cout<<" Its an integer ";
   }

   else if(s[x]=='.')
  {
   if(!DotReached) DotReached= true; else
   {
   input_type=IS_STRING; 
   cout<<"ITs a Float";
   break;
   }
 }
}

Line 6:
How do you want the program to output "It's an integer" when there's a "break;" before it?
You should output "It's a string" and not "It's an integer" if the input_type is IS_STRING.

Line 14:
Again, it's a string, not a floating-point number.

If you want to determine the input type after you know it's not a char/string (if input_type==0, which is its initial value), you should just check if DotReached returns true or false, because you already know it's a number:

if(input_type==0) // the initial value - meaning no type is determined yet
   {
   if(DotReached) input_type=IS_FLOAT;
   else input_type=IS_INTEGER;
   }

Cool i corrected it ......
thanks Mr Unbeatable()
It works but Also Say Its integer
when i enter Floating

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
//using std::cout;
//using std::cin;
//using std::endl;
using namespace std;
int main(void)
{
string s;
cout<<"Input a string/character/number:\n";
cin>>s;
short int input_type=0;
int x;
bool DotReached=false;
if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
{
input_type=IS_CHARACTER;
cout<< "its a char";
}
else
for(x=0;x<s.length();x++)
{
if((s[x]>'9' || s[x]<'0') && s[x]!='.')
{
input_type=IS_STRING;
cout<<" ITs an String ";
break;
}
else if(input_type==0) 
  {
    if(DotReached) 
    {
     input_type=IS_FLOAT;
     cout<<"Its a Floating Point Value";
     break; 
      }
   else
   {
    input_type=IS_INTEGER;
    cout<<"Its an Integer";
    break;
   }
   }
}
cin.get();
cin.get();
cin.get();
return 0;
}

Need help for this Part

Wats wrong ??

it shows Int For float.............

You have a few errors:
Firstly, the if(input_value==0) ..... should be OUTSIDE the 'for' loop, because you want to check what kind of number the input is after you know it isn't a string nor a char.
Therefore you need to end the loop before this 'if' and remove the 'else' before it.
Secondly, in lines 38-39, the type is IS_STRING, because a number can't have more than a single dot.
Now just return the code you've put in the comments. It's necessary!
Lastly, you don't need the break; in lines 49 and 55, as they're not supposed to be in a loop

I think i corrected mistakes you have mentioned earlier........

Plz Now Say wats the Problem.........

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
//using std::cout;
//using std::cin;
//using std::endl;
using namespace std;
int main(void)
{
string s;
cout<<"Input a string/character/number:\n";
cin>>s;
short int input_type=0;
int x;
bool DotReached=false;
if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
{
input_type=IS_CHARACTER;
cout<< "its a char";
}
else
for(x=0;x<s.length();x++)
{
  if((s[x]>'9' || s[x]<'0') && s[x]!='.')
    {
     input_type=IS_STRING;
     cout<<" ITs an String ";
     break;
     }
}
//else 
if(input_type==0)
   {
    if(DotReached) 
     {
     input_type=IS_FLOAT;
     cout<<"Its a Floating Point Value";
     //break; 
     }
   else
     {
    input_type=IS_INTEGER;
    cout<<"Its an Integer";
     //break;
     }
   }
cin.get();
cin.get();
cin.get();
return 0;
}

That's because you removed this piece of code from your code:

else if(s[x]=='.')
{
   if(!DotReached) DotReached= true;

   else
   {
   input_type=IS_FLOAT; 
   cout<<"ITs a Float";
   break;
   }
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
//using std::cout;
//using std::cin;
//using std::endl;
using namespace std;
int main(void)
{
string s;
cout<<"Input a string/character/number:\n";
cin>>s;
short int input_type=0;
int x;
bool DotReached=false;
if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
{
input_type=IS_CHARACTER;
cout<< "its a char";
}
else
for(x=0;x<s.length();x++)
{
  if((s[x]>'9' || s[x]<'0') && s[x]!='.')
   {
     input_type=IS_STRING;
     cout<<" ITs an String ";
     break;
    }
     else if(s[x]=='.')
     {
     if(!DotReached) DotReached= true; 
     else
     {
     input_type=IS_FLOAT; 
     cout<<"Its a flt";
     break;
     }
    //else 
if(input_type==0) // the initial value - meaning no type is determined yet
   {
    if(DotReached) 
     {
     input_type=IS_FLOAT;
     cout<<"Its a Floating Point Value";
     //break; 
     }
   else
     {
    input_type=IS_INTEGER;
    cout<<"Its an Integer";
     //break;
     }
   }
cin.get();
cin.get();
cin.get();
return 0;
}
}
}

IS THIS SO ?????/
MENTION THE LINE IN HERE SIR
I CANT GET IT ....SIR

CAN YOU PLZ FILL WAT TO BE ADDED ........
PLZ.........

Lines 40-41 - it's a STRING, not a float. I've never heard of a number with two decimal dots.
Also, you didn't end the 'for' loop before the input_type check.
You can see it yourself.

COOL IT WORKS
I GOTTCHA

THNKS MAN

UR A KIND FULL PERSON MAN
NOW I AM HAPPY
Great job..........Mr.Unbeatable()..........

THAnks Mr.

heres the working Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
//using std::cout;
//using std::cin;
//using std::endl;
using namespace std;
int main(void)
{
 string s;
 cout<<"Input a string/character/number:\n";
 cin>>s;
 short int input_type=0;
 int x;
 bool DotReached=false;
 if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
 {
 input_type=IS_CHARACTER;
 cout<< "its a char";
 }
 else
 for(x=0;x<s.length();x++)
  {
   if((s[x]>'9' || s[x]<'0') && s[x]!='.')
    {
      input_type=IS_STRING;
      cout<<" ITs an String ";
      break;
    }
   else if(s[x]=='.')
    {
     if(!DotReached)
      { 
       DotReached= true; 
      }
   else
     {
      input_type=IS_STRING; 
      cout<<"Its a flt";
      break;
     }
   }
}
    //else 
if(input_type==0) // the initial value - meaning no type is determined yet
   {
    if(DotReached) 
     {
     input_type=IS_FLOAT;
     cout<<"Its a Floating Point Value";
     //break; 
     }
   else
     {
    input_type=IS_INTEGER;
    cout<<"Its an Integer";
     //break;
     }
   }
cin.get();
cin.get();
cin.get();
return 0;
}
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.