im having trouble understanding what this means if somebody can help explain to me what it stands for so I can go on doing my work.
The third function
o Use this prototype: int getUserNumber(string prompt);
o This function will prompt the user for a number (integer).
o This function will return the user-entered number to its caller
o Invoke this function from main in a loop order to get 3 valid numbers from the user
Which part exactly are you having trouble with?
The "prototype" you have there, all you need to do is turn is put it in a block instead of a semi-colon and add code to the function
1 2 3 4 5 6 7
int getUserNumber(string prompt); // prototype of function
int getUserNumber(string prompt)
{
//actually defining the function here
//need to return an int at the end
}
You can prompt for user input using cin.
1 2
int my_number;
cin >> my_number;
You'd then return the number that is put into that variable.
A basic for loop looks like this
1 2 3 4
for (int i = 0; i < 3; i++)
{
//the code in here will be executed 3 times
}
Inside here is where'd you call your getUesrNumber function.
ok and for me to call it on main would I put getUserNumber(string prompt) as well??
and in the loop statement would I ask the user to enter the numbers in a cout statements??
Whoops sorry for not responding sooner.
That does what the problem asks of you, so it isn't wrong per se.
You'll notice that, however, your function asks for 3 values, but only does something useful with (returns) the third prompted value.
I was just thinking more along the lines of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int getUserNumber(string prompt)
{
int myNumber=0;
cout << prompt;
cin >> myNumber;
return myNumber;
}
int main()
{
string prompt="enter a number:";
for (int i = 0; i < 3; i++)
{
int n = getUserNumber(prompt);
cout << "You have input " << n << ".\n";
}
return 0;
}