Sie sind auf Seite 1von 12

* * *

Problem Solving in Programming Simple C++ program (Chp2) Data Types

Problem solving in programming


Case Study: Kilometers Converting Miles to

Problem Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion.

Problem solving in programming


Analysis The first step in solving this problem is to determine what you are asked to do. You must convert from one system of measurement to another, but are you supposed to convert from kilometers to miles, or vice versa? The problem states that you prefer to deal in metric measurements, so you must convert distance measurements in miles to kilometers.

Problem solving in programming


Design The next step is to formulate the algorithm that solves the problem. Begin by listing the three major steps, or sub problems, of the algorithm. Implementation To implement the solution, you must write the algorithm as a C++ program. Testing How do you know the sample run is correct?

Simple C++ Program Source Code


#include <iostream> int main() { cout << Hello, World! << endl; return 0; }

C++ Basics Preprocessor Directive


Preprocessor directives start with #: #include <iostream> These instructions tell the preprocessor to also look in the file iostream for source code before compiling The contents of iostream are combined with the current file, as if you copied the contents directly into your source code iostream is one of the standard packages included in C++ Provides input and output support, including reading from the keyboard and writing to the screen

C++ Basics Source Code


#include <iostream>
The main function is where our programs start

int main() { cout << Hello, World! << endl; return 0; }

C++ Basics main() Function


A function does a specific task and includes instructions (a module, or procedure) Functions are identified with parentheses after the function name int main() The function called main is special, as it identifies where the program should start Every C and C++ program requires a main function

C++ Basics Code Block


Blocks of code are grouped together between curly brackets: { somecodegoeshere } These brackets can identify the start and end of each function Everything between the curly brackets can be considered one big statement, or a compound statement

C++ Basics Instructions


Each instruction or statement is ended with a semicolon cout << Hello, World! << endl; This statement writes the string Hello, World! (without the quotes) to the screen followed by moving the cursor to the next line The symbol << is an operator that takes the item on the right side and inserts it into the item on the left side cout is a C++ object that represents the standard output (normally the screen) endl is a C++ string constant for end of line

C++ Basics More about the simple program


In our function, int main(), int is a keyword that describes the output type of the function This function will produce an integer output The return statement defines the output and terminates a function return 0; Sets the function output to the number 0 and stops the function

C++ Basics Source Code


#include <iostream> int main() { cout << Hello, World! << endl; return 0; Terminate the main function and set its }

output to the number 0

C++ Basics Comments


Readability is extremely important Comments help explain your code Use comments wisely Explain the overall intent of what you are trying to do Dont repeat the code in your comments, Write your comments as if you are explaining yourself to another person Comment as you write it is surprisingly easy to forget to do this later Comments can be written between /* and */ Comments can also be written after // on a single line

Data Types

Reserved Words and Identifiers (variables)


Reserved words have special meanings
Can NOT be used for other purposes (const, float and void are some examples)

Identifiers (Variables)
Used to store data by the program (user defined) Valid identifiers - letter, scale1, scale_1 Invalid identifiers 1member, const, memb er

Reserved Words and Identifiers


Special symbols
C++ has rules for special symbols = * ; { } ( ) // << >>

Upper and Lower Case


C++ case sensitive
Compiler differentiates upper & lower case Identifiers (Variable) can be either Be careful though (cost Cost)

Data Types and Declarations


In C++ each variable has a data type

Predefined data types


short less bits 16 or 32 bits more bits

int

(integers)

int long

Positive or negative whole numbers 1000 -12 -199 100000

int mynum;

Data Types and Declarations


Predefined data types
float
Store capacity of decimal digits

9 15 19

float

(real numbers)

double long double

See precision error program

Positive or negative decimal numbers 10.5 -1.2 100.02 99.88

Data Types and Declarations


Predefined data types
bool
true false

(boolean)

char

(Characters)

Represent characters A a 2

Declarations
Examples
char response; int element; float score; float temperature; int i; int n; char c;

Constant Declarations
Types of constants
integer float char bool

Associate meaningful terms


const float PAYRATE = 10.25;

General Form for cin and cout


cin >> dataVariable; cin >> class >> f_name >> l_name; cout << my class is: << class << and my first name is: << f_name ;

Dont mix types with cin int x; cin >> x;

Keyboard input 16.6 Value placed in x would be 16

Das könnte Ihnen auch gefallen