How To Get this ??

Thread Solved
Reply

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 Get this ??

 
0
  #1
Dec 16th, 2008
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.

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdio>
  5. #define IS_STRING 1
  6. #define IS_CHARACTER 2
  7. #define IS_INTEGER 3
  8. #define IS_FLOAT 4
  9. //using std::cout;
  10. //using std::cin;
  11. //using std::endl;
  12. using namespace std;
  13. int main(void)
  14. {
  15. string s;
  16. cout<<"Input a string/character/number:\n";
  17. cin>>s;
  18. short int input_type=0;
  19. int x;
  20. bool DotReached=false;
  21. if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
  22. {
  23. input_type=IS_CHARACTER;
  24. cout<< "its a char";
  25. }
  26. else
  27. for(x=0;x<s.length();x++)
  28. {
  29. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  30. {
  31. input_type=IS_STRING; break;
  32. cout<<" Its an integer ";
  33. }
  34. else if(s[x]=='.')
  35. {if(!DotReached) DotReached= true; else
  36. {
  37. input_type=IS_STRING;
  38. cout<<"ITs a Float";
  39. break;
  40. }
  41. }
  42. }
  43. cin.get();
  44. cin.get();
  45. return 0;
  46. }
Last edited by ninja_gs; Dec 16th, 2008 at 9:43 am.
" 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 Get this ??

 
0
  #2
Dec 16th, 2008
  1. for(x=0;x<s.length();x++)
  2. {
  3. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  4. {
  5. input_type=IS_STRING; break;
  6. cout<<" Its an integer ";
  7. }
  8.  
  9. else if(s[x]=='.')
  10. {
  11. if(!DotReached) DotReached= true; else
  12. {
  13. input_type=IS_STRING;
  14. cout<<"ITs a Float";
  15. break;
  16. }
  17. }
  18. }

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:
  1. if(input_type==0) // the initial value - meaning no type is determined yet
  2. {
  3. if(DotReached) input_type=IS_FLOAT;
  4. else input_type=IS_INTEGER;
  5. }
Last edited by unbeatable0; Dec 16th, 2008 at 10:30 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 Get this ??

 
0
  #3
Dec 16th, 2008
Cool i corrected it ......
thanks Mr Unbeatable()
It works but Also Say Its integer
when i enter Floating
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdio>
  5. #define IS_STRING 1
  6. #define IS_CHARACTER 2
  7. #define IS_INTEGER 3
  8. #define IS_FLOAT 4
  9. //using std::cout;
  10. //using std::cin;
  11. //using std::endl;
  12. using namespace std;
  13. int main(void)
  14. {
  15. string s;
  16. cout<<"Input a string/character/number:\n";
  17. cin>>s;
  18. short int input_type=0;
  19. int x;
  20. bool DotReached=false;
  21. if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
  22. {
  23. input_type=IS_CHARACTER;
  24. cout<< "its a char";
  25. }
  26. else
  27. for(x=0;x<s.length();x++)
  28. {
  29. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  30. {
  31. input_type=IS_STRING;
  32. cout<<" ITs an String ";
  33. break;
  34. }
  35. else if(input_type==0)
  36. {
  37. if(DotReached)
  38. {
  39. input_type=IS_FLOAT;
  40. cout<<"Its a Floating Point Value";
  41. break;
  42. }
  43. else
  44. {
  45. input_type=IS_INTEGER;
  46. cout<<"Its an Integer";
  47. break;
  48. }
  49. }
  50. }
  51. cin.get();
  52. cin.get();
  53. cin.get();
  54. return 0;
  55. }

Need help for this Part

Wats wrong ??

it shows Int For float.............
Last edited by ninja_gs; Dec 16th, 2008 at 12:04 pm.
" 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 Get this ??

 
0
  #4
Dec 16th, 2008
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
Last edited by unbeatable0; Dec 16th, 2008 at 11:50 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 Get this ??

 
0
  #5
Dec 16th, 2008
I think i corrected mistakes you have mentioned earlier........

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdio>
  5. #define IS_STRING 1
  6. #define IS_CHARACTER 2
  7. #define IS_INTEGER 3
  8. #define IS_FLOAT 4
  9. //using std::cout;
  10. //using std::cin;
  11. //using std::endl;
  12. using namespace std;
  13. int main(void)
  14. {
  15. string s;
  16. cout<<"Input a string/character/number:\n";
  17. cin>>s;
  18. short int input_type=0;
  19. int x;
  20. bool DotReached=false;
  21. if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
  22. {
  23. input_type=IS_CHARACTER;
  24. cout<< "its a char";
  25. }
  26. else
  27. for(x=0;x<s.length();x++)
  28. {
  29. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  30. {
  31. input_type=IS_STRING;
  32. cout<<" ITs an String ";
  33. break;
  34. }
  35. }
  36. //else
  37. if(input_type==0)
  38. {
  39. if(DotReached)
  40. {
  41. input_type=IS_FLOAT;
  42. cout<<"Its a Floating Point Value";
  43. //break;
  44. }
  45. else
  46. {
  47. input_type=IS_INTEGER;
  48. cout<<"Its an Integer";
  49. //break;
  50. }
  51. }
  52. cin.get();
  53. cin.get();
  54. cin.get();
  55. return 0;
  56. }
