@Ganado,
Thank you. I see your point now and realize I was looking at it in a different way. In the future I will work on considering more possibilities.
Hello briancb2004,
Sorry for the delay.
For the code:
1 2 3 4 5 6 7 8
|
constexpr double
PENNY{ 0.01 },
NICKELS{ 0.05 },
DIMES{ 0.10 },
QUARTERS{ 0.25 };
double uPennies{}, uNickels{}, uDimes{}, uQuarters{};
double total{};
|
"constexpr" is available from C++11 on and is the preferred way to make a numeric variable a constant. The older form is just "const" and either will work.
The fact that the variable names are in capital letters tends to imply that they are constant variables whose value can not be changed. This is helpful when the compiler finds you are trying to change the value of a constant variable and stops with an error.
Line 7 defines the non-constant variables whose values do change in the program.
The {}s, available from C++11 on, known as the uniform initializer make it very easy to initialize your variables. The above code demonstrates 2 ways to use the uniform initializer. The empty {}s will set the variable to the type of zero needed based on the variables type.
Lastly when giving a "double" variable a value it is best to use "0.0" or "0.25" although "0" or ".02" will work it is more proper to use the former.
Using the last code you posted I have made a few adjustments for you to consider:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
constexpr double
PENNY{ 0.01 },
NICKELS{ 0.05 },
DIMES{ 0.10 },
QUARTERS{ 0.25 };
double uPennies{}, uNickels{}, uDimes{}, uQuarters{};
double total{};
std::cout << std::fixed << std::setprecision(2);
do
{
cout << "Enter number of pennies>> ";
cin >> uPennies;
cout << "Enter number of nickels>> ";
cin >> uNickels;
cout << "Enter number of dimes>> ";
cin >> uDimes;
cout << "Enter number of quarters>> ";
cin >> uQuarters;
uPennies *= PENNY;
uNickels *= NICKELS;
uDimes *= DIMES;
uQuarters *= QUARTERS;
total = uPennies + uNickels + uDimes + uQuarters;
if (total == 1.00)
{
cout << "\n\n Hooray your amazing you did it....Way to go!\n\n";
}
else if (total > 1.00)
{
cout << "\n Your guess was too high try again....\n\n";
}
else
{
cout << "\n Your guess of " << total << " was too low try again....\n\n";
}
//cout << total << endl; // <--- OK for testing, but no longer needed.
} while (total != 1.00);
return 0;
}
|
Line 2 I added for the use with the output. You do not have to include this header for the program to work, but you would have to comment line 17 or remove it.
Starting at line 21 and using the blank lines makes the code easier to follow and read.
Line 52 I changed. See what you think. Line 48 would work better if it matches the format of line 52. Line 43 is up to you if you want to do something different. I like to put some leading spaces at the beginning of some of the output just to make it look different. This part is up to you how you do it.
This is another version that would be more acurate than using "double"s:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
using namespace std;
int main()
{
constexpr int
PENNY{ 1 },
NICKELS{ 5 },
DIMES{ 10 },
QUARTERS{ 25 };
int uPennies{}, uNickels{}, uDimes{}, uQuarters{};
int total{};
do
{
cout << "Enter number of pennies>> ";
cin >> uPennies;
cout << "Enter number of nickels>> ";
cin >> uNickels;
cout << "Enter number of dimes>> ";
cin >> uDimes;
cout << "Enter number of quarters>> ";
cin >> uQuarters;
uPennies *= PENNY;
uNickels *= NICKELS;
uDimes *= DIMES;
uQuarters *= QUARTERS;
total = uPennies + uNickels + uDimes + uQuarters;
if (total == 100)
{
cout << "\n\n Hooray your amazing you did it....Way to go!\n\n";
}
else if (total > 100)
{
cout << "\n Your guess of " << total / 100 << '.'
<< (total % 100 < 10 ? "0" : "") << total % 100 << " was too high try again....\n\n";
}
else
{
cout << "\n Your guess of " << total / 100 << '.'
<< (total % 100 < 10 ? "0" : "") << total % 100 << " was too low try again....\n\n";
}
//cout << total << endl; // <--- OK for testing, but no longer needed.
} while (total != 100);
return 0;
}
|
Compare and see what you think.
Andy