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...

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.

std::string input;
cout << "Enter a percent";
getline(input, cin);
double n;
stringstream str(input);
str >> n;

Maybe you should take a look at the following code:

#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:

#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;
}

Replace..

if(usrInput.c_str()[usrInput.length()-1]=='%')
...

with

if(usrInput[usrInput.length()-1] == '%')
commented: Useful comment +1

Replace..

if(usrInput.c_str()[usrInput.length()-1]=='%')
...

with

if(usrInput[usrInput.length()-1] == '%')
...

Thank you for mentioning that, I didn't know that was possible...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.