| | |
How to Find The user input ?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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 .
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 .
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
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)
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)
Last edited by unbeatable0; Dec 11th, 2008 at 11:19 am.
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
c++ Syntax (Toggle Plain Text)
#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 }
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;}
}
}
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;}
}
}
Last edited by ninja_gs; Dec 11th, 2008 at 2:12 pm.
" Known is A Drop - Unknown is An Ocean "
please use code tags, and avoid red text many people will be repelled by it!
This is to make a it easy for you to understand, basically writing IS_STRING is exactly the same as writing 1.
Create a boolean value can have the value of true or false
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.
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.
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
C++ Syntax (Toggle Plain Text)
define IS_STRING 1 define IS_CHARACTER 2 define IS_INTEGER 3 define IS_FLOAT 4
C++ Syntax (Toggle Plain Text)
bool DotReached=false;
C++ Syntax (Toggle Plain Text)
if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
C++ Syntax (Toggle Plain Text)
if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.') {input_type=IS_STRING; break;}
C++ Syntax (Toggle Plain Text)
{if(!DotReached) DotReached=ture; else {input_type=IS_STRING; break;}
Chris
Last edited by Freaky_Chris; Dec 11th, 2008 at 2:41 pm.
Knowledge is power -- But experience is everything
Please don't tag other questions onto the end of exsisting threads.
If the original problem is solved please mark this thread as solved and start a new topic (if nessasery).
This is probably the best:
http://www.microsoft.com/express/download/#webInstall
Mircosoft's Visual C++ Express 2008 (if your on windows)
Or you could use Code::Blocks
http://www.codeblocks.org/
Thanks,
Chris
If the original problem is solved please mark this thread as solved and start a new topic (if nessasery).
This is probably the best:
http://www.microsoft.com/express/download/#webInstall
Mircosoft's Visual C++ Express 2008 (if your on windows)
Or you could use Code::Blocks
http://www.codeblocks.org/
Thanks,
Chris
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- Tutorial: User Input: Strings and Numbers [C++] (C++)
- Tutorial: User Input: Strings and Numbers [C] (C)
- User input (Assembly)
- How to do Input in Python? (Python)
- Validating user input against text file (Python)
- Receiving user input to direct program (C++)
- error checking of user input (C++)
- user input and search (Java)
- help about programming (C++)
Other Threads in the C++ Forum
- Previous Thread: confusion
- Next Thread: c++ find word from text file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





