C++ looping issues - c++

I am having difficulty locating and solving a bug in my basic ATM program. The issue manifests in the withdrawal option of both the checking and savings accounts. After the account has zero available funds it will allow the user to enter a negative number essentially over-drafting the account. I am trying to prevent this from happening.
Additionally, if there are zero funds available to withdraw the program continues to loop, thereby not allowing the user to utilize the menu to select a different option.
I have provided examples of the screen output illustrating the issues and the some of the source code.
Issue
Issue
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
double checking_balance = 2500.00, savings_balance = 1000.00, savings_amount,
checking_amount;
int menu;
int main() {
do {
// Main Menu
cout << std::fixed << std::setprecision(2);
cout << "\n";
cout << "\tWelcome to Seabreeze Bank\n";
cout << "*********************************\n\n";
cout << "1. Savings Account\n";
cout << "2. Checking Account\n";
cout << "3. Quit\n\n";
cin >> menu;
cout << "\n\n";
// User validation for the Main Menu
if (menu < 1 || menu > 3) {
cout << "You have entered an invalid option.\n";
cout << "Please enter a number 1-3 > ";
cin >> menu;
cout << "\n\n";
}
switch (menu) {
case 1:
int savings_menu;
do {
// Savings Account Menu
cout << "\t\tSavings Account\n\n";
cout << "Please enter a menu item (1-3) >\n";
cout << "*********************************\n";
cout << "1. Withdrawal\n";
cout << "2. Deposit\n";
cout << "3. Main Menu\n";
cout << "\n\n";
cin >> savings_menu;
cout << "\n";
// Withdrawal selection for Savings Account
if (savings_menu == 1) {
cout << "How much would you like to withdraw from your savings "
"account: ";
cin >> savings_amount;
cout << "\n";
while (savings_balance < savings_amount) {
cout << "You do not have enough funds in your account to withdraw "
"that much\n";
cout << "Please enter a smaller amount: ";
cin >> savings_amount;
cout << "\n";
}
while (savings_amount <= 0) {
cout << "Please enter an amount greater than 0: ";
cin >> savings_amount;
}
savings_balance -= savings_amount;
cout << "Your Savings Account Balance: " << savings_balance << "\n\n";
if (savings_balance == 0) {
cout << "You now have zero funds in your Savings Account.\n\n";
}
}

This is a floating point arithmetic problem.
In general, floating point arithmetic is not exact because of rounding errors, saving_amount is not equal to 0 but it is almost equal to 0.
I prefer the casting option because in certain cases using the round() function will need a bit more modification if the result is negative.
Instead of "while (savings_amount <= 0)" you should use while (int(saving_amount) <= 0). Similarly you have to do casting where you are using conditional operator in your program.
To prevent entering negative number to withdraw, you have to put an "if" statement to check if number is negative.

Related

how to add all in all the total in loop?

It is me again. I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed.
int main()
{
int choice;
int purchase;
int quantity;
double totalChoice1;
double totalChoice2;
char view;
char confirm;
char buyMore;
char look;
double alloy, apex, kraken, aorus;
double oppo, alpha, rog, huawei;
double ps4, nintendo, xbox, wii;
alloy = 69.99;
apex = 199;
kraken = 90;
aorus = 60;
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "--------------------" << endl;
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
cout << "[2] SteelSeries APEX PRO - $199" << endl;
cout << "[3] Razer Kraken X - $90" << endl;
cout << "[4] AORUS K7 - $60" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> purchase;
cout << "--------------------" << endl;
if (purchase == 1) {
cout << "How many would you like to purchase? ";
cin >> quantity;
totalChoice1 = quantity * alloy;
cout << "The total price for that is " << totalChoice1 << endl;
cout << "Confirm the Purchase? [Y]/[N]: ";
cin >> confirm;
if (confirm == 'Y') {
totalChoice1; // This is just a trial code.
cout << "Would you like to buy more items? [Y]/[N]: ";
cin >> buyMore;
}
else if (confirm == 'N') {
cout << "Do you still want to look for items? [Y]/[N]: ";
cin >> look;
if (look == 'N') {
break;
}
}
else {
break;
}
}
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
You are simply missing a variable to keep track of that total. Don't forget to give it an initial value of 0!
There are plenty of minor other issues with your code, so you'll have some more learning ahead. For instance, we find it easier to do bookkeeping in cents, because you can treat them as integers.

How can I accept user input differently from the same line in C++?

I have an assignment for my intro C++ class to make a program that has a user select C, D, or E to process a check, deposit a check, or end the program respectively. The professor specified that if a user chooses C or D, he would like to accept both the selection and the amount of money in the same line. For example, if a user wants to deposit a check for $20, they would enter "D 20" in one line. I have it set up as such:
cout << "Enter the beginning balance:\n\n";
cin >> balance;
cout << "Commands:\n";
cout << "C - process a check\n";
cout << "D - process a deposit\n";
cout << "E - end the program\n\n";
while (choice != 'E')
{
cout << "Enter a transaction:\n";
cin >> choice >> amount;
Followed by a switch statement. The code itself works properly when entering C or D, but when I go to enter E to end the program, it will only work if I add a number to the end, because the line asking for input wants a char and a float. However, in the example output my professor showed, you could just enter E and it would terminate. Is there any way around this? How can I set it up so it accepts E differently from C and D?
EDIT: Here is the switch statement:
switch(choice)
{
case 'C':cout << fixed << setprecision(2);
cout << "Processing check for $" << amount << endl;
balance = processCheck(amount, balance);
cout << "Balance: $" << balance << endl;
break;
case 'D':cout << fixed << setprecision(2);
cout << "Processing deposit for $" << amount << endl;
balance = depositCheck(amount, balance);
cout << "Balance: $" << balance << endl;
break;
case 'E':cout << "Processing end of month\n";
cout << "Final balance: $" << balance << endl;
break;
default : cout << "Invalid choice\n";
}
Already answered in comments, but anyway:
Replace this code
cin >> choice >> amount;
by gradual and conditional input code:
cin >> choice;
if (choice == 'C' || choice == 'D')
cin >> amount;

C++ Basic Menu Driven Program

I have a question that should be simple but I can't find the answer anywhere.
I have a menu driven C++ program that works perfectly when the menu options are numbers but I can't figure out how to change the numbers to letters.
For example:
works fine when choices are 1, 2, or 3 but not A, B, C.
Not sure how I am supposed to declare the option... is it a char? thanks in advance
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Variables needed for the problem
int numbServCharge; //number of service charges
char choice; //menu choice
double transAmt, balence, totalServCharge; //transaction amount, current balence, total service charges
//Constant service change
const double servCharge = 0.25; //constant $0.25 for service charges
numbServCharge = 0; //Start the counter for number of service charges at zero
cout << "Checkbook Balencing Program\n\n";
cout << "Enter the beginning balence: ";
cin >> balence; //get the initial balence
cout << endl;
do
{
//Highlight menu options
cout << "\nCommands\n";
cout << "C - process a check\n";
cout << "D - process a deposit\n";
cout << "E - end the program\n\n";
//Get user's choice from menu
cout << "Enter transaction type: ";
cin >> choice;
cout << endl;
//Create an error message for invalid choice and get a second choice
while((choice != 'C') && (choice != 'D') && (choice != 'E')
{
cout << "Invalid selection. Please choose C, D or E: ";
cin >> choice;
}
//Set up for option #1 -- using a check
if(choice=='C')
{
cout << "Enter transaction amount: ";
cin >> transAmt; //Get the amount of the check
cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
transAmt = transAmt + servCharge; //Add the service charge onto the transaction
numbServCharge++; //Add one to the number of service charges there have been
balence = balence - transAmt; //Update account balence
cout << "\nBalence: $" << fixed << setprecision(2) << balence;
cout << "\nService charge: $0.25 for a check\n";
totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
}
//Set up for option #2 -- deposits
if(choice=='D')
{
cout << "Enter transaction amout: ";
cin >> transAmt; //Get the amount of the deposit
cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
transAmt = transAmt - servCharge; //Add the service charge onto the deposit
numbServCharge++; //Add one to the number of service charges there have been
balence = balence + transAmt; //Update account balence
cout << "Balence: $" << fixed << setprecision(2) << balence << endl;
totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
}
}while(choice != 'E'); //Close program if option 3 is selected
//Display final balence
cout << "Processing end of the month";
cout << "\nFinal balence : $ " << fixed << setprecision(2) << balence << endl << endl;
system("pause"); //Pause screen so it doesn't close right away
return 0;
}
When testing for a string, you should convert everything to a common case. In you case you should convert the user input to upper case. You can do this by using toupper function
Here is the bit of code you need to change to make your program work
cout << "Enter transaction type: ";
cin >> choice;
choice = toupper(choice); // Change Here
cout << endl;
Once you change while((choice != 'C') && (choice != 'D') && (choice != 'E') to while((choice != 'C') && (choice != 'D') && (choice != 'E')), your code runs well. Although I would have personally used a std::string instead of a char.
Granted, Caesar's answer is a valid point - and I would change that as well. However, it would be better as a comment because it doesn't resolve the problem. Wait, hold on. What problem? Your program seems to still "run perfectly" even with the alphabetic menu options.
The only bug in your program is when you try and assign balence (or any of your three doubles) to a non-numeric entry. When I type "C" for initial balance, I see this monstrosity:
Over and over and over. That's not fun. Similar thing happens if I type a letter for transaction amount:
Solution: Never try to jam input taken directly from the user into a variable that is not a type of string. Use a built in string-to-number function like atof, or (much preferred) add error handlers like this:
if (std::cin >> dbl)
{
//input is a double. handle that here.
}
else
{
//input isn't a double. handle that here.
}
By the way, it's spelled balance, not balence ;)

C++ error with input validation in a do-while loop

I'm creating a very simple number guessing game for a school project and am having trouble with the repeating main menu. I created it using a do-while loop and the problem I'm having is that the menu selection variable is an int, and so when I (or the user) enters a non-int input by accident when selecting from the menu the }while(condition) at the end of the main loop can't catch it and the program repeats infinitely. Conversely if you enter an invalid int at menu selection the program catches it displays the "invalid input" message and then repeats the main menu.
It's kind of hard to explain in writing exactly what I mean so here is the source code with relevant lines denoted with an asterisk. I'm saving as .cpp and am compiling in linux using g++ -ansi -pedantic -Wall -Werror The teacher has forbidden hardcoding in conditional statements hence the global constants.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int PLAY = 1, HIGH_SCORE = 2, EXIT = 3;
const char YES = 'y', NO = 'n';
int main()
{
// Randomly generated value
int randomNumber;
// User input
int userGuess, menuChoice;
char repeat;
// Calculated value
int numberOfGuesses;
// Place-holder values (to be replaced by calculated values)
int score1 = 1000, score2 = 2000, score3 = 3000;
cout << endl << endl;
cout << "Greetings! This is a number guessing game where I think of" << endl
<< "a whole number between one and ten and you try to guess it!" << endl
<< "You can guess as many times as you like, so don't be afraid" << endl
<< "to use trial and error, but your score is based on the " << endl
<< "number of guesses you make (the lower the better) so don't " << endl
<< "guess too haphazardly. Remember, only guess whole numbers!" << endl
<< endl;
do
{
cout << endl << "Main menu." << endl
<< "1. Play game" << endl
<< "2. Display high scores" << endl
<< "3. Exit game" << endl
<< "Please select an option: ";
cin >> menuChoice;
if (cin.fail()){
cout << "Please enter a valid choice" << endl;
continue;
}
cin.ignore();
switch(menuChoice)
{
case PLAY:
do
{
unsigned seed = time(0);
srand(seed);
randomNumber = 1 + rand() % 10;
cout << endl << "Press enter when you're ready to begin!";
cin.ignore();
cout << "Ok I thought of one!" << endl << endl;
numberOfGuesses = 0;
do
{
numberOfGuesses++;
cout << "Enter your guess: ";
cin >> userGuess;
cin.ignore();
// Check user's guess
if (userGuess == randomNumber)
cout << "Correct! That was impressive!" << endl << endl;
else if (userGuess < randomNumber)
cout << "Not quite, you guessed low." << endl << endl;
else if (userGuess > randomNumber)
cout << "Not quite, you guessed high." << endl << endl;
}while (userGuess != randomNumber);
cout << "Your score for this game was " << numberOfGuesses << endl;
// Determine if a high score was beaten
if (numberOfGuesses <= score1)
{
score3 = score2;
score2 = score1;
score1 = numberOfGuesses;
cout << "That's a new all time high score!" << endl;
}
else if (numberOfGuesses <= score2)
{
score3 = score2;
score2 = numberOfGuesses;
cout << "That's a new high score!" << endl;
}
else if (numberOfGuesses <= score3)
{
score3 = numberOfGuesses;
cout << "That's a new high score!" << endl;
}
else
{
cout << endl;
}
cout << "Would you like to play again? y/n: ";
cin.get(repeat);
cin.ignore();
while (tolower(repeat) != YES && tolower(repeat) != NO)
{
cout << endl;
cout << "Sorry, that is an invalid choice." << endl
<< "Please enter 'y' for yes or 'n' for no: ";
cin.get(repeat);
cin.ignore();
}
}while (tolower(repeat) == YES);
break;
case HIGH_SCORE:
cout << endl << "High Score 1: " << score1 << endl
<< "High Score 2: " << score2 << endl
<< "High Score 3: " << score3 << endl << endl;
cout << "Press enter to continue. ";
cin.ignore();
break;
case EXIT:
cout << endl << "Thanks for playing, I'll see you next time!" << endl << endl;
break;
default:
cout << endl << "That is an invalid selection, please enter '1', '2' or '3'"
<< endl;
break;
}
}while (menuChoice != EXIT);
return 0;
}
Code Edited in regards to current answer.
Please let me know if you need anymore information, thanks in advanced!
Use cin.fail() like this (instead of just cin >> menuChoice;) (modelled after this post):
cin >> menuChoice;
if (cin.fail()) {
cout << "Please enter a valid choice" << endl;
cin.clear();
cin.ignore();
continue;
}
//Remove the cin.ignore() at this place!
For more detailed info, see this SO thread
Use a do-while to ensure that the loop body will run at least once.
By using a do-while and prompting a user outside the loop you assume the user wants to play the game once which may not be the case.
A cleaner approach IMO would be use a while loop. Display the menu outside the loop and at the end of the loop. The user will have the choice to exit immediately.
cout << "Greetings.....
cout << menu
// Get menuChoice input here.
while(menuChoice != EXIT){
...
cout << menu //reprompt at end to continue or exit cleanly
// Get menuChoice input here
}
Input Validation is a perfect time to use a do-while
do{
if(!cin){
cout << "Invalid input"
cin.clear()
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}while(!(cin >> menuChoice)) // This gets console input. If fail, loop.
Use numeric_limits<streamsize>::max() to completely clear the
buffer.
Use cin.clear() to reset the fail flag on cin so it wont
always be false.
cin.fail() is fine. However some would consider !cin more natural.

If else statement loop or repeat until true

I'm not sure how to loop or repeat a cin >> cashTendered if the amount tendered is less than the required amount.
So far, this is what I have.
if (cashTendered > || == total)
{
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
}
else
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
}
So now I want the if statement to repeat until true.
any suggestions?
Have a look at loops in c++. Also if condition can be shortened to >= instead of > || ==. Moreover, since you want to repeat until the cashTendered is >= total, you need a loop that checks the condition if cashTendered is < total.
while (cashTendered < total)
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
cin >> cashTendered;
}
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
while(true)
{
cin >> cashTendered
if (cashTendered >= total)
{
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
break;
}
else
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
}
}