I have been working on a program that Converts Dollars to coins. In my code it has to have multiple outputs to the screen, at least one input, the use of integers and strings, repetition with Do..While, If..Else, must have some output text to show correct value of coins that would be converted from the dollars, code must include comments explaining your reason for the code section or what the code is doing, code must compile whole dollars only. If value is less than 1 or 0, the program should break or exit. So far I have come up with this but get warning C4700: uninitialized local variable 'm' used and the same for warning C4700: uninitialized local variable 'c' used. Here is what I have:
#include <iostream>
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
#define pennies 1 // tells the code what pennies value is
#define dime 10 // tells the code what dime value is
#define quarter 25 // tells the code what dime value is
#define nickel 5 // tells the code what nickel value is
int main() // function main begins program execution
{
int m; // creates integer for dollar input
int p = pennies; // creates integer for pennies
int d = dime; // creates integer for dime
int q = quarter; // creates integer for quarter
int n = nickel; // creates integer for nickel
int c; // creates integer for coin input
int amount = ( m * 100 / c ); //answer for amount is m * 100 / c
cout << "Please enter in a dollar amount to convert it to change"; //prompts user
cin >> m; // user enters dollar amount
system("pause"); // keep window open to enter in c
cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
cout << "pennies or nickel or dime or quarter\n"; // choice of coins
cin >> c; //user inputs name of coin
{
if ( m <= 1 ) // if dollar amount if 1 or less end loop
return 0;
}
{
if ( c == pennies )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == nickel )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == dime )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == quarter )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
}
cout << "The result is:" << amount <<"'s in" << c << endl; // display message with amount from m * 100 / c
system("pause"); // keep window open
return 0; // indicates successful termination
}
I am sure I am screwing this up big time, and I am lost. I am new to this 4 weeks into learning C++. Please tell how I am screwing this up, any help or ideas would be appreciated.
Im working on it, I cant quite figure out whats wrong, as I am semi-new myself. But I saw that noone else replied so dedicated myself to help you figure it out. right now its saying The variable 'm' is being used without being initialized. Same with C.. But youve clearly placed them as "int m" "int c" So im not for sure.. =/
If you could find some way to #define them, I think thatd solve the problem.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//#include <string> <-- Not Needed
/// Use the preprocessor sparingly.
/*#define pennies 1
#define dime 10
#define quarter 25
#define nickel 5*/
int main()
{
// Changed macros to constants
constint PENNY = 1;
constint NICKEL = 5;
constint DIME = 10;
constint QUARTER = 25;
int money; // <-- Gave it a more descriptive name
/*int p = pennies;
int d = dime;
int q = quarter;
int n = nickel;*/ // <-- Not Needed
//int c;
//int amount; /* = ( m * 100 / c );*/ /// <-- m and c contain garbage, don't assign
cout << "Please enter in a dollar amount to convert it to change";
cin >> money; // <-- Reminder: Input is not validated.
//system("pause"); <-- Not needed
//cout << "To determine how many penny's, nickels, dimes or quarters, enter\n";
//cout << "pennies or nickel or dime or quarter\n";
//cin >> c;
//cout << ( m * 100 / c ) << endl;
if ( money < 1 ) // Changed this line ( if m is less than 1)
return -1; // Return a non-zero to indicate failure
/*if ( c == pennies )
return ( amount = m * 100 / c );
else if ( c == nickel )
return ( amount = m * 100 / c );
else if ( c == dime )
return ( amount = m * 100 / c );
else if ( c == quarter ) return ( amount = m * 100 / c );
}*/
cout << "Number of pennies: " << ((money * 100) / PENNY);
cout << "Number of nickels: " << ((money * 100) / NICKEL);
cout << "Number of dimes: " << ((money * 100) / DIME);
cout << "Number of quarters: " << ((money * 100) / QUARTER);
// cout << "The result is:" << amount <<"'s in" << c << endl;
// system("pause");
return 0; // Return zero, indicating a successful run.
}
Thanks Luc for the response and thank you for the example. If I wanted to have the user input a dollar amount and then also input what coin to divide by how would I do that? I made some changes per your recommendations and example to mine, now I only get one warning, : warning C4700: uninitialized local variable 'total' used.
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl
int main() // function main begins program execution
{
int money; // creates integer for money input
const int penny = 1; // creates constant integer for penny
const int dime = 10; // creates constant integer for dime
const int quarter = 25; // creates constant integer for quarter
const int nickel = 5; // creates constant integer for nickel
int coin; // creates integer for coin input
int total; //answer for amount is m * 100 / c
cout << "Please enter in a dollar amount to convert it to change"; //prompts user
cin >> money; // user enters dollar amount
cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
cout << "pennies or nickel or dime or quarter\n"; // choice of coins
cin >> coin; //user inputs name of coin
{
if ( money <= 1 ) // if dollar amount if 1 or less end loop
return -1;
else if ( coin == penny )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == nickel )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == dime )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == quarter )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
}
cout << "The result is:" << total <<"'s in" << coin << endl; // display message with amount from m * 100 / c
WHY DOES NO ONE USE CODE TAGS?? He just said that posts that don't use code tags go to the back of the line in his book. Well it's like that for 90% of this community and seeing as how he JUST said that I would think your next post would have code tags.
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl
int main() // function main begins program execution
{
int money; // creates integer for money input
constint penny = 1; // creates constant integer for penny
constint dime = 10; // creates constant integer for dime
constint quarter = 25; // creates constant integer for quarter
constint nickel = 5; // creates constant integer for nickel
int coin; // creates integer for coin input
int total; //answer for amount is m * 100 / c
cout << "Please enter in a dollar amount to convert it to change"; //prompts user
cin >> money; // user enters dollar amount
cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
cout << "pennies or nickel or dime or quarter\n"; // choice of coins
cin >> coin; //user inputs name of coin then coin total is displayed next to "The result is:"
{
if ( money <= 1 ) // if dollar amount if 1 or less end loop
return -1;
elseif ( coin == penny ) // if user types penny then
return ( total = ( money * 100 ) / coin ); // divide money by coin
elseif ( coin == nickel ) // if user types nickel then
return ( total = ( money * 100 ) / coin ); // divide money by coin
elseif ( coin == dime ) // if user types dime then
return ( total = ( money * 100 ) / coin ); // divide money by coin
elseif ( coin == quarter ) //if user types quarter
return ( total = ( money * 100 ) / coin ); // divide money by coin
}
cout << "The result is:" << total <<"'s in" << coin << endl; // display message with amount from m * 100 / c
system("pause"); // keep window open
return 0; // indicates successful termination
}
What do you want the user to enter at the menu? As it's written, to select pennies, the user types 1, for nickels they enter 5, dimes enter 10, quarters enter 25.
Most menus give a print-out of choices like so:
MENU
0) Cancel
1) Penny
2) Nickel
3) Dime
4) Quarter
Please Enter Selection:
either a word or number works for me, just as long as the user has to type the money in, then type the coin. I had put the pause earlier in the code at the beginning in a hopes of some how pausing the display window so the user could type in the money and coin, but could not get it to build. I would say a number corresponding to the coin amount would work best.
#include <iostream>
usingnamespace std;
int main()
{
// Common convention is that constants are all capital
constint PENNY = 1;
constint NICKEL = 5;
constint DIME = 10;
constint QUARTER = 25;
int money;
int coin;
cout << "Please enter in a dollar amount to convert it to change ";
cin >> money;
cout << "MENU\n"
<< "1) Pennies\n"
<< "2) Nickels\n"
<< "3) Dimes\n"
<< "4) Quarters" << endl;
char choice;
cin >> choice;
switch(choice)
{
case'1':
// Do Pennies Here
coin = PENNY;
break;
case'2':
// Do Nickels Here
coin = NICKEL;
break;
case'3':
// Do Dimes Here
coin = DIME;
break;
case'4':
// Do Quarters Here
coin = QUARTER;
break;
default:
// If the user entered an invalid choice handle it here.
return -1;
}
cout << "The result is: " << ((money * 100) / coin) << " in " << money << " dollars." << endl;
return 0;
}
OK I so I received no errors or warnings after I figured out how to initialize "total" is set total = 0. I can get all the outputs to show up, but everytime I enter in a dollar value and hit return it go right to the result is 0 instead of next prompting me to enter in a choice. Do I have to write an if statement to go between where I enter money and enter a choice for coin? Thanks for the tip on convention, I have to remember to follow suit.
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl
int main() // function main begins program execution
{
int money; // creates integer for money input
constint PENNY = 1; // creates constant integer for penny
constint DIME = 10; // creates constant integer for dime
constint QUARTER = 25; // creates constant integer for quarter
constint NICKEL = 5; // creates constant integer for nickel
int total = 0; //answer for amount is money * 100 / choice
cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user
cin >> money; // user enters dollar amount
cout << "To determine how many pennies, dimes, quarters or nickels, enter\n"; //display message
cout << "MENU\n"
<< "1) Pennies\n"
<< "2) Dimes\n"
<< "3) Quarters\n"
<< "4) Nickels" << endl;
char choice;
cin >> choice;
{
if ( money <= 1 ) // if dollar amount if 1 or less end loop
return -1;
elseif ( choice == 1 ) // if user types penny then
return ( total = ( money * 100 ) / PENNY ); // divide money by coin
elseif ( choice == 2 ) // if user types nickel then
return ( total = ( money * 100 ) / DIME ); // divide money by coin
elseif ( choice == 3 ) // if user types dime then
return ( total = ( money * 100 ) / QUARTER ); // divide money by coin
elseif ( choice == 4 ) //if user types quarter
return ( total = ( money * 100 ) / NICKEL ); // divide money by coin
}
cout << "The result is:" << total <<"'s in" << choice << endl; // display message with amount from m * 100 / c
system("pause"); // keep window open
return 0; // indicates successful termination
}