Beginer needs help with reference variables

I have been asked to write a program that will take an integer from 1 to 99 and will output the amount of coins that equal that amount. Use coin denominations 25, 10, and 1, also use integer division and the % operator.

I was given a sample function...
void ComputeCoins (int CoinValue, int& number, int& AmountLeft)

I took a stab at writing the code but it does not work, logic and syntax errors is my guess but everything i know i learned in the past two weeks from a badly written text book, or this web site.
So any help would be greatly appreciated, I want to learn this.

When I try to run the program I get 'AmountLeft' declared as reference but not initialized

So what am I doing wrong?

#include <iostream>
#include <cmath>


char ans;

void ComputeCoins (int CoinValue, int& number, int& AmountLeft);


int main( )

{
using namespace std;

int& AmountLeft; (AmountLeft < 100);

do
{
cout << "Please enter an ammount from 1 to 99" " ";
cin >> AmountLeft;
cout << AmountLeft << " " "Cents will be\n";
cout << ComputeCoins(number, AmountLeft) << " " "quarters\n";
cout << ComputeCoins(number, AmountLeft) << " " "dimes\n";
cout << ComputeCoins(number) << " " "pennies\n";



cout << "would you like to make another calculation?\n";
cout << "Press y or n\n";
cin >> ans;

} while(ans == 'y' or ans == 'Y');

system("Pause");
return 0;
}

void ComputeCoins (int CoinValue, int& number, int& AmountLeft)
{
CoinValue =25;
number= Amountleft%CounValue;
return (number, AmountLeft);
}

void ComputeCoins (int CoinValue, int& number, int& AmountLeft)
{
CoinValue = 10;
number = AmountLeft%CoinValue;
return (number, AmountLeft);
}

void ComputeCoins (int& number, int& AmountLeft)
{
number = AmountLeft;
return number;
}

Last edited on
I am sorry to say this, but your program is so badly written (it is full of bugs!) that I hardly believe that you know what you are doing.

Also, it is unclear what the output of your program should be.

I do not know why, but it seems that people like to make their problems harder than they should be:

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

using namespace std;

int main()
{
    cout << "Please enter an ammount from 1 to 99:" << endl;
    int value;
    cin >> value;

    map<int, int> coins;

    coins.insert(pair<int, int>(1, 0));
    coins.insert(pair<int, int>(10, 0));
    coins.insert(pair<int, int>(25, 0));

    int sum = 0;

    for (map<int, int>::reverse_iterator it = coins.rbegin(); it != coins.rend(); it++)
    {
        while (sum + it->first <= value)
        {
            sum += it->first;
            it->second++;
        }
    }

    for (map<int, int>::reverse_iterator it = coins.rbegin(); it != coins.rend(); it++)
    {
        string coin_name = it->first == 25 ? "Quarters: "
                         : it->first == 10 ? "Dimes: "
                         : "Pennies: ";
        cout << coin_name << ' ' << it->second << endl;
    }

    return 0;
}

I know... I'm being a pain in the butt. ;)
Last edited on
To the OP,

You are still new at this. I try to read your code and it does not make any sense to me.

For starters...

int &AmmountLeft; (AmmountLeft<100);

This does not mean anything.
Instead use
int AmmountLeft;

At the end of your code you are redefining the same function void ComputeCoins, but two have matching parameter lists. This is a misuse of overloading. You can't do that.

I suggest you read the tutorial on this site.
Ya very new, and don't know what I'm diong. I tried to write this program how it asked in the book, with a void function to compute all 3 coin amounts, and use a reference variable for with the '&' symbol for amount left.

The problem is I don't understand how to initialize the reference variable

int &AmmountLeft; (AmmountLeft<100);

was me trying to say if the amount left is less than 100 then do the calculations. I knew it was wrong when i wrote it, but even after reading the tutorials on functions and reference variables, under the pointer section. I still don't know how to write the correct code. Did i miss something? I want to learn how to do this, my instructor has a different teaching style, and my text book is so ambiguously written it's hard to follow. I am in the process of finding a tutor, so i can understand this better. I'm sorry for my inadequate programing skills but practice makes perfect and i need a lot of practice.
Last edited on
Let me explain then,
when you pass a variable by reference, you can use the function to change the value of the variable. Here is a simple example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void someMath(int &a) {
a++;
}

int main() {
int myInt = 0;//declares and sets a value

someMath(myInt);//calls the function with myInt passed as reference
cout << myInt;

cout << endl << "press enter";
cin.get();//to pause the screen
return 0;
}


Here the function is able to change the value of myInt because it was passed by reference using the '&' symbol.
Here is a heavily commented solution:

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
#include <iostream> //We actually don't need to include <cmath>. You only need that for maths functions such as tangents, sine, powers etc...

using namespace std; //So we don't have to write std::cout everytime and std::cin everytime.

int main()
{
    int amount = 0; //This declares an integer and sets its initial value to zero.
    int coin25 = 0, coin10 = 0, coin1 = 0; //Integers which we will use to give our answer, also set to initial value of zero.
    char cont; //We will use this to ask the user if they want to run the program again.
    bool running = 1; //This is a booleon (a yes/no or on/off or 1/0 whatever you want to call it).
    //Basically, it can be 0 or 1 and we have set it to 1.
    while(running == 1) //While our booleon is equal to 1 (in C++ the test for equality is '=='), keep doing this loop of code.
    {
        amount = 0; //Set amount to zero so we will enter the following while loop.
        coin25 = 0; //And reset variables used by the program.
        coin10 = 0;
        coin1 = 0;
        while(amount < 1 || amount > 99) //While the amount entered is outside our 1-99 range.
        {
            cout << "Enter an integer from 1 - 99: "; //This loop will keep asking this until the user enters a valid number.*
            cin >> amount; //Get the value of amount from the user.
        }
        coin25 = amount / 25; //coin25 will become equal to the number of times 25 divides into amount.
        amount = amount % 25; //Then we check the remainder after division and this becomes the new value amount.
        coin10 = amount / 10; //Check how many times 10 divides into the new value of amount.
        amount = amount % 10; //Then we check the remainder after division, and this becomes the new value of amount.
        coin1 = amount / 1; //Finally, check how many times 1 divides into the new value of amount.
        cout << "You need " << coin25 << " 25 coins, " << coin10 << " 10 coints and " << coin1 << " 1 coins to make " << amount << "\n";
        //The above line: outputs our answer.
        cout << "Again? Y/N"; //Ask the user if they want to run the program again. **
        cin >> cont; //Get the answer in the form of a char.
        if(cont == 'n' || cont == 'N') //If the user has entered N or n.
        {
            running = 0; //If running becomes 0, the while loop all this code is running in will stop. See line 12.
        }
    }
    return 0;//All done!
}

//* Line 20: A more robust way exists of doing this, but for now, this will do. To see how this is not robust, try entering something like "word" or "a".

//** Line: 32: Similarly to above, there is a more robust way of doing this, but for now, this will be fine. 
Passing by reference is different then declaring a variable a reference.

Pretty much when you pass a variable by reference instead of creating a local copy of the variable you are passing by the address of that variable ( so if you edit the variable it will point to that address and modify that.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/


Also to why are you not including nickels?

By the way you should read up on parameters in a function

You are not even passing a copy of the coin value instead you are setting it equal in the function...

All that being said I wouldn't use a map that seems a bit overkill..


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

void compute_coins( int coin_value , int &number , int &amount )
{
    number = amount / coin_value; //being modified outside of function
    amount %= coin_value; //it is being modified outside of function
}

int main()
{
    int amount = 0 , number_of_coins = 0;
    const int penny = 1 , nickel = 5 , dime = 10 , quarter = 25; //an enum may work also
    char loop_answer = 'y';
    do
    {
        std::cout << "Please enter your amount in cents(1-99): ";
        std::cin >> amount;
        std::cout << "This means you have:" << std::endl;
        if( amount > 24 ) compute_coins( quarter , number_of_coins , amount );
        std::cout << number_of_coins << " quarters , ";
        number_of_coins = 0;
        if( amount > 9 ) compute_coins( dime , number_of_coins , amount );
        std::cout << number_of_coins << " dimes , ";
        number_of_coins = 0;
        if( amount > 4 ) compute_coins( nickel , number_of_coins , amount );
        std::cout << number_of_coins << " nickels , ";
        number_of_coins = 0;
        if( amount > 0 ) compute_coins( penny , number_of_coins , amount );
        std::cout << "and " << number_of_coins << " pennies." << std::endl;

        std::cout << "Please enter <y> to continue > ";
        std::cin >> loop_answer;

    } while( loop_answer == 'y' || loop_answer == 'Y' );
    return( 0 );
}


I honestly would put the output in the function and not even return the amount by reference..

Or even put a return type for the number and use a prototype like

1
2
3
4
5
6
7
8
9
10
11
12
int compute_coins( int coin_value , int &amount )
{
    int result = amount / coin_value;
    amount %= coin_value;
    return( result );
}

/*int main stuff*/

std::cout << compute_coins( penny , amount ) << " pennies" << std::endl;

/*int main stuff*/

thank you everyone, Mats the comments do help, and big thanx to giblit that tutorial helps a lot. I'll be reading everything i can on c++. Also thank you for the corrected program, I will study it excessively. As for not using nickels I have no idea why thats just what the book said, no nickels or half dollars.
Last edited on
Topic archived. No new replies allowed.