Another program I had to write for this chapter was a calculator program; I wrote it, and it was working just fine, but then I decided to include an option to continue or not in main, with a while-loop that would end once the user entered 'Q' or 'q' for the prompt "Continue (C == Continue, Q == Quit)?", but it isn't working correctly in that part of the code. It takes the answer, but then it doesn't go into the loop.
Here is the code:
Code:
#include <iostream>
using namespace std;
double calculator(string operation, double first_value, double second_value);
int main()
{
cout << "< -- Mini Calculator Program -- >\n";
cout << "Please provide operation (+, -, /, *): ";
string operation;
cin >> operation;
cin.ignore();
double first_value;
double second_value;
cout << "Please enter the first value: ";
cin >> first_value;
cin.ignore();
cout << "Please enter the second value: ";
cin >> second_value;
cin.ignore();
double result = calculator(operation, first_value, second_value);
cout << "The result of your calculation is: " << result << ".\n";
cout << "Would you like to continue?\n";
char answer;
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
char continuation;
while (continuation == 'C' || continuation == 'c')
{
calculator(operation, first_value, second_value);
cout << "Continue (C == Continue, Q == Quit)?\n";
cin >> continuation;
cin.ignore();
if (continuation == 'Q' || continuation == 'q')
{
cout << "Alright, thanks for using this calculator program.\nQuitting . . . \n";
return 0;
}
}
}
else
{
cout << "Alright, thanks for using this calculator program.\nQuitting . . . \n";
return 0;
}
cin.ignore();
}
double calculator(string operation, double first_value, double second_value)
{
double result;
if (operation == "+")
{
result = first_value + second_value;
}
else if (operation == "-")
{
result = first_value - second_value;
}
else if (operation == "/")
{
result = first_value / second_value;
}
else if (operation == "*")
{
result = first_value * second_value;
}
return result;
}
And I also got the coin program to work. Just let me know of a way to make it more efficient and simple, if there's a way to do that.
Code:
#include <iostream>
using namespace std;
double moneyCount(int pennies, int dimes, int nickels, int quarters, int half_dollars, int dollar_coins);
int main()
{
int pennies, dimes, nickels, quarters, half_dollars, dollar_coins;
cout << "Program to count amount of money in dollars and cents\n";
cout << "Please specify how many of each you have;\n";
cout << "pennies: ";
cin >> pennies;
cin.ignore();
cout << "dimes: ";
cin >> dimes;
cin.ignore();
cout << "nickels: ";
cin >> nickels;
cin.ignore();
cout << "quarters: ";
cin >> quarters;
cin.ignore();
cout << "half_dollars: ";
cin >> half_dollars;
cin.ignore();
cout << "dollar_coins: ";
cin >> dollar_coins;
cin.ignore();
double money_value = moneyCount(pennies, dimes, nickels, quarters, half_dollars, dollar_coins);
cout << "You have $" << money_value << ".\n";
}
double moneyCount(int pennies, int dimes, int nickels, int quarters, int half_dollars, int dollar_coins)
{
if (pennies > 1)
{
cout << "You have " << pennies << " pennies.\n";
}
else if (pennies == 1)
{
cout << "You have 1 penny.\n";
}
if (dimes > 1)
{
cout << "You have " << dimes << " dimes.\n";
}
else if (dimes == 1)
{
cout << "You have 1 dime.\n";
}
if (nickels > 1)
{
cout << "You have " << nickels << " nickels.\n";
}
else if (nickels == 1)
{
cout << "You have 1 nickel.\n";
}
if (quarters > 1)
{
cout << "You have " << quarters << " quarters.\n";
}
else if (quarters == 1)
{
cout << "You have 1 quarter.\n";
}
if (half_dollars > 1)
{
cout << "You have " << half_dollars << " half dollars.\n";
}
else if (half_dollars == 1)
{
cout << "You have 1 half dollar.\n";
}
if (dollar_coins > 1)
{
cout << "You have " << dollar_coins << " dollar coins.\n";
}
else if (dollar_coins == 1)
{
cout << "You have 1 dollar coin.\n";
}
int penny_value = pennies * 1, nickel_value = nickels * 5, dime_value = dimes * 10, quarter_value = quarters * 25;
int half_dollar_value = half_dollars * 50, dollar_value = dollar_coins * 100;
int total_coins = penny_value + nickel_value + dime_value + quarter_value + half_dollar_value + dollar_value;
double money_amount = total_coins * 1.0 / 100;
return money_amount;
}
Any help with either one or both would be much appreciated. Thanks in advance.