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 feet
int y; char c='3'; y = 5 + c; cout<<y<<endl; output?
Character Data l Each character corresponds to a binary code l Most commonly use binary codes are ASCII (American Standard Code for Information Interchange) CharacterASCII CodeInteger Equivalent % A a b c
int y; char c='3'; y = 5 + c; cout<<y<<endl; output 56 CharacterASCII CodeInteger Equivalent
char ck='3'; int i=ck; cout <<i<<endl;
§Casts can be used cout<<(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 int main(){ int i, j; i=23.867; j= ; cout<<i<<" "<<j<<endl; return 0; }
//Compiler will give warnings Try this out! #include int main(){ int i, j, k; i=23.867; j= ; cout<<i<<" "<<j<<endl; return 0; }
cout<<"This is called \"fund raising\""; cout<<"\"\\"; cout<<"one"<<"\t"<<"two";
§Concept of true and false §true is any value other than zero §false is zero
bool b=true; cout<<b<<endl; b=false; cout<<b<<endl; //zero is false and non-zero is true b=35; cout<<b<<endl; b=-45; cout<<b<<endl; b=0; cout<<b<<endl;
bool b=true; cout<<b<<endl; //prints 1 b=false; cout<<b<<endl; //prints 0 //zero is false and non-zero is true b=35; cout<<b<<endl; //prints 1 b=-45; cout<<b<<endl; //prints 1 b=0; cout<<b<<endl; //prints 0