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?
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.
}
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, constchar *s)
{
cout << s << endl;
}
... and then the plot thickens...
Are you sure you you have to write a function void getDays(int, constchar *); // two parameters
and not a int getDays(constchar *); // one parameter ?
Also, to quote code, use the <> button on the right menu when editing.
However, does your suggestion for constchar *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?
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>
usingnamespace std;
void getDays( int i, constchar *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, constchar *s)
{
cout << s << endl;
}
#include <iostream>
usingnamespace std;
void getDays( int &days, constchar *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, constchar *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.
I'm confused as to what constchar *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.
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:
#include <iostream>
usingnamespace std;
void getDays( int &days, constchar *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, constchar *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