CSC15 Lab 2 Due Wednesday 2/14/2001 Please read the ENTIRE LAB before proceeding. In this lab you will be writing your first C++ program from scratch. Well, almost. First type in the following template, which represents the basic structure of very simple C++ programs. #include int main() { /* Your code goes here. */ return 0; // leave this line alone } Be sure to save the file with a ".C" suffix such as "lab2.C". The "main" function, which is part of every C++ program, is the "entry point" of the program. Larger programs will have its code partitioned into components that are outside of main. However, since the first program we're going to write is very simple, the entire program will be inside main. ---- We are going to write a little program that adds up minutes and seconds. For example, if the user inputs 3 minutes and 40 seconds, followed by 2 minutes and 30 seconds, then the program needs to figure out that the sum of the two times represents 6 minutes and 10 seconds. ******* Make sure that the math required is clear to you before proceeding to program. Write out the code you need for the calculation first on paper. This write up is to be turned in as part of the assignment. ******* It's not that I don't think you can't do simple math, but it is very important to always be clear about the algorithm in an abstract sense before coding. To do otherwise will be akin to getting into a car without knowing where you want to go. ----- Note, if two integer values are divided using / operator, then the result will automatically throw away the remainder. For example, with the lines: int x; x = 9; cout << (x / 2); we should see 4 printed. To take the remainder after division, we use the mod, or % operator. Continuing the above example, cout << (x % 2); should print 1, the remainder of 9 divided by 2. ---- So, we know how to do the math and to code the math as C++ statemnets. Now let's think about what kind of variables we'll need to represent our data. We'll need the following variables to represent the input data: minute1, minute2 second1, second2 That is, these variables will be used to hold the values of the two pairs of minutes and seconds we're adding. We also need variables to hold the value of the result of our computation: sumMinutes, sumSeconds. You may need additional variables to hold intermediate values. For these, you need to COMMENT what they're used for in clear english. For example: int minute1; // minute value of first time int minute2; // minute value of second time ... int A; // value to hold total number of seconds in first time. int B; // value to hold total number of seconds in second time. int C; // temporary value to hold remainder of A divided by 60. ... Comments are a very important part of the program. It's worth 15% of your grade on this assignment. You need to comment each variable you use - that is, make it clear what it's used for. --- Here is a possible transcript of your program: Enter minute1: 3 Enter second1: 40 Enter minute2: 4 Enter second2: 50 The sum is 8 minutes and 30 seconds. --- ***** turn in printout of program + writeup on paper. ***** Part 2: For the second part of the assignment you are to write the same program but use an already-prepared graphical user interface instead of cin and cout. The graphical window generated will have 6 text fields, representing the minutes and seconds of the values. They are as follows: field_m1 : minute value of first time (input) field_s1 : second value of first time (input) field_m2 : minute value of second time (input) field_s2 : second value of second time (input) field_rm : minute value of total time (output) field_rs : second value of total time (output) In programming terminalogy each of these fields is an "object". To read an integer value from a field, use (for example) minute1 = field_m1->getIntVal(); whereas in the first program you said cin >> minute1; To output an integer value to a field, do field_m1->setIntVal(5); // if 5 is the value you want to output whereas in the first program you said cout << 5; Your program should have the following skeleton: #include "timegui.C" void mywindow::addtime() { // your code goes here } SPECIAL COMPILATION INSTRUCTIONS: 1. do a "source useqt" in the directory where useqt was downloaded. 2. download the file "timegui.C" from the class homepage. This is code I wrote for the graphical interface. You're welcome to look through it. 3. execute the command "moc timegui.C -o timegui.moc" (this extra command is particular to this assignment only) 4. compile your program with "qtcompile timecalc" (assuming timecalc.C is what you saved it under). Same rules on commenting applies.