double read_double(string prompt) { string message; message = read_string(prompt); return convert_to_double(message); }
Computer Science
I had some code in which i allowed the user to input numbers for int value and then output it however now i want to allow the user to input double value as well so that means we will be using the same variable and store both values and output it.
int main()
{
// Declare variables
string name;
int age;
string birdsname;
int freq;
int amp;
double freq;
double amp;
// Get details of user, bird and the speed.
name = read_string("What is your name:");
age = read_integer("What is your age:");
birdsname = read_string("What is the birds name:");
freq = read_integer("Enter Frequency: ");
amp = read_integer("Enter Amplitude: ");
The above code is where i had the variables freq and amp in which we asked user to input integer whole numbers but now we want to use double values as well like 0.1 or 0.21 something like that. But i am not sure how can i do that because i can't obviously create 2 different inputs.
Its from a task so i have been advised to create the following function:
Design and implement a function declared as <output_type> read_double(string parameter) to read in a double from the user, with a parameter for the message to prompt with.
i have created something like the below code because we did same code before so its pretty similar. However i just don't know how we will be allowing the same variable freq and amp to take double and integer as well as outputing it using teh same variable. Can someone please help with that. Program i am using is visual studio code c++.
double read_double(string prompt)
{
string message;
message = read_string(prompt);
return convert_to_double(message);
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images