Function parameters

Hi there,
I need to write two different functions. First one should ask the user's name and get it as input and in the second one I must call the user with their name and ask them their budget. But I have problems calling user with their name in the second function. Can anybody help?

1
2
3
4
5
6
7
8
void Name(string name)
{	
	cout << "Please type your name: ";
	cin >> name;
	cout << endl;
	cout << "Welcome " << name << "! " << endl;
	cout << endl;
}

This is my first function btw.
Last edited on
pass the name by reference using the "&"

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
#include <iostream>
#include <string>

using namespace std;

void getName(string&);  // function prototype

int main()

{
   string name;

   getName(name); // calling the function getName;

   ....
   ....

}     // end main


void getName(string& theName)   // function definition start
{
   cout << "Please type your name: ";
	getline(cin, theName);

} // end function defintion


after the getName function has executed, any time you use name within the main it will use the name they entered.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    cout<< "That helped a lot. Thank you! (:" ;

   cin.ignore();
   cin.get();
   return 0;
}
Topic archived. No new replies allowed.