How to Find The user input ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

How to Find The user input ?

 
0
  #1
Dec 11th, 2008
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 .
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: How to Find The user input ?

 
0
  #2
Dec 11th, 2008
Any One Help ..... please......
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: How to Find The user input ?

 
0
  #3
Dec 11th, 2008
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)
Last edited by unbeatable0; Dec 11th, 2008 at 11:19 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: How to Find The user input ?

 
0
  #4
Dec 11th, 2008
Can U Povide some part of codes
plz unbeatable0
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: How to Find The user input ?

 
1
  #5
Dec 11th, 2008
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. #define IS_STRING 1
  7. #define IS_CHARACTER 2
  8. #define IS_INTEGER 3
  9. #define IS_FLOAT 4
  10.  
  11. int main(void)
  12. {
  13. string Input;
  14. cout<<"Input a string/character/number:\n";
  15. cin>>Input;
  16. short int input_type=0;
  17. int x;
  18. bool DotReached=false;
  19. if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
  20. input_type=IS_CHARACTER;
  21. else
  22. for(x=0;x<Input.length();x++)
  23. {
  24. if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.')
  25. {input_type=IS_STRING; break;}
  26. else if(Input[x]=='.')
  27. {if(!DotReached) DotReached=ture; else
  28. {input_type=IS_STRING; break;}
  29. }
  30. }
  31. // continue from here
  32. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: How to Find The user input ?

 
0
  #6
Dec 11th, 2008
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;}

}
}
Last edited by ninja_gs; Dec 11th, 2008 at 2:12 pm.
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How to Find The user input ?

 
1
  #7
Dec 11th, 2008
please use code tags, and avoid red text many people will be repelled by it!
  1. define IS_STRING 1
  2. define IS_CHARACTER 2
  3. define IS_INTEGER 3
  4. 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.
  1. bool DotReached=false;
Create a boolean value can have the value of true or false
  1. 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.
  1. if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.')
  2. {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.
  1. {if(!DotReached) DotReached=ture; else
  2. {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
Last edited by Freaky_Chris; Dec 11th, 2008 at 2:41 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: How to Find The user input ?

 
0
  #8
Dec 11th, 2008
THank You Chrisssssss...............
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: How to Find The user input ?

 
0
  #9
Dec 11th, 2008
Plz Give me A good C++ compiler.........
links to download it
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How to Find The user input ?

 
0
  #10
Dec 11th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC