Is there an easy way to ignore...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 25
Reputation: rickster11 is an unknown quantity at this point 
Solved Threads: 0
rickster11 rickster11 is offline Offline
Light Poster

Is there an easy way to ignore...

 
0
  #1
Mar 5th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #2
Mar 5th, 2009
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.
  1. std::string input;
  2. cout << "Enter a percent";
  3. getline(input, cin);
  4. double n;
  5. stringstream str(input);
  6. str >> n;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #3
Mar 7th, 2009
Maybe you should take a look at the following code:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

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

 
1
  #4
Mar 7th, 2009
Replace..
  1. if(usrInput.c_str()[usrInput.length()-1]=='%')
  2. ...
with
  1. if(usrInput[usrInput.length()-1] == '%')
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #5
Mar 7th, 2009
Originally Posted by cikara21 View Post
Replace..
  1. if(usrInput.c_str()[usrInput.length()-1]=='%')
  2. ...
with
  1. if(usrInput[usrInput.length()-1] == '%')
  2. ...
Thank you for mentioning that, I didn't know that was possible...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
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