Last edited by ninja_gs; Dec 16th, 2008 at 12:10 pm.
" 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 Get this ??

 
0
  #6
Dec 16th, 2008
That's because you removed this piece of code from your code:
  1. else if(s[x]=='.')
  2. {
  3. if(!DotReached) DotReached= true;
  4.  
  5. else
  6. {
  7. input_type=IS_FLOAT;
  8. cout<<"ITs a Float";
  9. break;
  10. }
  11. }
Last edited by unbeatable0; Dec 16th, 2008 at 12:14 pm.
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 Get this ??

 
0
  #7
Dec 16th, 2008
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdio>
  5. #define IS_STRING 1
  6. #define IS_CHARACTER 2
  7. #define IS_INTEGER 3
  8. #define IS_FLOAT 4
  9. //using std::cout;
  10. //using std::cin;
  11. //using std::endl;
  12. using namespace std;
  13. int main(void)
  14. {
  15. string s;
  16. cout<<"Input a string/character/number:\n";
  17. cin>>s;
  18. short int input_type=0;
  19. int x;
  20. bool DotReached=false;
  21. if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
  22. {
  23. input_type=IS_CHARACTER;
  24. cout<< "its a char";
  25. }
  26. else
  27. for(x=0;x<s.length();x++)
  28. {
  29. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  30. {
  31. input_type=IS_STRING;
  32. cout<<" ITs an String ";
  33. break;
  34. }
  35. else if(s[x]=='.')
  36. {
  37. if(!DotReached) DotReached= true;
  38. else
  39. {
  40. input_type=IS_FLOAT;
  41. cout<<"Its a flt";
  42. break;
  43. }
  44. //else
  45. if(input_type==0) // the initial value - meaning no type is determined yet
  46. {
  47. if(DotReached)
  48. {
  49. input_type=IS_FLOAT;
  50. cout<<"Its a Floating Point Value";
  51. //break;
  52. }
  53. else
  54. {
  55. input_type=IS_INTEGER;
  56. cout<<"Its an Integer";
  57. //break;
  58. }
  59. }
  60. cin.get();
  61. cin.get();
  62. cin.get();
  63. return 0;
  64. }
  65. }
  66. }

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

CAN YOU PLZ FILL WAT TO BE ADDED ........
PLZ.........
Last edited by ninja_gs; Dec 16th, 2008 at 12:46 pm.
" 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 Get this ??

 
0
  #8
Dec 16th, 2008
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.
Last edited by unbeatable0; Dec 16th, 2008 at 12:51 pm.
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 Get this ??

 
0
  #9
Dec 16th, 2008
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
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdio>
  5. #define IS_STRING 1
  6. #define IS_CHARACTER 2
  7. #define IS_INTEGER 3
  8. #define IS_FLOAT 4
  9. //using std::cout;
  10. //using std::cin;
  11. //using std::endl;
  12. using namespace std;
  13. int main(void)
  14. {
  15. string s;
  16. cout<<"Input a string/character/number:\n";
  17. cin>>s;
  18. short int input_type=0;
  19. int x;
  20. bool DotReached=false;
  21. if(s.length()==1 && (s[0]>'9' || s[0]<'0'))
  22. {
  23. input_type=IS_CHARACTER;
  24. cout<< "its a char";
  25. }
  26. else
  27. for(x=0;x<s.length();x++)
  28. {
  29. if((s[x]>'9' || s[x]<'0') && s[x]!='.')
  30. {
  31. input_type=IS_STRING;
  32. cout<<" ITs an String ";
  33. break;
  34. }
  35. else if(s[x]=='.')
  36. {
  37. if(!DotReached)
  38. {
  39. DotReached= true;
  40. }
  41. else
  42. {
  43. input_type=IS_STRING;
  44. cout<<"Its a flt";
  45. break;
  46. }
  47. }
  48. }
  49. //else
  50. if(input_type==0) // the initial value - meaning no type is determined yet
  51. {
  52. if(DotReached)
  53. {
  54. input_type=IS_FLOAT;
  55. cout<<"Its a Floating Point Value";
  56. //break;
  57. }
  58. else
  59. {
  60. input_type=IS_INTEGER;
  61. cout<<"Its an Integer";
  62. //break;
  63. }
  64. }
  65. cin.get();
  66. cin.get();
  67. cin.get();
  68. return 0;
  69. }
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC