Dec 8, 2013 at 7:20pm UTC
I'm writing this utility that converts dollars to coins, but keep gettinmg this error message, (Run-Time Check Failure #3 - The variable 'amount' is being used without being initialized) and can't figure out whats wrong.I'm stuck and need some help.
Code
#include <iostream>
using std::cin; //program uses cin
using std::cout; //program uses cout
using std::fixed; //program uses fixed
using std::endl; //program uses endl
#include <iomanip> // sets parameterized stream manipulators
using std::setprecision; // sets numeric output precision
void ConvertToCoins(double);
int main ()
{
double amount;
while(true)
{
if(amount < 0) break;
ConvertToCoins(amount);
}
}
void ConvertToCoins(double amount)
{
static int quarters = 0;
static int dimes = 0;
static int nickles = 0;
static int pennies = 0;
if(amount >= .25)
{
quarters++;
ConvertToCoins(amount - .25);
}
else if(amount >= .10)
{
dimes++;
ConvertToCoins(amount - .10);
}
else if(amount >= .05)
{
nickles++;
ConvertToCoins(amount - .05);
}
else if(amount >= .01)
{
pennies++;
ConvertToCoins(amount - .01);
}
else
{
system ("cls");
}
{
cout << "Convert Dollars to Coins";
cout << "Program will show least coins possible";
cout << "--------------------------------------";
cout << "To exit the program enter -1: ";
cout << "--------------------------------------";
cin >> amount;
while (amount !=-1)
cout<<"Total Number of Coins:"<<endl;
cout << "--------------------"<<endl;
cout << "Quarters=" << quarters << "$" << (quarters * .25) <<endl;
cout << "Dimes=" << dimes << "$" << (dimes * .10 )<<endl;
cout << "Nickles=" << nickles << "$" << (nickles * .05) <<endl;
cout << "Pennies=" <<pennies << "$" <<( pennies * .01) <<endl;
cout << "--------------------" <<endl;
}
}
Last edited on Dec 8, 2013 at 7:28pm UTC
Dec 8, 2013 at 7:31pm UTC
1 2 3 4 5 6 7 8 9 10 11
int main()
{
double amount;
while (true )
{
if (amount < 0) break ;
ConvertToCoins(amount);
}
return 0;
}
Line 3 you declare your variable amount line 6 you try to test it and it has no initial value. And I would rethink your logic, while (true) is an infinite loop. I realize you are using break to get out of it, I am just not a fan of using break in this manner. And amount doesn't get updated so your loop will never end the way it is written.
Last edited on Dec 8, 2013 at 7:32pm UTC
Dec 8, 2013 at 8:37pm UTC
Thanks, did a few changes and now it works somewhat but still having problems at the end.
#include <iostream>
using std::cin; //program uses cin
using std::cout; //program uses cout
using std::fixed; //program uses fixed
using std::endl; //program uses endl
#include <iomanip> // sets parameterized stream manipulators
using std::setprecision; // sets numeric output precision
void ConvertToCoins(double);
int main ()
{
double amount = 0;
{
if(amount < 0);
ConvertToCoins(amount);
}
return 0;
}
void ConvertToCoins(double amt)
{
static int quarters = 0;
static int dimes = 0;
static int nickles = 0;
static int pennies = 0;
if(amt >= .25)
{
quarters++;
ConvertToCoins(amt - .25);
}
else if(amt >= .10)
{
dimes++;
ConvertToCoins(amt - .10);
}
else if(amt >= .05)
{
nickles++;
ConvertToCoins(amt - .05);
}
else if(amt >= .01)
{
pennies++;
ConvertToCoins(amt - .01);
}
else
{
system ("cls");
}
{
cout << "Convert Dollars to Coins \n";
cout << "Program will show least coins possible\n";
cout << "--------------------------------------\n";
cout << "To exit the program enter -1:\n ";
cout << "-------------------------------------\n";
cout << "Enter a $ amount...\n";
cin >> amt;
while (amt !=-1)
cout << "-----------------------\n";
cout << "Total Number of Coins:\n"<<endl;
cout << "----------------------\n"<<endl;
cout << "Quarters=" << quarters << "$" << (quarters * .25) <<endl;
cout << "Dimes=" << dimes << "$" << (dimes * .10 )<<endl;
cout << "Nickles=" << nickles << "$" << (nickles * .05) <<endl;
cout << "Pennies=" <<pennies << "$" <<( pennies * .01) <<endl;
cout << "--------------------" <<endl;
}
}
Last edited on Dec 8, 2013 at 8:39pm UTC