Advertisement
Guest User

c++ code

a guest
Jan 16th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. //============================================================================
  7. // Global definitions visible to all methods and classes
  8. //============================================================================
  9.  
  10. // FIXME (1): Define a data structure to hold bid information together as a single unit of storage.
  11. struct Info {
  12.     char title;
  13.     int fund;
  14.     int vehicleID;
  15.     int bidAmount;
  16. };
  17.  
  18. // FIXME (4): Display the bid values passed in data structure
  19. /**
  20.  * Display the bid information
  21.  *
  22.  * @param ?type? data structure containing the bid info
  23.  */
  24. void displayBid(Info itemOne) {
  25.     cout << "Title: " << itemOne.title << endl;
  26.     cout << "Fund: " << itemOne.fund << endl;
  27.     cout << "Vehicle: " << itemOne.vehicleID << endl;
  28.     cout << "Bid Amount: " << itemOne.bidAmount << endl;
  29.  
  30.     return;
  31. }
  32.  
  33. // FIXME (3): Store input values in data structure
  34. /**
  35.  * Prompt user for bid information
  36.  *
  37.  * @return data structure containing the bid info
  38.  */
  39. Info getBid() {
  40.     Info itemOne;
  41.  
  42.     cout << "Enter title: ";
  43.     cin.ignore();
  44.     getline(cin, itemOne.title);
  45.  
  46.     cout << "Enter fund: ";
  47.     cin >> itemOne.fund;
  48.  
  49.     cout << "Enter vehicle: ";
  50.     cin.ignore();
  51.     getline(cin, itemOne.vehicleID);
  52.  
  53.     cout << "Enter amount: ";
  54.     cin.ignore();
  55.     string strAmount;
  56.     getline(cin, strAmount);
  57.     itemOne.bidAmount = strToDouble(strAmount, '$');
  58.  
  59.     return;
  60. }
  61.  
  62. /**
  63.  * Simple C function to convert a string to a double
  64.  * after stripping out unwanted char
  65.  *
  66.  * credit: http://stackoverflow.com/a/24875936
  67.  *
  68.  * @param ch The character to strip out
  69.  */
  70. double strToDouble(string str, char ch) {
  71.     str.erase(remove(str.begin(), str.end(), ch), str.end());
  72.     return atof(str.c_str());
  73. }
  74.  
  75.  
  76. /**
  77.  * The one and only main() method
  78.  */
  79. int main() {
  80.  
  81.     // FIXME (2): Declare instance of data structure to hold bid information
  82.     Info itemOne;
  83.  
  84.     // loop to display menu until exit chosen
  85.     int choice = 0;
  86.     while (choice != 9) {
  87.         cout << "Menu:" << endl;
  88.         cout << "  1. Enter Bid" << endl;
  89.         cout << "  2. Display Bid" << endl;
  90.         cout << "  9. Exit" << endl;
  91.         cout << "Enter choice: ";
  92.         cin >> choice;
  93.  
  94.         // FIXME (5): Complete the method calls then test the program
  95.         switch (choice) {
  96.             case 1:
  97.                 itemOne = getBid();
  98.                 break;
  99.             case 2:
  100.                 displayBid(itemOne);
  101.                 break;
  102.         }
  103.     }
  104.  
  105.     cout << "Good bye." << endl;
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement