Beginnning C++ class functions HW need help.

I am not asking for the answers I just really need help this teacher decided to tack this on on top of our huge project which I am already confused on. I don't really know what I need to do or how to really do it since functions are brand new and still a bit confusing to me. Also before anyone asks this is an online class so it is hard to get in contact with teacher and he is rarely actually willing to help which is why I am here

Assignment:

Write a program with a function that asks the user for a number and the returns that number. Define the function with one string argument named 'prompt' that is displayed to the user when asking for input. Also, 'prompt' should have a default value.
Last edited on
Hey,

I can help a little but I'm also new so I can't explain too much. Anyway, I understand what they want you to do but can I see what you have so far?

Talemache~
I don't really have much of anything I am trying to figure out how to make a string into a function first, and how it would even work with a string when you need an integer back all I really have is the main method right now. I mean i got this code but i know it is wrong



#include <iostream>
#include <string>

using namespace std;

string prompt();

char num;

int main()
{
char number = num;
cout << "Thanks for entering: " << number << "\n\n";



return 0;
}

string prompt()
{
char num;



return num;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
using namespace std;

int prompt(string);

int main()
{
   string prompt = "This is your prompt!";
   int x;

   x = getNumber(prompt);
  
   cout << "The function returned: " << x << endl;

   return 0;
}

int getNumber(string prompt)
{
   int number;

   cout << prompt << endl;
   cout << "Please enter in any number: "; cin >> number;

   cout << "you entered: " << number;

  return(number);
}


something like this?
Last edited on
number isn't a type. Let us assume integer?

1
2
3
4
5
6
7
int promptfunc(string prompt = "missing requirements are annoying")
{
      cout << prompt << endl << "Please enter a number" << endl;
      int num;
      cin >> num;
      return num;
}


All sarcasm in the above is aimed at whoever wrote the problem, not you.

In all likelihood the teacher wants the prompt to BE the only text written out and ASSUMES prompt is passed in something like "Enter the number of things you want to do" in one place in the code and "Enter the number of hooligans in texas" in another place in the code and so on -- a re-usable data entry routine. But that is nothing at all like what the problem statement says.

You may also want to read into a string and convert it to an integer to ensure some idiot user does not type in "my name is groot" whenever asked for a number. That may be in an upcoming assignment though; its hard to say without more info.





Last edited on
What he said above. If you have any other questions I can try to answer.

Talemache~
Alright, this is what I got so far. I left in the missing requirement bit because I don't know what he means by giving it a default value. It would make sense for an integer but not a string. Hopefully, someone might have an Idea.

#include <iostream>
#include <string>
using namespace std;

int promptfunc(string prompt = "missing requirements are annoying");

int main()
{

int x;

x = promptfunc();

cout << "\nYour Returned Value is: " << x << endl;

return 0;
}

int promptfunc(string prompt)
{
cout << prompt << endl << "\nPlease enter a number" << endl;
int num;
cin >> num;
return num;
}
Last edited on
Make sure to put the code between "code and /code" (with [] around each one and without the quotes) when posting. That way people can run your code and see if it works. The default part is confusing btw. Strings are kinda (keyword kinda) Null by default (there are a lot of discussions about this like the difference between empty and null) so maybe they mean just put "" with nothing in between or actually using a null pointer. I don't know.
Talemache~


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


#include <iostream>
#include <string>
using namespace std;

int promptfunc(string prompt = "missing requirements are annoying");

int main()
{

int x;

x = promptfunc();

cout << "\nYour Returned Value is: " << x << endl;

return 0;
}

int promptfunc(string prompt)
{
cout << prompt << endl << "\nPlease enter a number" << endl;
int num;
cin >> num;
return num;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

// spec: write a function that asks the user for a number and the returns that number.
// define the function with one string argument named 'prompt' that is displayed
// to the user when asking for input. also, 'prompt' should have a default value.
//
// choose a sensible name for the function; a name that conveys meaning.
// avoid rubbish names like 'promptfunc'.
// also choose a reasonable default value for the prompt - "enter an integer: "
// it too should be something meaningful, that tells the user what input is expected.
//
// in real life, specifications are often somewhat sketchy; a programmer is expected to
// either seek clarification or make reasonable assumptions when required. in any case,
// the assumptions that can't be expressed in the code should be stated via comments.
//
// this function assumes that the user does enter a valid integer
int get_number( std::string prompt = "enter an integer: " )
{
    std::cout << prompt ; // 'prompt' is displayed to the user when asking for input.

    int number = 0 ;
    std::cin >> number ; // we will assume that the user does enter an integer
                         // (in practice, zero would be returned on input failure)

    return number ;
}

// write a program with a function ....
// a small main which exercises the function, once with the default prompt
// and a second time with a custom prompt
int main()
{
    const int x = get_number() ; // uses the default prompt ("enter an integer: ")
    std::cout << "you entered " << x << '\n' ;

    const int y = get_number( "and another one: " ) ; // uses the prompt "and another one: "
    std::cout << "the second number you entered is " << y << '\n' ;
}
Topic archived. No new replies allowed.