by
0
5
3,268
1
Top 1% !
Popular
Famous
Tagged
Specified
Popularity: 3911st place
Created
Modified Apr 18, 2013
This snippet is Public
LanguageC++
LicenseMIT_X11

C++ ~ Menu

Need's STRING input for the searchQuote() function.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/646/C-----Menu?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
#include <iostream> #include <string> #include <fstream> #include <windows.h> #include <h:\\Code\\C++\\input2010.h> const std::string TXTFILE = "file.dat"; using namespace std; struct customer{ int opNo, age, engine, noClaims; char area, name[21], sold; //Sold Y or N double bPremium, aPremium;//bPrem is total before discount. aPrem is total after discount. }; void createQuote(customer cust[], int &rows, double &base, double &extras); void readFile(customer customers[], int &rows); void writeFile ( customer customers[], int rows); void showArray( customer cust[], int rows); void pick( char &choice, customer cust[], int &rows, double base, double &extras, double &sold, double &notSold); void searchQuote(customer cust[], int &rows); void inputCustomerDetails(customer &cust, double base, double extras) { system("CLS"); cout << "Enter your name: \n"; Input(cust.name,20); cout << "Enter your age: \n"; Input(cust.age); if(cust.age < 25) extras += (.75*base); if ( cust.age > 50 ) extras += (.05*base); cout << "Please enter a Area (A,B or C): \n"; Input(cust.area); while (cust.area !='A' && cust.area !='B' && cust.area !='C') { cout << "Please enter a correct Area (A,B or C): \n"; Input(cust.area); } if (cust.area == 'A') extras += 0.01*base; if (cust.area == 'B') extras += .02*base; if (cust.area == 'C') extras += (.03*base); cout << "Enter your engine size in CC: \n"; Input(cust.engine); if(cust.engine >= 1300 && cust.engine <= 1500) extras += (.03*base); if (cust.engine > 1500) extras += (.04*base); cout << "Enter the amount of year without claims bonus:\n"; Input(cust.noClaims); cust.bPremium = base+extras; cust.aPremium = cust.bPremium-(cust.noClaims*.10*cust.bPremium); cout << cust.aPremium << endl; cout << "The extras we pay you is: \n"<<cust.aPremium << endl; } void readFile(customer customers[], int &rows) { ifstream myfile(TXTFILE,ios::binary); if (!myfile.is_open()) cout << "Unable to open file: " << TXTFILE << endl; else { while ( !myfile.eof() ) { myfile.read((char *)&customers[rows], sizeof(customer)); rows++; } rows--; myfile.close(); } } // WRITE OUT 1 RECORD AT A TIME void writeFile (customer customers[], int rows) { int r=0; ofstream myfile(TXTFILE,ios::binary); if (myfile.is_open()) { while ( r < rows ) { myfile.write((char *)&customers[r], sizeof(customer)); r++; } myfile.close(); cout << "Data written to " << TXTFILE << "\n"; } else cout << "Unable to open file: " << TXTFILE << endl; } void displayAll(customer cust[], int rows) { int sub=0; while ( sub < rows ) { cout << cust[sub].opNo << " " //* << cust[sub].name << " " << cust[sub].age << " " << cust[sub].engine << " " << cust[sub].area << " " << cust[sub].sold << " " //* << cust[sub].aPremium << " " << cust[sub].bPremium << endl; sub++; } } void searchQuote(customer cust[], int &rows) { clrscr(); char name[31]; cout << "Please enter search name" << endl; Input(name,30); for(int i=0;i<rows;i++) { if ( strnicmp(cust[i].name, name, strlen(name) ) == 0 ) { cout << cust[i].name << endl; } } } void add( customer cust[], int &rows, double base, double &extras, double &sold, double &notSold) { clrscr(); char quoteQuestion; inputCustomerDetails(cust[rows], base, extras); char yesno; cout<<"do you want to sell this quote?" << endl; Input(yesno); cust[rows].sold = yesno; if(yesno == 'y' || yesno == 'Y') sold += cust[rows].aPremium; else notSold += cust[rows].bPremium; rows++; } void menu(char &choice) { cout << "\n\t------------Main Menu-----------"; cout << "\n\t| |"; cout << "\n\t| |"; cout << "\n\t| (C)reate a QUOTE |"; cout << "\n\t| (D)isplay all QUOTE |"; cout << "\n\t| (S)earch by name |"; cout << "\n\t| (Q)uit |"; cout << "\n\t| |"; cout << "\n\t| |"; cout << "\n\t---------------------------------" << "\n\n>>>" ; Input(choice); } void pick(char &choice, customer cust[], int &rows, double base, double &extras, double &sold, double &notSold) { switch(choice) { case 'c' : add(cust, rows, base, extras, sold, notSold); break; case 'd' : displayAll(cust, rows); break; case 's' : searchQuote(cust, rows); break; case 'q' : break; default : "\n\tInvalid Input!"; } } void main() { char choice ='x'; customer cust[99]; int rows = 0; double extras = 0; double base = 100; char close = ' '; int opNo = 0; double gt[4] = {0,0,0,0}; char quoteQuestion = ' '; double sold[4] = {0,0,0,0}; double notSold[4] = {0,0,0,0}; system("Color 0A"); readFile(cust,rows); cout<<"Please enter your operator number..." << endl; Input(opNo); while (opNo !=0) { choice = 'x'; while (choice != 'q') { cust[rows].opNo = opNo; menu(choice); pick(choice,cust,rows,base,extras,sold[opNo],notSold[opNo]); } cout<<"Please enter your operator number..." << endl; Input(opNo); } cout << endl << endl; cout << "Total Sold for Operator 1: " << sold[1] << endl;//Displaying the amount sold cout << "Total Sold for Operator 2: " << sold[2] << endl; cout << "Total Sold for Operator 3: " << sold[3] << endl << endl; cout << " Not Sold for Operator 1: " << notSold[1] << endl;//Displaying the amount not sold cout << " Not Sold for Operator 2: " << notSold[2] << endl; cout << " Not Sold for Operator 3: " << notSold[3] << endl << endl; cout <<"The GTotal for Operator 1: " << gt[1] <<endl;//Displaying the grand total for each Operator. cout <<"The GTotal for Operator 2: " << gt[2] <<endl; cout <<"The GTotal for Operator 3: " << gt[3] <<endl; writeFile(cust, rows); system("pause"); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus