In: Computer Science
Hello,
In C++ how would I get it to ignore a percent sign in an
input.
cout << "Enter the annual interest rate in %: ";
cin >> interestRate;
I want the user to input "3%" for example but having the % sign
makes the program terminate.
How can I get it to just read 3% (or any percent number) as just
"3"
SOLUTION:
Since the intention is to obtain only the interest rate ,
Define the variable type of 'interestRate' as integer (if interest rate is confirmed integer) or float (if interest rate can be decimal value too).
Defining the variable type of 'interestRate' will automatically neglect the '%' sign and store only the value in that variable.
Please refer to the code below: (refer to the screenshot of the code and output)
#include <iostream>
using namespace std;
int main()
{
float interestRate;
//take interestRate as input
cout<<"Enter the annual interest rate in %: ";
cin >> interestRate;
//display the interestRate
cout<<"Interest rate = "<<interestRate;
return 0;
}
Screenshot of the code and output: