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