Coder Profile - Show off your skills, get a coder profile.
 
 
 
C++ And Me (A Love Story)
Programming
Recently I have really got into C++.
I used to think being able to create batch files was the way forward. Don't get me wrong, knowing how to create batch files is great before stepping up to C++ but now I know where my heart is!

I could of never created anything as complex as the code I will show now.
This is a combination of 3 separate sources codes I have posted, to make one big fantastic program!
It is simply called...Ed George's Math Room Program...ADVANCE!
Code :: Ed George Math Room Program Adv. Copy / Restore
  1. /*
  2.                                           ed george 2008
  3.  
  4. Please Enter Version and Update Date                                                                                                     */
  5.  
  6.  
  7. #include <iostream>
  8. #include <cmath>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. int main(){
  14.           string version;
  15.           version = "1.2.0";
  16.           string update;
  17.           update = "02/03/08";
  18.                       char ch = 0;
  19.           int choice(0);
  20.           int schoice(0);
  21.      const double PI = 3.14159;
  22.      cout << "---------------------------"<<endl;
  23.      cout<<"Welcome to Ed George's Math Class Program Version "<<version<<endl; //Version = Build, Major, Minor
  24.      cout << "---------------------------"<<endl;
  25.      cout<<"Enter A for Sphere Tool"<<endl;
  26.      cout<<"Enter B for Gradient Tool"<<endl;
  27.     cout<<"Enter C for Shape Tool"<<endl;
  28.     cout<<"Enter D for Info About this Program"<<endl;
  29.      cout<<"Enter E to Quit"<<endl;
  30.      cin >> ch;
  31.      ch = toupper (ch);
  32.  
  33.  
  34.  
  35. switch (ch)
  36.           {
  37.           case 'A':
  38.  
  39.  
  40.            do
  41.            {
  42.  
  43.                      cout<<"--Sphere Tool--"<<endl;
  44.                      cout<<"0: Exit"<<endl;
  45.                      cout<<"1: Find Surface Area and Volume"<<endl;
  46.                      cout<<"2: Find Radius Using Surface Area"<<endl;
  47.                      cout << "---------------------------"<<endl;
  48.                      cin>>choice;
  49.  
  50.                      if (choice == 1)
  51.                      {
  52.                                double r(0);
  53.                                double sa(0);
  54.                                double v(0);
  55.  
  56.                                do
  57.                                {
  58.                                          cout<<"Enter radius: ";
  59.                                          cin>>r;
  60.                                } while (r < 0);
  61.  
  62.                                sa = 4 * PI * (r*r);
  63.                                v = (4.0 * PI * (r*r*r) ) / 3.0;
  64.  
  65.                                cout<<"Surface area: "<<sa<<endl;
  66.                                cout<<"Volume: "<<v<<endl;
  67.                      }
  68.                      else if (choice == 2)
  69.                      {
  70.                                double r(0);
  71.                                double sa(0);
  72.  
  73.                                do
  74.                                {
  75.                                          cout<<"Enter surface area: ";
  76.                                          cin>>sa;
  77.                                } while (sa < 0);
  78.  
  79.                                r = sqrt( sa / (4*PI) );
  80.                                cout<<"Radius: "<<r<<endl;
  81.  
  82.                      }
  83.                      else if (choice == 0)
  84.                      {
  85.                                cout<<"Good bye!"<<endl;
  86.                      }
  87.                      else
  88.                      {
  89.                                cout<<"Invalid input!"<<endl;
  90.                      }
  91.  
  92.            } while (choice != 0);
  93.  
  94.  
  95.                      break;
  96.  
  97.           case 'B':
  98.  
  99.    cout << "---------------------------"<<endl;
  100.    cout << "Welcome to Ed George's C++ Gradient Finder tool - Version 1.1.5"<<endl;
  101.    cout << "Use this tool to find the gradient of a line"<<endl;
  102.    cout << "---------------------------"<<endl;
  103.    cout << "x1 and x2 are the two different x co-ordinates in a pair of co-ordinates"<<endl;
  104.    cout << "e.g. (3,4) & (5,6)"<<endl;
  105.    cout << "x1 would be 3 and x2 would be 5"<<endl;
  106.    cout << "Its the same with y1 and y2."<<endl;
  107.    cout << "Using the same example: y1 would be 4 and y2 would be 6."<<endl;
  108.  
  109.  
  110.    int a;
  111.  
  112. do
  113.    {
  114.   double x1, x2, y1, y2, slope, mid1, mid2, dist, A, B;
  115.  
  116.  
  117.       cout << "---------------------------"<<endl;
  118.       cout<< "Please enter a value for x1: ";
  119.       cin >> x1;
  120.       cout << "Please enter a value for x2: ";
  121.       cin >> x2;
  122.       cout << "Please enter a value for y1: ";
  123.       cin >> y1;
  124.       cout << "Please enter a value for y2: ";
  125.       cin >> y2;
  126.       cout << "---------------------------"<<endl;
  127.  
  128.       //Calculating the results for gradient(slope):
  129.       slope = (y2 - y1)/(x2 - x1);
  130.  
  131.       //Mid point formula broken into 2 sections for x and y:
  132.       mid1 = (x1 + x2)/2;
  133.       mid2 = (y1 + y2)/2;
  134.  
  135.       //Distance Formulae
  136.       A = pow(x1 - x2, 2);
  137.       B = pow(y1 - y2, 2);
  138.       dist = sqrt(A + B);
  139.  
  140.  
  141.       cout << "Results:"<<endl;
  142.       cout << "\nThe slope of the line is = " << slope <<endl;
  143.       cout << "\nThe midpoint of the line is (" << mid1 << "," << mid2 << ")"<<endl;
  144.       cout << "\nThe distance of the line segment is = " << dist <<endl;
  145.       cout << "---------------------------"<<endl;
  146.  
  147.  
  148.  
  149.            cout << "If you would like to Continue using this Program, Enter 1"<<endl;
  150.            cout << "For Help please press 2"<<endl;
  151.            cout << "To EXIT Please Go To Help (2) and then Enter \"3\" when prompted: ";
  152.  
  153.    cin >> a;
  154.  
  155.  
  156.  
  157.    } while ( a == 1 );
  158.  
  159.    do
  160.    {
  161.  
  162.            cout << "---------------------------"<<endl;
  163.            cout << "Welcome to the help section."<<endl;
  164.            cout << "---------------------------"<<endl;
  165.            cout << "To use this program properly you must know two co-ordinates that a line passes"<<endl;
  166.            cout << "To find the gradient."<<endl;
  167.            cout <<"e.g. y=x^2 (squared)"<<endl;
  168.            cout << "(2,4) and (3,9) are both on the line"<<endl;
  169.            cout << "Therefore: "<<endl;
  170.            cout << "x1 = 2"<<endl;
  171.            cout << "x2 = 3"<<endl;
  172.            cout << "y1 = 4"<<endl;
  173.            cout << "y2 = 9"<<endl;
  174.            cout << "---------"<<endl;
  175.            cout << "Please note that if any value is (example) 6.02e23"<<endl;
  176.            cout << "It simply means 6.02 x 10^23"<<endl;
  177.            cout << "If the value is (example) 1.6e-19"<<endl;
  178.            cout << "It simply means 1.6 x 10^-19"<<endl;
  179.            cout << "---------"<<endl;
  180.            cout << "If you would like to Continue using help, Enter 2"<<endl;
  181.            cout << "To return to the main program enter 1"<<endl;
  182.           cout << "Otherwise, to exit press 3 and then enter."<<endl;
  183.  
  184.    cin >> a;
  185.  
  186.  
  187.  
  188.    } while ( a == 2 );
  189.  
  190. do{
  191.  
  192.   break;
  193.  
  194. } while (a == 3);
  195.  
  196.  
  197.                      break;
  198.           case 'C':
  199.  
  200.           do
  201.            {
  202.  
  203.                      cout<<"--Shape Tool--"<<endl;
  204.                      cout<<"0: Exit"<<endl;
  205.                      cout<<"1: Trapezium"<<endl;
  206.                      cout<<"2: Parallelogram"<<endl;
  207.                      cout << "---------------------------"<<endl;
  208.                      cin>>schoice;
  209.  
  210.                      if (schoice == 1)
  211.                      {
  212.                                double tbase(0);
  213.                                double ttop(0);
  214.                                double vh(0);
  215.            double ta(0);
  216.  
  217.                                do
  218.                                {
  219.                                     cout<<"Enter Base Lenght: ";
  220.                                     cin>>tbase;
  221.                                } while (tbase < 0);
  222.  
  223.            do
  224.                                {
  225.                                     cout<<"Enter Top Lenght: ";
  226.                                     cin>>ttop;
  227.                                } while (ttop < 0);
  228.  
  229.            do
  230.                                {
  231.                                     cout<<"Enter Vertical Hight: "<<endl;
  232.                                     cin>>vh;
  233.                                } while (vh < 0);
  234.  
  235.                                ta = (0.5 * (ttop + tbase)) * vh;
  236.                                cout<<"\n";
  237.                                cout<<"The Area Is: "<<ta<<endl;
  238.            cout << "---------------------------"<<endl;
  239.                                cout<<"To Start Over Enter 1"<<endl;
  240.            cout<<"To Move Onto Parallelograms Enter 2"<<endl;
  241.            cout<<"To Exit Enter 0"<<endl;
  242.            cout << "---------------------------"<<endl;
  243.            cin>>schoice;
  244.                      }
  245.                      else if (schoice == 2)
  246.                      {
  247.                                double pbase(0);
  248.                                double pvh(0);
  249.            double pa(0);
  250.  
  251.                                 do
  252.                                {
  253.                                     cout<<"Enter Base Lenght: ";
  254.                                     cin>>pbase;
  255.                                } while (pbase < 0);
  256.  
  257.            do
  258.                                {
  259.                                     cout<<"Enter Vertical Hight: ";
  260.                                     cin>>pvh;
  261.                                } while (pvh < 0);
  262.  
  263.                                pa = pbase * pvh;
  264.  
  265.                                cout<<"The Area Is: "<<pa<<endl;
  266.            cout << "---------------------------"<<endl;
  267.                                cout<<"To Start Over Enter 2"<<endl;
  268.            cout<<"To Move to Trapeziums Enter 1"<<endl;
  269.            cout<<"To Exit Enter 0"<<endl;
  270.            cout << "---------------------------"<<endl;
  271.            cin>>schoice;
  272.  
  273.                      }
  274.                      else if (schoice == 0)
  275.                      {
  276.                                cout<<"Good bye!"<<endl;
  277.                      }
  278.                      else
  279.                      {
  280.                                cout<<"Invalid input!"<<endl;
  281.                      }
  282.  
  283.            } while (schoice != 0);
  284.  
  285.  
  286.                      break;
  287.  
  288.                      case 'D':
  289.  
  290.  
  291.                                         cout<<"The Current Version is "<<version<<endl;
  292.                                         cout<<"The Last Update Was on the "<<update<<endl;
  293.                                         cout<<"Created In Full By Funkyjunky 2008"<<endl;
  294.  
  295.                                         cout<<"---------------------------"<<endl;
  296.                                         cout<<"Menu"<<endl;
  297.                                         cout<<"---------------------------"<<endl;
  298.                                         cout<<"Enter A for Sphere Tool"<<endl;
  299.                                         cout<<"Enter B for Gradient Tool"<<endl;
  300.                                         cout<<"Enter C for Shape Tool"<<endl;
  301.                                         cout<<"Enter E to Quit"<<endl;
  302.                                         cout<<"---------------------------"<<endl;
  303.                  cin >> ch;
  304.  
  305.  
  306.                      break;
  307.  
  308.                      case 'E': break;
  309.  
  310.  
  311.           default: // Program should handle the case where the user enters an invalid character
  312.           cout << "Sorry, '" << ch << "' does not correspond to a function. Please try again."<<endl;
  313.                       cout<<"Enter A for Sphere Tool"<<endl;
  314.           cout<<"Enter B for Gradient Tool"<<endl;
  315.           cout<<"Enter C for Shape Tool"<<endl;
  316.           cout<<"Enter D for Info About this Program";
  317.           cout<<"Enter E to Quit"<<endl;
  318.                cin >> ch;
  319.           }
  320.  
  321.  
  322.  
  323. return 0;
  324. }
