Review 1 A compiler translates code written in a language into language Chapter 3 CS1A Review P2 2 T F Syntax is the rules that dictate the meaning attached to instructions in a programming language 3 A is the name of a location in memory that has a data value that may be changed 4 Values for these identifiers are obtained at time and the amount of memory to be reserved is determined at Input Output 5 A is the name of a location in memory that has a data value that may not be changed 6 Values for these identifiers are obtained at time and the amount of memory to be reserved is determined at 1 of 56 7 The documentation next to the declarations for variables and named constants is called the Input Ouput to the screen cin variable for input places the value into a memory location 8 It tells the reader and their values are obtained 9 What is an unsigned int 10 What does the data type tell the compiler 2 things cout what you want to output for output prompts the user for data could also be a variable or expression They usually work in pairs 11 What are the differences between how a constant is declared between a variable 12 Explain the difference between the following declarations char charVal char strVal 10 2 of 56 No not a C statement Program Basic Structure 3 of 56 Pre processor Directive s information the program needs a list of all necessary header files used in the program Heading int main functions by definition return a value include iostream include iomanip using namespace std Don t put void Write it just like this Remember this ends a int main program statement float floatVal int intVal the above heading indicates that this function will an int These define a code block You must start with and end with main function named constant declarations variable declarations executable statements return 0 Topic 1 CS1A Review Input Output For Example 4 of 56 preprocessor directives for I O Tells the compiler to use predefined Standard C functions variables classes All C programs must start with this It tells the compiler where to start Declares a float variable Declares an integer variable cout Enter a floating point number prompts for an input cin floatVal Reads the input into floatVal cout Enter an integer cin intVal cout fixed setprecision 2 prompts for the 2nd input Reads the input into intVal These are output manipulators that Formats floating point values more on this later cout n nThe floating point value is setw 8 floatVal endl cout The integer value is setw 6 intVal endl return 0 Tells the OS that the program terminated properly 5 of 56 ran successfully Note the indent 1 Main Breaking it down int main body of function i e program statements return 0 include iostream include iomanip Pre processor Directives Tell the pre processor that we want to use i o functions so we include them in our code We need iostream to use cin cout statements We need iomanip to use output manipulators which dictate how our output will be formatted All executable statements are here using namespace std The book has an error MAIN CANNOT BE VOID Tells the compiler that we want to use all the standard C functions variables and classes Functions are small code segments that we use to build our program Must start with and end with Must have return 0 as last statement This returns the value 0 to the system so it knows the program completed properly cout Enter a floating point number cin floatVal End of C statement Variable Name These are variable declarations Remember we must reserve memory locations to store data The compiler needs to know the cout and cin are how we communicate with the user cout Inserts data to the output stream Uses the insertion operator Can output a string literal using or variables cout fixed setprecision 2 Define how the floats should output cout n nThe floating point value is setw 8 floatVal endl Insertion operator Type of data or datatype and how much memory to allocate 8 of 56 Topic 1 CS1A Review Input Output 7 of 56 Cin Cout Declaring Identifiers Datatype or Type 6 of 56 Topic 1 CS1A Review Input Output float floatVal int intVal Program execution begins with this function All C programs must have this function MUST BE an int String Literal n inserts a Carriage return Must be in Variable Name Function that Sets the width endl inserts a Carriage return without Note We can keep output much as we want in one line 9 ofin 56 Topic 1 CS1Amake Review it Inputso Output don t long you can t read it when typing it CIN cin floatVal Extraction operator MUST BE A VARIABLE End of C statement cin extracts information from the input buffer using the extraction operator In this example the contents of the input buffer are put into the variable floatVal YOU CAN ONLY HAVE VARIABLES on the RIGHT OF A CIN STATEMENT because the data must be stored somewhere Input in C cin Extraction operator cin getline cin get cin ignore cin width Note Generally speaking we want to pair cin with a cout so that the user knows the program is waiting for an input 10 of 56 Topic 1 CS1A Review Input Output 2 Basic Input in C The Extraction operator We want to be able to extract data into a variable This allows us to execute our programs with values that are not predefined cin is a predefined variable in c that allows us extract input directly from the user There are many ways to extract input using the cin variable Anytime an input command is executed the program will wait for an input to be read in Syntax cin variable variable We use the extraction operator to read in numerical data Only variables to the right of the extraction operator So far We have discussed the extraction operator This ideal for reading in numbers but not so effective for reading in strings characters or whitespace Remember the purpose is to store data in a memory location Variables are the only memory locations that can be modified at runtime You can use more than one variable in one statement Input should MATCH the data type prompt appropriately Input is extracted from the buffer unless the buffer is empty then it reads in from the 13 keyboard 12 of 56 Topic 1 CS1A Review Input Output Extraction Operator of 56 Example2 Extraction Operator Ignores leading whitespace Reads data until it reaches white space Everything else goes into the input buffer cout Enter a floating point number cin floatVal cout Enter an integer cin intVal EXAMPLE cout Enter a floating point number cin floatVal OUTPUT Enter a floating point number 32 5 n floatVal 32 5 Input
View Full Document
Unlocking...