cs31 lecture 17

F at the end of the number to instruct the compiler

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int foo = 3; foo = foo / 4; // foo has value 0 x Problem arises also for values Problem Example: float foo = 3 / 4; // foo has value 0 21 Integer division x To avoid the problem with values Add .f at the end of the number, to instruct the compiler to treat it Add as a float as • Example: float foo = 3.f / 4.f Add .0 at the end of the number to make it decimal • Example: float foo = 3.0 / 4.0 22 Common mistakes x Forgetting to declare a variable Forgetting The compiler tells you the error x Read the value of a variable before setting it Read The compiler *may* tell you the error IIn this case, the value of the variable at its first use is n undetermined undetermined x Use a type too small to hold the data The compiler usually doesn’t tell anything x Make a mistake with integer division The compiler doesn’t tell you anything 23 How to interact with a program x Key feature of computers: ability to interact with the user x Typical uses: Display a result on the screen Input a value from the keyboard x Not always needed: some programs operate on data that is Not already on the computer already x Extremely useful to debug & understand the behavior of a Extremely program program 24 Console input and output x In C++, predefined classes to interact with the default In screen, and the default keyboard screen, x Default scre...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online