| | |
Is there an easy way to ignore...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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)
std::string input; cout << "Enter a percent"; getline(input, cin); double n; stringstream str(input); 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.
Maybe you should take a look at the following code:
Using Ancient Dragon's method:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main(void) { string usrInput; double dbl = 0; cout << "Enter a percent: "; getline(cin, usrInput); cout << endl; if(usrInput.c_str()[usrInput.length()-1] == '%') { usrInput.replace(usrInput.length()-1, 0, ""); cout << usrInput << endl; dbl = atof(usrInput.c_str()); } else { dbl = atof(usrInput.c_str()); } cout << "The double is: " << dbl << endl; return 0; }
Using Ancient Dragon's method:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> using namespace std; int main(void) { string str; double dbl = 0; cout << "Type a number: "; cin >> str; cout << endl; stringstream ss(str); ss >> dbl; cout << "The number is: " << dbl; return 0; }
Last edited by tux4life; Mar 7th, 2009 at 3:14 am.
Replace..
with
c++ Syntax (Toggle Plain Text)
if(usrInput.c_str()[usrInput.length()-1]=='%') ...
c++ Syntax (Toggle Plain Text)
if(usrInput[usrInput.length()-1] == '%')
•
•
•
•
Replace..
withc++ Syntax (Toggle Plain Text)
if(usrInput.c_str()[usrInput.length()-1]=='%') ...
c++ Syntax (Toggle Plain Text)
if(usrInput[usrInput.length()-1] == '%') ...
![]() |
Similar Threads
- how to cin.ignore Punctuation (C++)
- Can I modify my BIOS to ignore Fan? (Cases, Fans and Power Supplies)
- this should be an easy loop (C++)
- Ignore List (DaniWeb Community Feedback)
- Easy drop down menu? (JavaScript / DHTML / AJAX)
- getting an image to ignore my CSS or easy fix. (HTML and CSS)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- Graphics in Pixel,Mode13h:Part 1 (C++)
- Red Hat 9 and Windows 2000 dual boot (*nix Software)
- Tutorials for Linux (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Using Delete in class function
- Next Thread: New here.. going to spend a lot of time I presume
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib 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 rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






