Function call with int and string

Hello folks. First post and desperate for help.

Please point me in the right direction. I've reading and coding all day yesterday and I still haven't figured out how to output the string literal "Enter the number of days: " from the function call.

Here is an exmaple of where my confusion is:
-----------
#include <iostream>
using namespace std;
//Is string header necessary?

void getDays (/* ??? */); //Function Prototype. My combinations haven't worked.


int main() //Cannot alter anything in main
{
int days;
getDays (days, "Enter the number of days: ");
cout <<"\nThe number of days is: " << days << endl;
return 0;
}

getDays (/* ???? */)//I need help with function header and body as well!
{
cout << ??;
cin >> days; //?? Keep getting errors with this being used without being
initialized.
}

-----------

Thank you very much!!!
Last edited on
My combinations haven't worked.

Ha ha! "Combinations". You're not supposed to do trial and error.

Right, so getDays (days, "Enter the number of days: "); shows that the getDays() function expects two parameters: the first one is int, the second is const char *.

1
2
3
4
void getDays(int i, const char *s)
{
    cout << s << endl;
}


... and then the plot thickens...
Are you sure you you have to write a function
void getDays(int, const char *); // two parameters
and not a
int getDays(const char *); // one parameter ?

Also, to quote code, use the <> button on the right menu when editing.
Thanks Catfish2!

However, does your suggestion for const char *s require the addition of char and assignment in main? Because I'm not allowed to alter or add anything to main.

As for the "thickening plot" hint, I'm under the impression that if the prototype has two arguments then so must the function header?
Your function has be declared the following way

1
2
3
4
5
void getDays( int &days, const char *prompt  )
{
   cout <<prompt;
   cin >> days; 
}
Last edited on
I tried this. Compiles ok but I get a runtime error telling me days is being used without being initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void getDays( int i, const char *s);

int main()
{
	int days;

	getDays(days, "Enter the number of days: ");
               cout << "The total days is: " << days << endl;

	return 0;
}

void getDays( int i, const char *s)
{
	cout << s << endl;
}
Last edited on
I showed you already the correct definition of the function.
Thank you vlad from moscow.
So this is the proper code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void getDays( int &days, const char *s);

int main()
{
	int days;

	getDays(days, "Enter the number of days: ");
	cout << "The total days is: " << days << endl;

	return 0;
}

void getDays( int &days, const char *s)
{
	cout << "Enter the number of days: ";
	cin >> days;
}


This compiled and debugged just fine.

I was fixated by the impression that the string in the call was somehow supposed to be outputted by some variation other than recreating it as a cout prompt.
Last edited on
I'm confused as to what const char *s is doing with the function call string "Enter the number of days: ", especially when I still have to retype it (as a cout prompt) in the function body.
It is your problem. I showed already how the function shall look.
Oh yeah you did. Thank you vlad from moscow. It's just that the char *s and the char *prompt are new concepts to me here. I don't know how I could have missed them in lecture or in the book if we're already studying functions.

So here's the finished product which runs perfectly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void getDays( int &days, const char *s);

int main()
{
	int days;

	getDays(days, "Enter the number of days: ");
	cout << "The total days is: " << days << endl;

	return 0;
}

void getDays( int &days, const char *prompt)
{
	cout << prompt;
	cin >> days;
}
I just realized that *s and *prompt are simply variables. My weakness is my understanding of how to pass the values from the call, and from the prototype to function body. I must study up!!!

Thank you all for the help! Your patience and knowledge is greatly appreciated!
days and prompt are names of parameters. They can be any identificators. Function declarations (and function definitions if they do not use some parameter) may omit parameter names. For example


void getDays( int &, const char *);
Last edited on
Thanks again for the clarification.

Could 'string' have been used somehow instead?
Yes of course. You can declare the function the following way

void getDays( int &, const std::string & );

and call it the same way without any changes

getDays(days, "Enter the number of days: ");

The definition of the function will be the same

1
2
3
4
5
void getDays( int &days, const std::string &prompt  )
{
   cout <<prompt;
   cin >> days; 
}



You have to include header <string>
Last edited on
Thank you! Problem solved!
... don't forget to look up "references" (a bad name for a language feature). In this case, "pass by value" vs. "pass by reference".
Topic archived. No new replies allowed.