functions help

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??
and I followed your example and did this
1
2
3
4
5
6
7
8
9
10
11
12
13
int getUserNumber(string prompt)
{
  int myNumber=0;

  for(int i=0;i<3;i++)
    {
      cout<<"Please enter a number:"<<endl;
      cin>>myNumber;

    }
  return myNumber;
}

and this is what I get when I run it does it seem right
Please enter a number:
2
Please enter a number:
3
Please enter a number:
4
Last edited on
Looks good, but based off of the prompt
Invoke this function from main in a loop order to get 3 valid numbers from the user

It sounds like the for loop they want should be put in the main function, and the getUserNumber should be put put inside the for loop.

Also, notice how you're doing nothing with your string "prompt" parameter.
I think whoever wrote this intents on it being made like this

n = getUserNumber("Please enter a number: ");
Last edited on
hmmm that's where it hits me and I don't understand the most so could I do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
string prompt="enter a number:";

return 0;
}
int getUserNumber(string prompt)
{
int myNumber=0;
for(int i=0;i<3;i++)
    {
   cout<<prompt<<endl;
cin>>myNumber;

    }
return myNumber;
}
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;
}

Last edited on
ok thanks for the help
Last edited on
Topic archived. No new replies allowed.