Reputation: 29
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;
}
Upvotes: 0
Views: 4829
Reputation: 1854
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 double
s) 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 ;)
Upvotes: 0
Reputation: 9841
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;
Upvotes: 1