Problem with functions

So my program was working and then at one point something got screwed up. I am incredibly lost with what to do. The errors I get are:

Error 1 error C2065: 'cents' : undeclared identifier c:\users\j0e\documents\visual studio 2008\projects\assignment6pt1\assignment6pt1\assignment6pt1.cpp 40 Assignment6pt1

Error 2 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\users\j0e\documents\visual studio 2008\projects\assignment6pt1\assignment6pt1\assignment6pt1.cpp 40 Assignment6pt1

User is supposed to input the amount in cents between 1-99 and the program gives back how many quarters, dimes, and pennies. I first made this program without functions and since my assignment required I use functions, I added them and I think something got messed up during that process. I had 18 errors and got it down to 2 now. Any help would be amazing. Thank you.

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
#include <iostream>
using namespace std;

void input(int cents);
void coinQuarter(int quarters);
void coinDime(int dimes);
void coinPenny(int pennies);
void output(int& quarters, int& dimes, int& pennies);


int main()
{
   
    int cents;
    int quarters = 0;
    int dimes = 0;
    int pennies = 0;
    char repeat;

    do
    {
        input(cents);
        coinQuarter(quarters);
        coinDime(dimes);
        coinPenny(pennies);
        output(quarters,dimes,pennies);
    }
    while(repeat == 'y');
     return 0;
 }

void input(int cents)
{
    cout << "Enter your change from 1-99 cents: ";
    cin >> cents;
    cout << "\n";
}
void coinQuarter(int quarters)
{
    while (cents >= 25)
        {
            quarters++;
            cents -= 25;
        }
}
void coinDime(int dimes)
{
    while (cents >= 10)
        {
            dimes++;
            cents -= 10;
        }
}
void coinPenny(int pennies)
{
    while (cents >= 1)
        {
            pennies++;
            cents -= 1;
        }
}
void output(int& quarters, int& dimes, int& pennies)
{
    cout << "Your change can be divided up into the following:\n";
    cout << "Quarters: " << quarters << "\n";
    cout << "Dimes: " << dimes << "\n";
    cout << "Pennies: " << pennies << "\n";
    cout << "\n";
    cout << "Repeat program?(y/n) ";
    cin >> repeat;
    cout << "\n";
}
Your problem is that cents is a local variable. You can either move it to the global scope or pass it to every function that uses it.
It's possible you've not quite understood function parameters and the scope of variables.

1. Function main declared cents then passes it by value to input.

2. Function input has it's own distint variable called cents, which is different from cents in main. This parameter cents is given a value and the function returns. The value of cents in main remains unchanged.

3. The same for coinQuarters. Variable quarters is passed by value to coinQuarters. The only local variables that coinQuarters sees is it's parameter quarters. It does not see a variable called coinQuarte, and so the compilation fails because of that.

4. The same is true of the other functions too.
I figured it out. THanks for the replies!
Topic archived. No new replies allowed.