Really don't get this function thing

Yeah, this program doesn't work like its supposed to.

Basically, its required to have 4 functions, 2 of them void, one of them returns a string and another returns an integer. It's an adding program of sorts, it counts the number of bills you have in the denominations (# of $1 bills, $5, etc.)

Multiplies the number of bills you have vs the value of the denominations and then sums them.

This all works fine when I use global variables, but the professor does not want that, he wants us to use methods (functions) which are super annoying. I clearly do not understand them, or I am missing something dumb. If you don't care to compile this, basically you enter your name, and it keeps asking for your name and doesn't repeat the string you inputted for every time it says your name, it returns no value, even though it should. Thank you in advance for your help. I really... really... don't get it... very frustrating. If you could please tell me what I am doing wrong... the functions return the variable inside the function to main, (unless they are void, which return nothing). Obviously I am doing something wrong because the string entered never gets saved, even though I saved it to a variable in main... Looks like this lab will be late, ugh, due 11:59:59 EST today. Rather get it correct, than be on time, though. Much, much obliged for your help.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// A program which will input the users' number of denomination of bills, multiply them to find the total amount of money in each denomination, and then sum all of the denominations for a grand total of money.


#include <iostream>
#include <string>

using namespace std;

string GetName()
{
	using namespace std;

	string inputName;

	cin >> inputName;

	return(inputName);

}

int TotalAmount(string)
{
	using namespace std;

	int TotalMoney;

	int Bills1; //numbers of bills
	int Bills2;
	int Bills5;
	int Bills10;
	int Bills20;
	int Bills50;
	int Bills100;
	int NumberOnes; //total amount of money of bills, i.e. 100 $1 bills = $100.
	int NumberTwos;
	int NumberFives;
	int NumberTens;
	int NumberTwenties;
	int NumberFifties;
	int NumberHundreds;

	cout << "How many $1 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills1;
	cout << "How many $2 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills2;
	cout << "How many $5 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills5;
	cout << "How many $10 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills10;
	cout << "How many $20 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills20;
	cout << "How many $50 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills50;
	cout << "How many $100 bills do you have, "; cout << GetName(); cout << "?" << endl;
	cin >> Bills100;
	
	NumberOnes = Bills1 * 1;
	NumberTwos = Bills2 * 2;
	NumberFives = Bills5 * 5;
	NumberTens = Bills10 * 10;
	NumberTwenties = Bills20 * 20;
	NumberFifties = Bills50 * 50;
	NumberHundreds = Bills100 * 100;

	TotalMoney = NumberOnes + NumberTwos + NumberFives + NumberTens + NumberTwenties + NumberFifties + NumberHundreds;

	return(TotalMoney);
}

void DisplayMoney(int, string)
{

	using namespace std;
	cout << "Congratulations, "; cout << GetName() << endl;


}

void ThankYouMsg(string)
{
	using namespace std;
	cout << "Thank you, "; cout << GetName(); cout << " for using my program.";

}

int main()
{
	using namespace std;

	string Name;
	int TotalMoney;


	Name = GetName();
	TotalMoney = TotalAmount();

	GetName();
	TotalAmount();
	DisplayMoney();
	ThankYouMsg();

	system("PAUSE");

	return(0);
}

Last edited on
make sure at the top of your program, under "using namespace std;" you declare your functions

your calculations look fine, but without declarations, nothing will return
I was taught you did not have to declare functions if they were at the top of the sequence and main was at the very bottom, in fact I have to do two versions of this program, one with prototyping and one without.
At line 94 you call the function GetName() and store the returned value in the variable Name. You should then pass that variable as a parameter to your other functions. There's no need to call GetName() repeatedly, just once will do.

A simplified example, note I added a parameter userName in the function ThankYouMsg()
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;

string GetName()
{
    string inputName;
    cout << "Please enter your name: ";
    cin >> inputName;

    return inputName;
}

void ThankYouMsg(string userName)
{
    cout << "Thank you, " << userName << " for using my program." << endl;
}

int main()
{
    string Name = GetName();

    ThankYouMsg(Name);

    return 0;
}
You are a life saver. Perfect, that's exactly what it was. They turn grey when you do that in Visual Studio, ill remember that for sure.

One more problem, though. below is my updated code. The top two functions GetName and TotalAmount repeat twice, while they should only be displayed once, and then continue to the final two functions.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>

using namespace std;

string GetName()
{
	using namespace std;

	string inputName;

	cout << "Hello, please enter your name." << endl;
	cin >> inputName;

	return(inputName);

}

int TotalAmount(string Name)
{
	using namespace std;

	int TotalMoney;

	int Bills1; //numbers of bills
	int Bills2;
	int Bills5;
	int Bills10;
	int Bills20;
	int Bills50;
	int Bills100;
	int NumberOnes; //total amount of money of bills, i.e. 100 $1 bills = $100.
	int NumberTwos;
	int NumberFives;
	int NumberTens;
	int NumberTwenties;
	int NumberFifties;
	int NumberHundreds;

	cout << "How many $1 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills1;
	cout << "How many $2 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills2;
	cout << "How many $5 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills5;
	cout << "How many $10 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills10;
	cout << "How many $20 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills20;
	cout << "How many $50 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills50;
	cout << "How many $100 bills do you have, "; cout << Name; cout << "?" << endl;
	cin >> Bills100;
	
	NumberOnes = Bills1 * 1;
	NumberTwos = Bills2 * 2;
	NumberFives = Bills5 * 5;
	NumberTens = Bills10 * 10;
	NumberTwenties = Bills20 * 20;
	NumberFifties = Bills50 * 50;
	NumberHundreds = Bills100 * 100;

	TotalMoney = NumberOnes + NumberTwos + NumberFives + NumberTens + NumberTwenties + NumberFifties + NumberHundreds;

	return(TotalMoney);
}

void DisplayMoney(int TotalMoney, string Name)
{

	using namespace std;
	cout << "Congratulations, "; cout << Name << endl;
	cout << "You have "; cout << TotalMoney;
	cout << "$";


}

void ThankYouMsg(string Name)
{
	using namespace std;
	cout << "Thank you, "; cout << Name; cout << " for using my program." << endl;

}

int main()
{
	using namespace std;

	string Name;
	int TotalMoney;

	Name = GetName();
	TotalMoney = TotalAmount(Name);

	GetName();
	TotalAmount(Name);
	DisplayMoney(TotalMoney, Name);
	ThankYouMsg(Name);

	system("PAUSE");

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