Tuesday, January 5, 2010

Simple Console Program & Code Snippet Embeding.

Happy New Year! There will lots of code altering and editing on this blog this year. Thus, displaying code in code snippets will be very effective. How do you display or embed code snippets on your blog? There are several ways you can embed scripts or code on your blog. One of the easiest ways is to use Microsoft Live Writer. Once you have that installed you may need a plug-in that helps with formatting and highlighting.

Here is an example with a simple Console Program I made:
   1: #include <iostream>

   2: #include <iomanip>

   3: #include <string>

   4:  

   5: using namespace std;

   6:  

   7: int main()

   8: {

   9:     //** Declarations **    

  10:     double hourpay, twoweeks, month, year;

  11:     int ans, weeksWorked;

  12:     string name;

  13:     bool flag = false; 

  14:  

  15:     cout << "Welcome to the Budget Console" << endl;

  16:     cout << endl;

  17:     cout << "Please enter your name: ";

  18:     getline(cin, name);

  19:     system("cls");

  20:  

  21:     cout << name << ", How much do you earn per hour: ";

  22:     cin >> hourpay;

  23:     cout << endl;

  24:     cout << "How much do you work in a week: ";

  25:     cin >> weeksWorked;

  26:     cout << endl;

  27:     system("cls");

  28:  

  29:     //** Menu **

  30:     while (!flag)

  31:     {

  32:     cout << "\t Menu" << endl;

  33:     cout << "1) Calculate how much I earn in 2 weeks."<< endl;

  34:     cout << "2) Calculate how much I earn in a month."<< endl;

  35:     cout << "3) Calculate how much I earn in a year: ";

  36:     cin >> ans;

  37:     cout << endl;

  38:  

  39:     //** Calculation **

  40:      twoweeks = hourpay * weeksWorked;

  41:      month = twoweeks * 2;

  42:      year = (twoweeks * 2) * 12;

  43:  

  44:     //** Outputs **

  45:     if (ans == 1) {         

  46:           cout << "Your Two Weeks Pay: $" << twoweeks << ".00 " << endl;

  47:           flag = true;

  48:     }

  49:     else if (ans == 2) {        

  50:         cout << "Your a month Pay: $" << month << ".00 " << endl;

  51:         flag = true;

  52:     }

  53:     else if (ans == 3) {        

  54:         cout << "Your a month Pay: $" << year << ".00 " << endl;

  55:         flag = true;

  56:     }

  57:     else {

  58:       cout << "Invalid Selection" << endl;

  59:     }

  60:       }

  61:     system("pause");

  62:     return 0;

  63: }

1 comment:

  1. I like it. I'll have to change the other posts of mine to use this.

    ReplyDelete