Question

In: Computer Science

Hello, In C++ how would I get it to ignore a percent sign in an input....

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"

Solutions

Expert Solution

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:


Related Solutions

Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
Kindly solve this in 15 minutes, and kindly ignore it if i did not get answer...
Kindly solve this in 15 minutes, and kindly ignore it if i did not get answer on time. Calculate mean, median and mode of the following    class Interval:              0-9       10-19   20-29   30-39   40-49   50-59   60-69 70-79 No. of students:           9        13        27        57        64        46        31        9
For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
Hello I would love to know the steps of how to be able to create a...
Hello I would love to know the steps of how to be able to create a Pearson's correlations chart in APa format from SPSS?
Hello! Html question. I am trying to add a background to a page. How would i...
Hello! Html question. I am trying to add a background to a page. How would i do this? I have tried using a jpg. The code is posted below. i used the background-image: url function. Any help of different ways to do this would be appreciated! Thank you! CODE: <!DOCTYPE html> <html> <head> <style> ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align:...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
Hello, I'm confused on part 'C' on how to graph the answer I received in part...
Hello, I'm confused on part 'C' on how to graph the answer I received in part 'B' B. A steam boiler is required as part of the design of a new plant. The types of fuel that can be used to ignite the boiler are natural gas, fuel oil, and coal. The cost of installation including all required controls is $40,000 for natural gas, $50,000 for fuel oil, and $120,000 for coal. In addition, the annual cost of fuel oil...
How do I get the following code to accept a float input, while also fixing the...
How do I get the following code to accept a float input, while also fixing the syntax errors in the code because I can't figure out what is wrong with it. def chemical_strength(pH): if 0<pH>14: if pH <= 7: if pH = 7: ans = str("neutral") else: # ph < 7 if 0<pH>=1: ans = str("very strong acid") else: # 1<pH>7 if 1<pH>4: ans = str("strong acid") else: # 4=<ph>7 if pH = 4: ans = str("plain acid") else: #...
Hello i dont get this problem determine the center of mass of the solid limited by...
Hello i dont get this problem determine the center of mass of the solid limited by the graphs of Y=x2, Y=x, Z=y+2 Z=0 if the density at point P is directly proportional to the distance from the XY plane
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT