943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 479
  • C++ RSS
Mar 5th, 2009
0

Is there an easy way to ignore...

Expand Post »
a percent sign? My program asks the user for a percentage rate. It then puts (with cin) the rate into a double. However...if a user tries to type a % after he enters a number...the program goes wack...
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
rickster11 is offline Offline
42 posts
since Nov 2007
Mar 5th, 2009
0

Re: Is there an easy way to ignore...

If you want the % sine then input the data as a string then convert to double laber so that the % symbol will be removed from the keyboard buffer.
C++ Syntax (Toggle Plain Text)
  1. std::string input;
  2. cout << "Enter a percent";
  3. getline(input, cin);
  4. double n;
  5. stringstream str(input);
  6. str >> n;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Mar 7th, 2009
0

Re: Is there an easy way to ignore...

Maybe you should take a look at the following code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. string usrInput;
  9. double dbl = 0;
  10. cout << "Enter a percent: ";
  11.  
  12. getline(cin, usrInput);
  13. cout << endl;
  14.  
  15. if(usrInput.c_str()[usrInput.length()-1] == '%')
  16. {
  17. usrInput.replace(usrInput.length()-1, 0, "");
  18.  
  19. cout << usrInput << endl;
  20.  
  21. dbl = atof(usrInput.c_str());
  22. } else {
  23. dbl = atof(usrInput.c_str());
  24. }
  25.  
  26. cout << "The double is: " << dbl << endl;
  27.  
  28. return 0;
  29. }

Using Ancient Dragon's method:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9. string str;
  10. double dbl = 0;
  11.  
  12. cout << "Type a number: ";
  13. cin >> str;
  14. cout << endl;
  15. stringstream ss(str);
  16. ss >> dbl;
  17. cout << "The number is: " << dbl;
  18.  
  19. return 0;
  20. }
Last edited by tux4life; Mar 7th, 2009 at 3:14 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 7th, 2009
1

Re: Is there an easy way to ignore...

Replace..
c++ Syntax (Toggle Plain Text)
  1. if(usrInput.c_str()[usrInput.length()-1]=='%')
  2. ...
with
c++ Syntax (Toggle Plain Text)
  1. if(usrInput[usrInput.length()-1] == '%')
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Mar 7th, 2009
0

Re: Is there an easy way to ignore...

Click to Expand / Collapse  Quote originally posted by cikara21 ...
Replace..
c++ Syntax (Toggle Plain Text)
  1. if(usrInput.c_str()[usrInput.length()-1]=='%')
  2. ...
with
c++ Syntax (Toggle Plain Text)
  1. if(usrInput[usrInput.length()-1] == '%')
  2. ...
Thank you for mentioning that, I didn't know that was possible...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Using Delete in class function
Next Thread in C++ Forum Timeline: New here.. going to spend a lot of time I presume





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC