# Lab46 Wiki

### Site Tools

notes:notes:projects:cpp

# Differences

This shows you the differences between two versions of the page.

 — notes:notes:projects:cpp [2010/03/22 13:58] (current) Line 1: Line 1: + ~~DISCUSSION|C++ Demonstration Page Discussion~~ + + ~~TOC~~ + ​ + // + // + + ​CSCS 2850: Projects​ + ​C++ Programming​ + A Practical Demonstration + ​ + + This is the documentation page for the CSCS 2850 C++ Demonstration Code.  These demonstrations explore the some of the finer points of C++ programming while using practical, workable examples to illustrate the language. ​ Specifically,​ this demonstration will follow the evolution a simple process driven program into a modular and then object oriented program using C++. + ​ + + For the Reader to consider: The programs in this demonstration begin near the culmination of the first month of a college C++ coarse, and lead to more advanced techniques that are addressed at the end of said coarse, or not at all.  So, consider that, if you're reading this article as one of the first C++ resources you've picked up, you're about 1 month behind. ​ However there are several resources on the web and at your + local bookstore that will catch you up in no time at all.  [[http://​lab46.corning-cc.edu/​documentation/​linkgarden#​cc|The Lab46 Link Garden]] is a good place to start. + ​ + ​ + + =====The Problem Statement ===== + This is a practical demonstration of the C++ and, by extension, C programming language. ​ The following tutorials are intended to demonstrate practical programming methods that can help you solve real-world problems. ​ As such, our first problem statement follows: + + Write a menu driven program that will allow a user to convert between the following values: + * Degrees Fahrenheit to degrees Celcius + * Degrees Celcius to degrees Fahrenheit + * Inches to centimeters + * Centimeters to inches + * Pounds to Kilograms + * Kilograms to pounds + + ====The Logic==== + In keeping up with the nature of academic exploration,​ this demonstration will begin with a short description of the logic needed to solve this problem, as it can be helpful to solve the problem in plain English, before writing any C++ code: + + ===Step 1=== + * Show the user all the possible options, request that they make a choice, and wait. + + This demonstration is a command line program, and in the interest of keeping it simple, we'll just write a quick number based menu ... 1 for this, 2 for that ... so on. + + In psuedocode, it looks something like this: + <​code>​ + print " 1. Fahrenheit to Celsius"​ + print " 2. Celsius to Fahrenheit"​ + print " 3. Inches to centimeter"​ + print " 4. Centimeters to inches"​ + print " 5. Pounds to kilograms"​ + print " 6. Kilograms to pounds"​ + print " 0. Exit" + ​ + + ===Step 2=== + *Get the number the user entered. + This line is pretty straight forward in psuedocode: + <​code>​ + num option + print "​Please choose an option"​ + get option + ​ + The ''​num option''​ part is simply declaring a variable named ''​option''​ that will hold a value that is a number. ​ Even though numbers are represented differently given each different type (int, float, double, long...), in psuedocode, it is sufficient to simply say number...as in something that can be counted. + + ===Step 3=== + *Test the number the user entered against the options that we presented. ​ So, ''​if''​ the user enters "​1"​ (without the quotes), ''​then''​ run the Fahrenheit to Celsius conversion ... if 2, then Celsius to Fahrenheit and so on. + + In psuedocode, it looks like this: + + <​code>​ + if (option == 1) then + + //fahr to cels + + else if (option == 2) then + + //cels to fahr + + else if (option == 3) then + + // inches to centimeters + + else if (option == 4) + + //​centimeters to inches + + else if (option == 5) then + + // pounds to kilos + + else if (option == 6) then + + // kilos to pounds + + end if + ​ + + The benefit of this method is that there'​s really very little need to error check the user's input. ​ If it doesn'​t match one of the conditions ​ (for example, if the user enters the letter '​A'​),​ nothing happens, and the program exits. ​ So, even though the menu states to press 0 for exit, anything other than 1 thru 6 will cause the program to exit. + + ===Step 4=== + *Depending on the user's choice, perform the calculation. + + Each of our conversions has different logic. + + **Fahrenheit to Celsius ... and back again:** + + We know that 100 degrees Celsius is equal to 212 degrees Fahrenheit and 0 degrees Celsius is equal to 32 degrees Fahrenheit, ​ therefore the ratio of Fahrenheit to Celsius is <​latex>​\frac{212-32}{100-0}​. ​ That means that if Celsius is ''​c''​ then Fahrenheit equals <​latex>​\frac{9}{5}*c+32,​ and if Fahrenheit is ''​f''​ then Celsius equals <​latex>​\frac{5}{9}*(f-32)​. ​ However, since we're after the modifier 1.81 and not 1 we need to use <​latex>​9.0/​5.0​. + + So, the final conversion is <​latex>​\frac{9.0}{5.0}*c+32​ to go from Celsius to Fahrenheit and <​latex>​\frac{5.0}{9.0}*(f-32)​ to convert Fahrenheit to Celsius. + It looks like this in psuedocode: + ​ + <​code>​ + //fahr to Celsius + num f; //temp in Fahrenheit + num c; //temp in Celsius + + print "Enter the temperature in Fahrenheit: " + get f + + c=(5.0/​9.0)*(f-32) + + print f, " degrees Fahrenheit is ", c, " degrees Celsius."​ + ​ + <​code>​ + //cels to fahr + + num f + num c + + print "Enter a temperature in Celsius: " + get c; + + f=(9.0/​5.0)*c+32;​ + + print c, " degrees Celsius is ", f, " degrees Fahrenheit."​ + ​ + + **Inches to centimeters and the other way around:** + + This one's a little easier. ​ There are 2.54 centimeters in one inch, so if given inches, divide by 2.54 to get centimeters. ​ If given centimeters,​ multiply by 2.54 to get inches. + + Here it is, in psuedocode: + ​ + + <​code>​ + // inches to centimeters + const num IN_CONV = 2.54 + num inches + + print "​Please enter a length in inches:"​ + get inches + + num cent = inches*IN_CONV + + print inches, " inches ​ is ", cent, " centimeters"​ + ​ + <​code>​ + //​centimeters to inches + + const num IN_CONV = 2.54 + num cent(0); + print "​Please enter a length in centimeters:"​ + + get cent; + + num inches = cent/​IN_CONV + + print cent, " centimeters ​ is ", inches, " Inch(es)";​ + ​ + + The psuedocode above introduces the concepts of constants; variables that maintain a constant value and cannot be changed within the body of code. + ​ + \\ + **Pounds to kilos and the inverse** + + + There are 2.2 kilograms in one pound, so again, if given pounds, simply divide by 2.2 to get kilograms, and likewise, if given kilograms, multiply by 2.2 to get pounds. + + The psuedocode snippet: + ​ + <​code>​ + // pounds to kilos + + num p //pounds + num k //kilos + + print " Please enter a weight in pounds: " + get p + + k = p/2.2; + + print p, " pounds is equal to ", k, " kilograms.";​ + ​ + <​code>​ + // kilos to pounds + + num p //pounds + num k //kilos + + print " Please enter a weight in kilograms: "; + get k; + + p = k*2.2; + + print k, " kilograms is equal to ", p, " pounds.";​ + ​ + + ===Step 5=== + * Loop back to the beginning and allow the user one more shot at it. + + There are several looping structures that could be used to solve this common task.  However, in this case the ''​do...while''​ is a good choice, as it is a post test looping structure, and its body will be executed at least once.  This means that we'll only have to code our menu once (rather than repeating it before and during the loop ), and we can set the condition to test inside the body of the loop. + + Like this: + + <​code>​ + int input=0 + boolean condition=false + do + + print "​Please enter 1 to continue or 0 to exit" + get input + + if (input == 1) + condition=true + + while(condition) + ​ + + The above loop will execute at least once, prompting the user for an input. ​ Upon testing the input, the condition is set, and immediately tested in the bottom of the do...while loop.  Our ''​do...while''​ loop will differ slightly, as we will be testing a compound condition: ''​while (input>0 && input<​7)''​ + + The final psuedocode solution looks like this: + + <​code>​ + start + num option; + + do{ + print " 1. Fahrenheit to Celcius"​ + print " 2. Celcius to Fahrenheit"​ + print " 3. Inches to centimeter"​ + print " 4. Centimeters to inches"​ + print " 5. Pounds to kilograms"​ + print " 6. Kilograms to pounds"​ + print " 0. Exit" + + print << " Choose an option: " + get option; + + if (option == 1) then + + //fahr to celc + + num f; //temp in Fahrenheit + num c; //temp in Celcius + + print "Enter the temerature in Fahrenheit: " + get f + + c=(5.0/​9.0)*(f-32) + + print f, " degrees Fahrenheit is ", c, " degrees Celcius."​ + + else if (option == 2) then + + //celc to fahr + + num f + num c + + print "Enter a temperature in Celcius: " + get c; + + f=(9.0/​5.0)*c+32;​ + + print c, " degrees celcius is ", f, " degrees fahrenheit."​ + + else if (option == 3) then + + // inches to centimeters + const num IN_CONV = 2.54 + num inches + + print "​Please enter a length in inches:"​ + get inches + + num cent = inches*IN_CONV + + print inches, " inches ​ is ", cent, " centimeters"​ + + else if (option == 4) + //​centimeters to inches + + const num IN_CONV = 2.54 + num cent(0); + print "​Please enter a length in centimeters:"​ + + get cent; + + num inches = cent/​IN_CONV + + print cent, " centimeters ​ is ", inches, " Inch(es)";​ + + else if (option == 5) then + // pounds to kilos + + num p //pounds + num k //kilos + + print " Please enter a weight in pounds: " + get p + + k = p/2.2; + + print p, " pounds is equal to ", k, " kilograms.";​ + + else if (option == 6) then + // kilos to pounds + + num p //pounds + num k //kilos + + print " Please enter a weight in kilograms: "; + get k; + + p = k*2.2; + + print k, " kilograms is equal to ", p, " pounds.";​ + ​  ​      } + + }while (option>​0 && option<​7);​ + + } + ​ + + =====Version 1: if...else===== + Solving the problem statement above in C++ is very similar to solving it in psuedocode. ​ The only notable difference is syntax.  ​ + + At this point, you can head over to the [[notes:​projects:​cpp_demo | C++ Demonstration Code page]] and either download the code, or create a new file and copy and paste the code into that file.  Name the file converter.cc and compile it using the instructions below ... or follow along with this tutorial as we write the the program in C++. + + ====Comments==== + The first lines in our demonstration program are comments. ​ C++ compilers (and some C compilers) recognize a couple types of comments. ​ For single line comments, simply use the escaped slash sequence and escaped asterisk sequence as follows: + + <​code>​ + /* + + everything between "/​*"​ and "​*/"​ is a comment + + */ + + //this is a single line comment + + a=x+b //end of line comment + + ​ + + ====Preprocessor Directives==== + The lines beginning with a hash (#) are not actually program statements. ​ These lines are called preprocessor directives as they are directives that send instructions to the preprocessor. ​ There are several directives that can be used, and some take arguments. ​ One of the most commonly used directives in C++ is the ''#​include''​ directive. ​ ''#​include''​ is essentially telling the preprocessor to include the contents of the source file (passed as and argument) as if it actually appeared in the program at that point. ​ As follows: + + + + #include <​istream>​ + #include <​ostream>​ + #include <​iostream>​ + #include <​iomanip>​ + + ​ + + \\ + \\ + * Namespaces + * int main() + * Variables and datatypes + * Standard Input/​Standard Output + * Looping and Conditional Sturctures + * .cc file extension + * Tips & Tricks + + + ====Compiling the code.==== + Once you get the code to your shell, simply compile it using your favorite C++ compiler. ​ For example, if you're using g++ you would issue the following command from the directory where you downloaded the files. + + + :~$g++ -o converter converter.cc + ​ + + Once the code is compiled, you can run the program using the command ./​converter. + + + :~$ ./converter + ​ + + =====Version 2: Modular===== + Compiling the code + * Directory Structures + * Header Files + * Functions + * Switch statements + * Tips & Tricks + + =====Version 3: Classes===== + Compiling the code + * Custom data types + * Classes + * Return Values + * Arguments + * Tips & Tricks + =====Version 4: Make===== + * Using Make + * Tips & Tricks + + =====Version 5: GUI with C++===== + *GUI With C++ + *Necessary libraries + *portability??​ + *Tips and Tricks + + =====C++ Demonstration Code===== + The C Demonstration Code is located [[notes:​projects:​cpp_demo|here]]. The context of the code will be explained through other demonstrations. The code page is only meant to be a repository for gathering the code in its original form. + + Download the code using the links found at the download page.  While you can copy and paste the code directly from the wiki text, this is method is discouraged as typos in each edit may not have passed through a compiler. ​ Each major revision is compiled and tested before it is made available for download. ​ The moral of the story is, use the links. + + If you're downloading the code to a Windows machine, you'll want to open it in using something like wordpad.  ​ + + *[[notes:​projects:​cpp_demo|C Programming Demonstration Code]] 