I would now like to talk through some of the things I like about this code.
Code Copy / Restore
  1. cout << "---------------------------"<<endl;
  2.     cout<<"Welcome to Ed George's Math Class Program Version "<<version<<endl;
  3.     cout << "---------------------------"<<endl;
  4.     cout<<"Enter A for Sphere Tool"<<endl;
  5.     cout<<"Enter B for Gradient Tool"<<endl;
  6.     cout<<"Enter C for Shape Tool"<<endl;
  7.      cout<<"Enter D for Info About this Program"<<endl;
  8.      cout<<"Enter E to Quit"<<endl;
  9.      cin >> ch;
  10.      ch = toupper (ch);
I like the main menu because of the "toupper()" command used. This converts a lowercase letter to an Upper case.
I also like the use of strings to hold the version and update information, I found this an effective way of doing this.

This code signals a beginning for me. A new door has just opened. This is what I want to do when I'm older...

Thats why I am going to commit to learning C++, then move onto other programming languages.

My next step, giving this code a GUI.

Thanks for reading.


Posted By funkyjunky
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Cinjection     Posted 218 Days Ago
 
 
Nice. I agree with Ginger. Functions would really increase readability. Also,
you're pretty far away from a GUI. They are surprisingly hard to code and I
would recommend you learn a lot more C++ before you start playing with GUIs. Although
you're in pretty good shape to learn the language.

If you ask me, the best thing you can do right now is get a C++ book and practice.
Do programming challenges like Project Euler (http://projecteuler.net/index.php) and
just practice.
 
Ginger     Posted 220 Days Ago
 
 
Sorry the previous comment, you need to learn to function code.. having so much
inline like that is hard to work with.. its a constructive comment rather than meant
to be harsh
 
Ginger     Posted 220 Days Ago
 
 
Learn to function your code.. all inline like that is impossible to read!
Page 1 of 1
More Articles By This Author
C++ And Me (A Love Story)
Recently Posted "Programming" Articles
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
First look at C
Introduction to Pseudo Code
Integrating your website with PHP
Recently Rated "Programming" Articles
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
if/else, switch/case and ?/: Statements In Actionscript 2.0
Actionscript API
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Programming
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved