Using C++: I\'m having a hard time understanding void and value returning functi
ID: 3753500 • Letter: U
Question
Using C++: I'm having a hard time understanding void and value returning functions and how they are used. Please help and explain how to do this.
Write a program that has three functions. (Two value returning and one void…)
The first function should be called getInt and should take a string to print, get the user’s input into an int and return back an int that is the user’s input. So you could use it like this:
int age = getInt("What is your age");
int cats = getInt("How many cats do you have?");
The second function should be called getStr and should work the same, except it
should return back a string/word from the user:
string word = getStr("What is your first name?”);
The final function should be a function called repeatIt. It should take a string and an int, and then repeat the given string the given number of times, on the same line, followed by an exclamation point(!) and an endl.
repeatIt("hi",5); // should print: hihihihihi!
Use the following code in your main to test your functions:
string word = getStr("Enter a word:");
int count = getInt("How many times to repeat it?");
repeatIt(word, count);
Explanation / Answer
#include<iostream>
using namespace std;
string getStr(string statement)
{
string value;
cout<<statement<<endl;
cin>>value;
return value;
}
int getInt(string statement)
{
int n;
cout<<statement<<endl;
cin>>n;
return n;
}
void repeatIt(string word,int count)
{
for(int i=0;i<count;i++)
cout<<word<<"!";
}
int main()
{
string word = getStr("Enter a word:");
int count = getInt("How many times to repeat it?");
repeatIt(word, count);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.