Skip to content

Instantly share code, notes, and snippets.

@kaosine
Created January 30, 2019 05:35
Show Gist options
  • Save kaosine/085ec672d27702c2f2898b20334ddb13 to your computer and use it in GitHub Desktop.
Save kaosine/085ec672d27702c2f2898b20334ddb13 to your computer and use it in GitHub Desktop.
project 4 for cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const float FIVEBLOCK = 1.50;
const float SINGLEBLOCK = 0.50;
void menu();
string msgConverter(string);
void fileProc();
double billCalc (int);
int main(){
string cName, stAddress, city, state, message, oString, zip;
int choice, wordsSent, change, recieved;
double total;
fstream bills;
choice = wordsSent = change = recieved = 0;
do
{
menu();
cin >> choice;
cin.ignore(256, '\n');
switch(choice)
{
case 1:// calculates bill
cName = stAddress = city = state = "";
zip = wordsSent = change = recieved = total = 0;
cout << "Enter the name of the customer: ";
getline(cin, cName);
cout << "Enter the customer's address: ";
getline(cin, stAddress);
cout << endl << "Enter city: ";
cin >> city;
cout << "Enter state: ";
cin >> state;
cout << "Enter zip code: ";
cin >> zip;
cout << "Enter the number of words sent: ";
cin >> wordsSent;
total = billCalc(wordsSent);
cout << endl << cName << endl << stAddress << endl;
cout << city << ", " << state << " " << zip << endl;
cout << "Amount Owed: $" << setprecision(2) << fixed << total << "\n";
total *= 100;
cout << "Enter the amount recieved from the customer: ";
cin >> recieved;
if (recieved >= total) {
change = recieved - (total);
cout << " Denomination Number" << endl;
cout << "-------------- ---------------" << endl;
cout << " Dollars " << change /100 << endl;
change %= 100;
cout << " Quarters " << change / 25 << endl;
change %= 25;
cout << " Dimes " << change / 10 << endl;
change %= 10;
cout << " Nickels " << change / 5 << endl;
change %= 5;
cout << " Pennies " << change << endl;
}
else
{
while (recieved < total)
{
cout << "Sorry this customer has not paid enough. ";
cin >> recieved;
}
}
break;
case 2: // converts message to morse code
message = " ";
cout << "Enter a message: ";
getline(cin, message);
message = msgConverter(message);
cout << message;
break;
case 3: // process file
fileProc();
break;
case 4: // ends program
cout << "\nThank you. Program ending. \n";
break;
default: //choice error
cout << "That's not a valid choice.\n\n";
break;
}
}while (choice != 4);
return 0;
}// end main
void menu()
{
cout << "\tWelcome to Western Union Telegraph Company\n \
1 - Process Telegram Bill\n\
2 - Translate to Morse Code\n\
3 - Process file\n\
4 - Quit\n\n";
cout << "Enter your choice: ";
} // end menu
void fileProc()
{
/*
Processes a data file and outputs it to the screen as each is read. Error
checks for file to not be open with the first loop, then
*/
string oString, cName, stAddress, city, state, zip;
int wSent = 0;
double total, fileTotal;
total = fileTotal = 0;
char avoid = ' ';
fstream bFile;
oString = cName = stAddress = city = state = zip = " ";
cout << "Enter a filename: ";
cin >> oString;
bFile.open(oString);
while (!bFile.is_open())
{
cout << "Error Occurred opening file.\n";
cout << "Please enter another file name: ";
cin >> oString;
bFile.open(oString);
}
while (bFile.good())
{
avoid = ' ';
total = 0;
getline(bFile, cName);
getline(bFile, stAddress);
bFile >> city;
bFile >> state;
bFile >> zip;
bFile >> wSent;
bFile >> avoid; // may seem weird but this clears the end of the record
total = billCalc(wSent);
fileTotal += total;
cout << cName << endl << stAddress << endl;
cout << city << ", " << state << " " << zip << endl;
cout << "Amount owed $" << fixed << setprecision(2) << total << "\n\n";
cout << avoid;
}
cout << "The total cost of all the telegram bills is $";
cout << setprecision(2) << fixed << fileTotal << endl << endl;
}
string msgConverter(string msg)
{
string finalMsg = " ";
for (int i = 0; i < msg.length(); i++)
{
if (msg.at(i) == ' ')
{
finalMsg += " ";
}
else if (msg.at(i) == 'A' || msg.at(i) == 'a')
{
finalMsg += " .- ";
}
else if (msg.at(i) == 'B' || msg.at(i) == 'b')
{
finalMsg += " -... ";
}
else if (msg.at(i) == 'C' || msg.at(i) == 'c')
{
finalMsg += " -.-. " ;
}
else if (msg.at(i) == 'D' || msg.at(i) == 'd')
{
finalMsg += " -.. " ;
}
else if (msg.at(i) == 'E' || msg.at(i) == 'e')
{
finalMsg += " . " ;
}
else if (msg.at(i) == 'F' || msg.at(i) == 'f')
{
finalMsg += " ..-. " ;
}
else if (msg.at(i) == 'G' || msg.at(i) == 'g')
{
finalMsg += " --. " ;
}
else if (msg.at(i) == 'H' || msg.at(i) == 'h')
{
finalMsg += " .... " ;
}
else if (msg.at(i) == 'I' || msg.at(i) == 'i')
{
finalMsg += " .. " ;
}
else if (msg.at(i) == 'J' || msg.at(i) == 'j')
{
finalMsg += " .--- " ;
}
else if (msg.at(i) == 'K' || msg.at(i) == 'k')
{
finalMsg += " -.- " ;
}
else if (msg.at(i) == 'L' || msg.at(i) == 'l')
{
finalMsg += " .-.. " ;
}
else if (msg.at(i) == 'M' || msg.at(i) == 'm')
{
finalMsg += " -- " ;
}
else if (msg.at(i) == 'N' || msg.at(i) == 'n')
{
finalMsg += " -. " ;
}
else if (msg.at(i) == 'O' || msg.at(i) == 'o')
{
finalMsg += " --- " ;
}
else if (msg.at(i) == 'P' || msg.at(i) == 'p')
{
finalMsg += " .--. " ;
}
else if (msg.at(i) == 'Q' || msg.at(i) == 'q')
{
finalMsg += " --.- " ;
}
else if (msg.at(i) == 'R' || msg.at(i) == 'r')
{
finalMsg += " .-. " ;
}
else if (msg.at(i) == 'S' || msg.at(i) == 's')
{
finalMsg += " ... " ;
}
else if (msg.at(i) == 'T' || msg.at(i) == 't')
{
finalMsg += " - " ;
}
else if (msg.at(i) == 'U' || msg.at(i) == 'u')
{
finalMsg +=" ..- " ;
}
else if (msg.at(i) == 'V' || msg.at(i) == 'v')
{
finalMsg +=" ...- " ;
}
else if (msg.at(i) == 'W' || msg.at(i) == 'w')
{
finalMsg +=" .-- " ;
}
else if (msg.at(i) == 'X' || msg.at(i) == 'x')
{
finalMsg +=" -..- " ;
}
else if (msg.at(i) == 'Y' || msg.at(i) == 'y')
{
finalMsg +=" -.-- " ;
}
else if (msg.at(i) == 'Z' || msg.at(i) == 'z')
{
finalMsg +=" --.. " ;
}
}
finalMsg += "\n\n";
return finalMsg;
}
double billCalc (int wSent) // calculates bill total
{
return (((wSent / 5) * FIVEBLOCK) + ((wSent % 5) * SINGLEBLOCK));
}// end bill calculation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment