Chapter 2: The Basics of C++ Programming



Intro. to C Programming

Programming - The process

• Understand the problem and requirements

• Design or select an algorithm to solve it

• Express (code) the algorithm in a programming language (e.g., C, Java)

• Test and debug the program until it works and meets the requirements

Hats I will wear this semester

|You |Me |

|[pic] |[pic] |

Programming - The mechanics

[pic]

Learning the setup of a program

• the setup is the same for many of your programs

• the setup must be in the SAME order as shown as below

|Setup Example |

|[pic] | |

| |Place name here |

| |Place what the program is going to be done |

| | |

| | |

| | |

| |Place all imports here |

| | |

| | |

| |Create main here |

| | |

| | |

| | |

| | |

| |( **Where most of your code goes!!** |

|Description of Setup |

|Includes |Some commands and features require other “packages or libraries” |

|Main |where most of your code will be placed. Starts from top to bottom. |

Types of files in a program

(or sections of a program- it depends on your style)

• .c file (source file):

o contains your functions (programming procedures).

o file you will primarily be writing in, because functions are the bulk of C coding.

• .h file (header):

o always accompanies a .c file.

o It DOES NOT WORK by itself. It must have a .c file to “Drive” it.

o It contains:

▪ declarations of functions created in the .c file

▪ variables used throughout the program

▪ special self-created classes, which are explained later in the year

▪ list of reserved command words that you use

▪ a “library” of functions, variables, and other functions that you can use in your .c file

Data types

• size of variables do depend on the # bit processor

| |Examples/Definitions |

|Type | |

|int |int yards = 202; (4 bytes) |

| |a WHOLE number, ranging from -2147483647 to 2147483647 |

|unsigned int |unsigned int feet = 6; |

|(uint32_t) |a WHOLE NON-NEGATIVE number ranging from 0 to 2147483647 |

|short |short coordinate = -12; (2 bytes) |

| |a WHOLE number, ranging from -32767 to 32767 |

|unsigned short |unsigned short attitude = 32,000; |

|(uint16_t) |a WHOLE NON-NEGATIVE number ranging from 0 to 32767 |

|long |long debit = 10000000000; |

| |a WHOLE number, ranging from -2^63 to 2^63-1 |

|unsigned long |unsigned long population = 5000000000; |

| |a WHOLE NON-NEGATIVE number ranging from 0 to 2^63-1 |

|unsigned long long |(8 bytes) |

|(uint64_t) |A whole number ranging from 0-18446744073709551615 |

|float |float GPA = 3.99; |

| |a real number in a floating point representation |

|double |double mole = 1.2336483; |

| |a double-precision real number in a floating point representation |

|long double |long double weight = 7.7493343658792749; |

| |a extended-precision real number in a floating point representation |

|char |char choice = 'x'; (1 byte) |

|(uint8_t) |ONE character, or a small INTEGER in the range from -128 to 127 |

|boolean |bool found = true; |

| |a Boolean value can either AND ONLY be true(1) or false(0) |

Input and Output

I/O stream

scanf printf

Using puts/printf/scanf

• to display to the screen

o puts( simple text output

o printf( if you have variables to print

• to read from the keyboard

o scanf(

Printing a Line of Text

|C Version |

|// Mr. Lupoli |

|// 1st program |

|#include |

|int main() |

|{ |

|puts(“Welcome to C!\n”); // what is the difference between this line |

|puts(“Welcome ”); // and this line?? |

|puts(“to Mr. Lupoli’s Class”); |

|return 0; |

|} |

|Draw a sizeable square on your paper. If it was a monitor what would like after the code above completed. |

Use the code above to display YOUR name on line ONE, and your town and state on line TWO

Using printf( ) with variables

|C Versions using I/O |

|// declare your variables first!!! |

|int x = 10; |

|float scores = 12.34; |

|double y = 23.3; |

|char z = ‘A’; |

|short int a = 12; |

|long int b = 12857612554; |

|long double c = 2834892734892.23; |

|char string[20] = “Lupoli”; // string AHEAD OF GAME HERE!! |

|printf |scanf |

|printf(“x is = %d \n”, x); // int |scanf(“%d”, &x); // int |

|printf(“score is = %f \n”, y); // float |scanf(“%f”, &score); // float |

|printf(“y is = %lf \n”, y); // double |scanf(“%lf”, &y); // double |

|printf(“z is = %c \n”, z); // char |scanf(“%c”, &z); // char |

|printf(“a is = %hd \n”, a); // short int |scanf(“%hd”, &a); // short int |

|printf(“b is = %Ld \n”, b); // long int |scanf(“%Ld”, &b); // long int |

|printf(“c is = %Lf \n”, c); // long double |scanf(“%Lf”, &c); // long double |

|printf(“String is = %s \n”, string); // string |scanf(“%s” , string); // string |

|printf(“X is: %d and Y is: %f\n”, x, y); \\ 2 displ |scanf(“%d %f”, &x, &y); // 2 done |

Notice, no space behind %?

literal escape constants/command constants

|JAVA Escape Sequences |

|Escape Sequence |Description |

|\t |tab |

|\r |carriage return, go to beg. of next line |

|\\ |backslash |

|\” |double quote |

|\’ |single quote |

|\n |new line |

|\b |back space |

|\f |form feed |

What will these statements below display??

puts(“Hi Class!\n”);

puts (“Good Luck!!! \n”);

puts (“\t \t You’ll need it!!!\n”);

|First Scanner Example |

|// Mr. Lupoli |

|// First C Example |

| |

|#include |

| |

|int main() |

|{ |

|int age = -1; // set a DEFAULT value |

| |

|puts("How old are you?"); |

|scanf("%d", &age); // grab value from keyboard (user) |

| |

|int dogAge = age * 7; |

| |

|printf("You are %d years old in DOG YEARS", dogAge); |

|return 0; |

|} |

| |

|Identify where the include statement is located |

|Identify where the scanf command is located |

|Identify type of data (float, String, int) is being read in |

|Identify where the output is taking place |

|Inputting a Number |How old are you? |

| |12 |

| |You are 84 years old in DOG YEARS |

|Inputting a Decimal |How old are you? |

|(error!!) |5.75 |

| |You are 35 years old in DOG YEARS |

|Inputting a String |How old are you? |

|(error!!) |Emily |

| |You are -7 years old in DOG YEARS |

Why did entering a decimal (float) or String mess up the calculation??

|Input/Output Exercise |

| |

|// First Program - Learning the Setup |

| |

|include |

| |

| |

|int main() |

|{ |

| |

|char name[20], address[40]; |

| |

|// 1. ) Create the CODE to LITERALLY display YOUR name, and address (No variables yet.) |

| |

| |

| |

|// 2.) Create the CODE to ask for user’s name and address, USE THE VARIABLES DECLARED FOR // YOU ALREADY!! Hint: Which scanner functions will you need? |

| |

| |

| |

|// 3.) Create the code to display their name and address that THEY type in. NOT yours!! |

| |

| |

|return 0; |

|} |

|! |printf(“Lupoli \n”); |! |

| |printf(“1600 Pennsylvania Ave. …\n”); | |

| | | |

| |printf(“Please enter your name\n”); | |

| |scanf(“%s”, &name); | |

| | | |

| |printf(“Please enter your address\n”); | |

| |scanf(“%s”, &address); | |

| | | |

| |printf(“Your name is: %s”, name); | |

| |printf(“Your address is: %s”, address); | |

Use of Comments

|// |/* … code … */ |

|// Mr. Lupoli |/* |

|// Project 1 |Mr. Lupoli |

|// 6/22/03 |Project 1 |

|// Period 1 |6/23/03 |

| |Period 1 |

|// ( “//”reserves REST of line for a // comment |*/ |

| | |

| |/* |

|// used for ONE line comments | |

| |“ /* ” reserves whole block until you end it with a “ */ “ |

|void main() | |

|{ |used for MULTIPLE lined comments |

|int counterValue;// sentinel value |*/ |

Why use comments?

• For notes

o to yourself AND to me!!

• For commenting out unfinished lines of code

o skipping unfinished functions

• To understand what the code is doing!!

Reserved Words

• case sensitive

• can not be used as variable or function names

• ex

printf

scanf

auto break case char

const continue default do

double else enum extern

float for if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

Creation of the MAIN()

|int |void |

|// opening |// opening |

| | |

|int main( ) // no void inside ( )’s |void main( ) // no void inside ( )’s |

|{ // brace to open code block |{ // brace to open code block |

|… |… |

|… |… |

| | |

|// closing |// closing |

|return 0; | |

|} // brace to close code block |} // brace to close code block |

• The difference between the two is slight for now, later will be crucial

• void

o program returns NOTHING when completed (void … get it!!)

• int

o notice “return 0” at end – MUST HAVE

o returns an actual ‘0’ when program is completed

Style (like me!!! Stylin’)

• nested blocks

o used for

▪ compound statements

▪ iteration (repeating loops of code)

▪ conditional (if (x < 10)…)

|Style and a COMPLETE example w/ Scanf |

|void main( ) |

|{ |

|// variables |

|char user; |

| |

|puts( “Will I do my work in Mr. Lupoli’s Class??\n” ); |

| |

|// have user press “Y” or “N” |

|scanf(“%c” , &user;) // reads what user typed |

| |

|if(user == ‘Y’) // will pass |

|{ puts( “Then I will pass C, and take C++ next year!\n” ); } |

|// ONE LINE |

| |

|if(user == ‘N’) // will fail |

|{ |

|puts( “Then I will not pass Mr. Lupoli’s class.\n” ); |

|puts( “And my parents will be upset!\n” ); |

|// TWO LINES OR MORE |

|} |

|} |

FYI - Section

Include Statements

• become a big problem for professors when they started using namespace

• namespace

o package of library files that Visual Studio uses

o are different than normal library files used in Borland or even VS 6.0

|Differences in using Namespace |

|using namespace |not using namespace |

|#include // for cin/cout |#include // for cin/cout |

|#include // for strings variables |#include // for strings variables |

| | |

|using namespace std; // MUST HAVE | |

| | |

|// rest of code below |// rest of code below |

• Use chart below (also on web as Namespace Chart)

|Common Library Files Used |

|using namespace |not using namespace |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |ONLY USE NAMESPACE!!! |

|#include |#include |

|#include |#include |

|Non-Commonly Used Library Files |

|using namespace |not using namespace |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

|#include |#include |

cls/clrscr()

• current text window and places the cursor in the upper left-hand corner (position 1,1).

o place in first line of printing the menu options

|VISUAL C/C++ (Microsoft) |BORLAND |TURBO C |

|#include |#include |#include |

|system(“cls” ); |clrscr( ); |clrscr( ); |

|// are CALLED in the main( ) |// are CALLED in the main( ) | |

Getchars

• a pause for a menu

• #include // has getchar()

• getchar( )

o is a pause, the user must hit a button to continue the program

displayVector(x); // some function

getchar( ); // our pause

WARNING!! – make sure that there are several lines separating your getchar( ) command from other lines of code (sometimes interferes with code surrounding it)

|Getchar and clrscr example |

|#include |

|#include |

| |

|void main() |

|{ |

|puts("Mr. L is da bomb"); |

|getchar(); // pause |

|clrscr(); |

|} |

-----------------------

keyboard

( stream (

Computer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download