tuesday, december 12, 2006 “press any key to continue or any other key to quit.” - anonymous

Post on 19-Dec-2015

217 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Tuesday, December 12, 2006

“Press any key to continue or any other key to quit.”

- Anonymous

What is wrong here?

int myvar.d;

int data-1;

int %value;

What is wrong here?

int a=3;

b=a;

What is wrong here?

int a,b;

a++;

b++;

What is wrong here?

cin>>a+b;

cin>>10;

What is wrong here?

int a=2; b=3;

What is wrong here?

int a=2, b=3;

a+b=5;

What is wrong here?

int a=2, b=3, c=6, d;

d = b–4ac;

What is wrong here?

int a=2, b=3;

cout<<a<<" "<<b<<endl;

double a;

cout<<a<<" "<<b<<endl;

What is wrong here?

char a=h;

Indentation and colors

Initialize with constant or expression

int a, b=3;

a = b*53+7;

int a=70, b=80;

How to print 70 80?

double amount;

cout<<“Enter the balance in dollars:$”;

cin>>amount;

int x=5, y=5;

y=++x;

cout<<x<<" "<<y<<endl;

y=x--;

cout<<x<<" "<<y<<endl;

int x=5, y=5;

y=++x;

cout<<x<<" "<<y<<endl; // prints 6 6

y=x--;

cout<<x<<" "<<y<<endl; // prints 5 6

int x=5, y=5;

cout << x++ << endl;

cout << ++y << endl;

cout << x << ‘,’ << y;

int x=5, y=5;

cout << x++ << endl; //prints 5

cout << ++y << endl; //prints 6

cout << x << ‘,’ << y; //prints 6,6

The following code causes conversions (in this example, integral promotions):

long lnum1=2345, lnum2=3456;int inum=45;

// inum promoted to type long prior to assignment.

lnum1 = inum;

// inum promoted to type long prior to multiplication.

lnum2 = inum * lnum2;

float fVal=3.0, fVal2=4.5;double dVal=9.768;int iVal=6;

fVal2 = iVal * fVal; // iVal converted to float; // result of multiplication is float.

dVal = iVal + dVal; // iVal converted to double;// result of addition is double.

What is wrong here?

double total_price ;

int feet;

cin>>feet;

//There are 5280 feet in a mile

total_price = 5000*(feet/5280);

//suppose user enters 15000 feet

int y;

char c='3';

y = 5 + c;

cout<<y<<endl;

output?

Character Data Each character corresponds to a binary code Most commonly use binary codes are ASCII (American

Standard Code for Information Interchange)Character ASCII Code Integer Equivalent

% 0100101 37

3 0110011 51

A 1000001 65

a 1100001 97

b 1100010 98

c 1100011 99

int y;

char c='3';

y = 5 + c;

cout<<y<<endl;

output 56

Character ASCII Code Integer Equivalent

3 0110011 51

char ck='3';

int i=ck;

cout <<i<<endl;

Casts can be usedcout<<(float)5/3<<endl;

cout<<(float)3/6<<endl;

Casts are considered operators – same precedence as unary operators

char ck='3';

cout <<(int) ck<<endl;

________________________________

char ck='3';

int i=ck;

cout <<i<<endl;

Be careful with casts!

e.g. Don’t cast a double into float

//Compiler will give warnings. Try this out!#include <iostream.h>int main(){

int i, j;

i=23.867;

j=9876543219876;

cout<<i<<" "<<j<<endl;

return 0;}

//Compiler will give warnings Try this out!#include <iostream.h>int main(){

int i, j, k;i=23.867;j=9876543219876;cout<<i<<" "<<j<<endl;

return 0;}

23 -1881560924

cout<<"This is called \"fund raising\"";

cout<<"\"\\";

cout<<"one"<<"\t"<<"two";

Concept of true and falsetrue is any value other than zerofalse is zero

bool b=true;cout<<b<<endl;b=false;cout<<b<<endl;//zero is false and non-zero is trueb=35;cout<<b<<endl;b=-45;cout<<b<<endl;b=0;cout<<b<<endl;

bool b=true;cout<<b<<endl; //prints 1b=false;cout<<b<<endl; //prints 0//zero is false and non-zero is trueb=35;cout<<b<<endl; //prints 1b=-45;cout<<b<<endl; //prints 1b=0;cout<<b<<endl; //prints 0

top related