Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,631 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,066 people online right now. Registration is fast and FREE... Join Now!





Money! v1.0

A simple calculating program used to mock a checkbook. Shows simple but good use of "fstream" and "switch" statement.

Submitted By: ihatepikingnames
Actions:
Rating:
Views: 1,890

Language: C++

Last Modified: October 31, 2006
Instructions: Just complile Money.cpp. Don't forget to put the header in the include path.
PS: you could easily make the headers part of the code... its just not my style.

Snippet


  1. /*Money! v1.0 by Boomer 2006.10
  2. underscoredot@yahoo.com*/
  3.  
  4.  
  5.  
  6. //Money.cpp (save as so)
  7.  
  8.  
  9.     #include <iostream>   //<--*
  10.     #include <fstream>    //   *
  11.     using namespace std;  //   ***Good call Xing,
  12.       #include <add>      //   *  I'm just lazy.
  13.       #include <sub>      //<--*
  14.  
  15.     void add(); //in Add.h
  16.      void sub(); //in Sub.h
  17.     enum BOOL { FALSE, TRUE };
  18.     typedef unsigned short int USHORT;
  19.    
  20.      
  21.     USHORT menu();
  22.  
  23.  
  24.     int main()
  25.     {
  26.  
  27.         BOOL exit = FALSE;
  28.         for (;;)
  29.         {
  30.              USHORT choice = menu();
  31.              switch(choice)
  32.              {
  33.                 case (1):
  34.                      Add();
  35.                      break;
  36.                 case (2):
  37.                      Sub();
  38.                      break;
  39.                 case (3):
  40.                      exit=TRUE;
  41.                      break;
  42.                 default:
  43.                      cout << "Please select again!\n";
  44.                      break;
  45.              }           // end switch
  46.  
  47.              if (exit)
  48.                     break;
  49.         }                // end forever
  50.        return 0;
  51.     }                    // end main()
  52.  
  53.     USHORT menu()
  54.     {
  55.         USHORT choice;
  56.  
  57.         cout << "\n **** Money! ****\n\n";
  58.         cout << "(1) Add $$.\n";
  59.         cout << "(2) Subtract $$.\n";
  60.         cout << "(3) Exit.\n\n";
  61.  
  62.         cin >> choice;
  63.         return choice;
  64.     }
  65. //****************************************************
  66. //add.h (save as so)
  67.  
  68. float Option(float A, float B){return A+B;}
  69.  
  70.  
  71.      void Add()
  72.      {
  73.        char RUSure;
  74.        float numA, numB, numC;
  75.  
  76.  
  77.        ifstream datain ( "MoneybyBoomer.txt" );
  78.        datain>> numA;
  79.        cout<< "You currently have $"<< numA <<"\n";
  80.  
  81.       restart:                                                              //for goto statement
  82.        cout<< "How much would you like to add?" <<"\n";
  83.        cin>> numB;
  84.        cout << '\n' << "Is this the right amount? (Y)es or (N)o. ";
  85.        cin >> RUSure;
  86.  
  87.  
  88.  
  89.                while(RUSure == 'y')
  90.           {
  91.             numC = Option(numA, numB);
  92.              ofstream dataout ( "MoneybyBoomer.txt" );
  93.              dataout<< numC;
  94.             dataout.close();
  95.              datain>> numC;
  96.              cout<< "Well shit, now you have $" << numC << "!" << "\n";
  97.             break;                                                   
  98.           }
  99.  
  100.                    while(RUSure !='y'){goto restart;}                   
  101.  
  102.      }     
  103. //*****************************************************
  104. //sub.h (save as so)
  105.  
  106. float Boption(float C, float D){return C-D;}
  107.  
  108.  
  109.      void Sub()
  110.      {
  111.        char RUSure;
  112.        float numA, numB, numC;
  113.  
  114.  
  115.      ifstream datain ( "MoneybyBoomer.txt" );
  116.      datain>> numA;
  117.      cout<< "You currently have $"<< numA <<"\n";
  118.  
  119.       restart:                                                              //for goto statement
  120.      cout<< "How much would you like to Subtract?" <<"\n";
  121.      cin>> numB;
  122.      cout << '\n' << "Is this the right amount? (Y)es or (N)o. ";
  123.      cin >> RUSure;
  124.  
  125.  
  126.  
  127.                while(RUSure == 'y')
  128.           {
  129.             numC = Boption(numA, numB);
  130.               ofstream dataout ( "MoneybyBoomer.txt" );
  131.              dataout<< numC;
  132.             dataout.close();
  133.              datain>> numC;
  134.              cout<< "Well shit, now you have $" << numC << "!" << "\n";
  135.             break;
  136.           }
  137.  
  138.                    while(RUSure !='y'){goto restart;}
  139.  
  140.      }                                         //end sub()                                          //end add()
  141.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month