Question:
Trying to finish computer coin ++ program? #2 getting 7 warnings. crazy values?
dudenell
2007-12-19 04:22:33 UTC
/**********************************************************************
* Program Name: Lab 12
* Author: Daniel Carr
* Date: 12 December 2007
* Course/Section: CSC-110-001
* Program Description: This program will tell what coins to give out for
* any amount of change from 1 cent to 99 cents.
*
*
**********************************************************************/

/************************** Compiler Directives **********************/

#include

using namespace std;

/*********************** Global Data Declarations ********************/

//None in this program.

/************************** Function Prototypes **********************/

//Sorts two numbers in ascending order
void input(int money);
int Calculatechange(int change);

int Calculatechange(int change);

void computecoin ( int& amount,int& coins, int denomiation);
void Output(int change, int quarters, int dimes, int nickles, int pennies);



/**********************************************************************
* Function Name: main
* Author: Daniel Carr
* Date: 8 November 2007
* Function Description:
*
* Pseudocode:
* Level 0
* -------
*
*
*
* Level 1
* -------
*
**********************************************************************/

int main()
{
//Local variables
int money; //First user-entered number
int change; //Second user-entered number

/*************** Begin main Function Executables *****************/
//Variables

int quarters;
int dimes;
int pennies;
int nickles;


//Call function input
input(money);
//Call the function calculate
Calculatechange(change);
//Call function output
Output( change, quarters, dimes, nickles, pennies);




//Indicate to OS successful termination of program
return 0;

} //End main

/**********************************************************************
* Function Name: Input Money
* Author: Daniel Carr
* Date: 8 November 2007
* Function Description:
*
*
* Pseudocode:
* Level 0
* -------
* Enter amount of Money
*
* Level 1
* -------
* Enter amount of money
* Display "Enter amount of money (1-99)--->"
* Input Money
*
**********************************************************************/
void input(int money)

//void inputmoney(float& x, //INOUT: First number to be sorted
// float& y) //INOUT: Second number to be sorted
{
//Local variables

cout << "Enter amount of money (1-99)--->: ";
cin >> money;




} //End Input money

/**********************************************************************
* Function Name: Calculate Change
* Author: Daniel Carr
* Date: 8 November 2007
* Function Description: Calculate quarters, dimes, nickles, and pennies
*
*
* Pseudocode:
* Level 0
* -------
* Calculate Change
*
* Level 1
* -------
* Calcualte change
* Compute Quarters
* Compute Dimes
* Compute Nicklets and Pennies
* Level 2
* -------
* Compute Quarters
* Compute coin with Quarters, Change, and Q
* Compute Dimes
* Compute coin with Dimes, Change, and D
* Compute Nickles and Pennies
* Compute coin with Nickes Change and N,
*
*
*
*
*
*
*
**********************************************************************/
int Calculatechange(int change)
{
//constants
const int q=25;
const int d=10;
const int n=5;
//arguements
int amount;
int denomiation;
int coins;

//Call the function computer coin
computecoin(amount,coins,denomiation);

return coins;

}

/**********************************************************************
* Function Name: Compute Coin
* Author: Daniel Carr
* Date: 8 November 2007
* Function Description: compute number of coin and amount left
*
*
* Pseudocode:
* level 0
* -------
* Compute number of coins and amount left
* level 1
* -------
* Compute number of coins and amount left
* coin=amount/demination
* amount = amount%demination
*
**********************************************************************/
void computecoin ( int& amount,int& coins, int denomiation)
{
//Constistants

//Variables

//Arguements


coins = amount/denomiation;
amount= amount%denomiation;
amount %=denomiation;









}

/**********************************************************************
* Function Name: Display Change
* Author: Daniel Carr
* Date: 8 November 2007
* Function Description: compute number of coin and amount left
*
*
* Pseudocode:
* level 0
* -------
* Display Change
* level 1
* -------
* Display Change
* display Change& "coints can be given" EOL
* display "-----------------------" EOL
* display quarters & "Quarter(s)" EOL
* display dimes & "Dimes(s)" EOL
* display nickles & "Nickles(s) EOL
* display pennies & "Pennie(s) EOL
*
**********************************************************************/

void Output(int change, int quarters, int dimes, int nickles, int pennies)
{
//Aruments

//Consistants
//Variables

cout << change << "coins can be given" << endl;
cout << "-------------------------------------" << endl;
cout << quarters << "quarter(s)" << endl;
cout << dimes << "dimes(s)" << endl;
cout << nickles << "Nickle(s)" << endl;
cout << pennies << "Pennie(s)" << endl;







;}
Four answers:
mintfresh
2007-12-20 09:00:42 UTC
Dude just going over and fell on this ques. and found it interesting!!

hey give one of these guys a best ans so that i can know what solved ur query.

>_<
jplatt39
2007-12-19 05:01:26 UTC
Just some thoughts from a quick look. The warnings all seem to be "this or that variable is used without being initialized" and it specifies local variable. Local may or may not be significant. It is significant in inputmoney().



You see when you pass a value such as an int to a function in C or C++ you do exactly that. The function creates a new variable and COPIES the value in the variable you created into their copy. When the function finishes its manipulations and exits, it destroys its copy of the variable (goes out of scope). What do you do in inputmoney()? Try void inputmoney(int money);. In other words, inputmoney returns nothing and takes the value in money rather than the variable money as input.



You have two choices for this. The better way most of us ignore would be int inputmoney() and in the main program money=inputmoney(). The other obvious way would be to pass it the variable itself, either void inputmoney(int *money) (a pointer to the address of money) or void inputmoney(int &money) (the address of money which doesn't require dereferencing.



I haven't gone over this program with a fine tooth comb and found the locations of all the warnings. If I were you I would simply find the line numbers referenced in your warnings and make sure you are actually assigning values to specific variables and not assigning them to copies. The crazy values are simply the garbage values which were in the addresses the variables were assigned to before the program started.
That Guy
2007-12-19 09:32:07 UTC
First, congratulations for making a sincere effort to write this yourself. Second, jplatt39 gave a very good answer so make sure to understand its meaning.



Basically you had a number of errors all revolving around one concept - pass by value versus pass by reference (or pointer).



Remember that when you pass just the variable name to a function, a copy of what you are passing is made before the function is entered. All changes made in the function are made to that copy and the copy will be destroyed before returning to the calling function. Hence no changes to the variable will be reflected in the calling function.



For a called function to actually change a variable you need to pass it by reference or pointer. I changed you program to pass by reference some variable that need to be changed. Note the function signatures



void somefunc(int a); // by value

void somefunc(int& a); // by reference

void somefunc(int *a); // by pointer



and how they called:



somefunc(amount); // by value

somefunc(amount); // compiler knows to pass reference

somefunc(&amount); //by pointer





#include



using namespace std;



void input(int &money);

void Calculatechange(int amount, int& quarters, int& dimes, int& nickles, int& pennies);

void computecoin (int& amount, int& coins, int denomiation);

void Output(int change, int quarters, int dimes, int nickles, int pennies);







int main()

{



int amount; //First user-entered number

int quarters;

int dimes;

int pennies;

int nickles;



input(amount);



Calculatechange(amount, quarters, dimes, nickles, pennies);



Output(amount, quarters, dimes, nickles, pennies);



return 0;



}





void input(int& money)

{

cout << "Enter amount of money (1-99)--->: ";

cin >> money;



}





void Calculatechange(int amount, int& quarters, int& dimes, int& nickles, int& pennies)

{



const int q=25;

const int d=10;

const int n=5;

const int p=1;



computecoin(amount, quarters, q);

computecoin(amount, dimes, d);

computecoin(amount, nickles, n);

computecoin(amount, pennies, p);



}



void computecoin (int& amount, int& coins, int denomiation)

{



coins = amount/denomiation;

amount= amount%denomiation;

amount %=denomiation;

}





void Output(int change, int quarters, int dimes, int nickles, int pennies)

{



cout << change << " coins can be given" << endl;

cout << "-------------------------------------" << endl;

cout << quarters << " Quarter(s)" << endl;

cout << dimes << " Dimes(s)" << endl;

cout << nickles << " Nickle(s)" << endl;

cout << pennies << " Pennie(s)" << endl;

}
aravind_mib
2007-12-19 04:41:34 UTC
Set all the variables to zero initially

ie,

int money=0;... etc...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...