CSCI 15A Jim Murphy The C++ compiler breaks your source code into tokens according to the rules of syntax of the language These smallest program units include: Special symbols of one or two characters such as // + { += <= etc. (Tokens are separated by space or carriage returns or special symbols or comments) Keywords such as int, return, struct, if, while etc. see page 583 for a complete list. int try = 1; Identifiers such as main, customer, cin etc. names of classes, objects, and functions Start with a letter then any number of letters, numbers and underbars _ Number of characters distinguished by the compiler depends on the compiler (some use 32 characters) float CostOfThisItemToYouWhenYouComeByOnMonday; float CostOfThisItemToYouWhenYouComeByOnTuesday; float s, x, t, b1, CostOfItem, Cost_of_Item; /* Constants such as -7, 23, 2.56, 'C', "Hello" etc. “A+” “int” “float” Comments either all after // on a given line or everything between /* and */ possibly several lines */ Objects supported include float, int, and char each object must be declared (optionally initialized) class-name object-identifier-list; int x, y, z; float cost, tax, interest; char grade; class-name object-identifier = expression; int sum = 0, x, y, z=0; char MidInitial = 'L'; float rate = 0.0825; Assignment to already declared objects with object-identifier = expression; // in compiler terms lvalue = rvalue; evaluate the expression on the right and assign the result into the memory location of the object named on the left can be used to alter the state of objects: sum = sum + cost; float tax = 5; // int 5 is converted to float 5.0 int count = 2.73; // count becomes 2 const object-initialization // will keep you from accidently changing a value later in the program const float pi = 3.1415926; If you omit the class then int is assumed and for const interest = 0.0875; interest will be taken to be an integer and the type conversion will make interest zero Arithmetic expressions include: numeric constants: 7, -2, 3.78, 2.74e-3 - expression expression op expression where op is +,-,*,/,% (expression) such as -(3 + 5) LESSON 3 higher precedence for: * / % evaluate left to right then - + evaluate left to right 6 - 2 + 4 / 2 is 4 + 2 not 8 / 2 7 / 2 = 3 is an integer division 7 % 2 = 1 is the remainder after integer division mixing int and float the int is promoted to float 7 / 2.0 = 3.5 any of these examples could have been given using object names such as: int x = 7; x / 2 would then be 3 Read all the exercises - try the ones that are not obvious - in class look at 4 and 5 pages 47 and 48 (15)Second assignment due June 5, 1996. Start from your personalized template file and write a C++ program on unix to solve lab project 2E on page 52. Then use ftp on the PC to transfer the source code to the 486 machine in OCNL 133 and compile there or to a PC or MAC at home. In Windows put the source file in a directory of by itself and within BC++ create a new project with the same name - use EasyWin as the target. Hand in a listing of your program, a print of the window containing your run on unix and Windows. In Windows put the source file in a directory by itself and within BC++ create a new project with the same name - use EasyWin as the target. email a listing of your program, a paste of the window containing your run on unix and Windows. Programming Errors can be: Compiletime, Linktime, Runtime, or of Intent Compiletime errors are detected by the compiler when it has trouble with your syntax (language rules) int student Number; cin >> student; missing comma between student and Number the real error may be that studentNumber is intended to be all one word - then we drop space and also cin >> studentNumber; Notice if we had: int student Number; cin >> studentNumber; both lines would generate errors since the space in the first line causes studentNumber to be an undeclared object. Some errors appearing later in the code will make no sense and will disappear when the earlier errors are fixed - also note that you will likely get quite different errors from different compilers Linktime errors are found by the linker when it attempts to put several modules together and can not find certain functions or objects that are referred to such as "Undefined symbol _main" if you are missing: int main ( ) in your program - perhaps because you have: int Main ( ) or some other unacceptable variation Runtime errors can occur if you have something like: int denom; cin >> denom; cout << 8 / denom; if denom should be entered as zero this will cause a runtime error Later we will see how to check for this and avoid it Errors of intent occur because computers do what we tell them to do not what we want them to do These errors are the most difficult to discover but testing known cases can help Try introducing known errors into the programs you have worked with so far and try compiling the faulted programs with various compilers to see what errors you get if any. Notice many changes to programs will not cause errors to be generated. In other words there are many statements which while legal in the language are not what you want. Typically only one statement is really correct. This is not to be confused with the fact that there are many approaches to solving any problem but that in the context in which the statement is found most of this flexibility is lost Find the classnotes in the directory: /usr/local/gx/mercer/classnotes or on the web site