What on Earth is a "FAQ" anyway?
FAQ stands for Frequently Asked Questions. It is here that you will find answers to questions asked via e-mail. Before e-mailing a question to the TA and proctors, you should check here to see if it has already been asked.
This FAQ will be updated approximately once a week.
Essentially the hours, minutes, and seconds are converted to seconds and stored in the object as seconds. Therefore seconds is a private data member. There will be NO member data "hours, minutes, seconds" for the Time class.
Why do we have to instantiate three objects, time1, time2, and time3.
time1 will hold the 1st time, time2 will the 2nd time, and time3 will hold the elapsed time between time2 and time1.
e.g.
time2 11:30:00 (2nd time)
- time1 09:00:00 (1st time)
--------------------
time3 02:30:00 (holds elapsed time)
Do I cout "08:09:10 and 09:11:13" from the fn Time::prt_time() ?
Yes. In the sample output statement: "The elapsed time from 08:09:10 to 09:11:13 is 1 hour 2 minutes 3 seconds". Time::prt_time() essentially just prints out the times in the format of hr:min:sec from long seconds.
This is how I did it:
In main() "The elapsed time from"
Time::prt_time() "08:09:10"
main() " to "
Time::prt_time() "09:11:13"
main() " is "
Time::elapsed_time() " 1 hour 2 minutes 3 seconds"
Note:
Page 2 of the Programming Assignment (PA) Handout "e)" states Time::prt_time()
prints the formatted hr:min:sec.
Page 3 of the PA Handout "g)" states Time::elapsed_time() prints the
calculated elapsed time in hours, minutes and seconds.Well either:
1) Typecast the double. e.g. double seconds; "(int) seconds % SEC_PER_HR" since modulus operator requires integers 2) Do not use the "%", modulus operator. Just subtract the number of hours (in seconds from the total number of seconds). e.g. sec = seconds; hr = sec / SEC_PER_HR; sec -= hr * SEC_PER_HR; // deduct the number of hours min = sec / ...Should Time::difference(Time, Time) have a t1 and t2 after Time respectively, so I can distinguish the two time parameter of seconds I am subtracting eg.Time::(Time t1, Time t2)
void Time::difference(Time, Time); is the function declaration. Notice that the formal parameter names are *NOT* needed in the function declaration.
The function definition will look something like: void Time::difference(Time t1, Time t2) { ... }
Notice that the formal parameter names are *NEEDED* in the function definition so that they can be used in the function body.
The function call in main() will look something like: diff.differenc(time1, time2);
For part a), should I put the cin in main() or in get_data()?
get_data()
3rd line states get_data() will allow the user to enter 4 characters delimited by spaces.
Yes, that is what I did.
For part d) are we sorting the ASCII values?
Yes. When you test "if ( *p1 > *p2 )" that is testing if ( 'B' > 'A') which is really the ASCII values testing if ( 66 > 65 ).
Altogether I have several ifs and one switch.
a) Try it first with NO error input.
b) Then match the character input 'A" with the j index.
c) Next with lowercase input.
d) Next with not A B C D input
e) Next with 'X' already
Ok there a several small problems here. Let's tackle each separately.
First let's say our array is indexed as b[i][j]:
1) Input is entered as row number and seat character.
a) No problem about row number directly associating to the index i.
Just one little if to test in range 1-7. If true, then set errorflag.
b) The character may be 'a' or 'A'. Well we can deal with either case
now or later. How about now. Just convert the character variable, ch, to
all uppercase. Now we need to associate the character variable, ch, with
the index number, j.
Hmm... 'A' only goes into column 0, 'B' into column 1, 'C' into column 2
And the index, j, is either 0, 1, 2, or 3!
So how about a switch statement?
switch ( ch )
{
case 'A':
j = 0; ... }
Or, you could map the character to a value that corresponds
with the j value as follows:
if ((ch - 'A') == j)
...
So if ch == 'B', then (ch - 'A') will equal to 1, this is
the value of j that allows a 'B' to be valid
2) The 2nd problem is if it's already 'X'ed out. So we only want to
check "if" there is no error input and the seat is 'X'ed out to
print an error message and set the error flag. Else there is no
error input and 'X' that seat out.
3) Then loop this whole thing again while an error exists.
Can you please explain to me where the true, false return to in main. I assume that it goes back to where sold_out() is called.
Yes. This is in the do loop in main().
Remember the value returned is Boolean. Boolean sold_out( char b[][COL]); Therefore "Boolean answer;" will be the declaration. Do not need a break statement.
I ran the program using the sample object code and it prints the error message for the row #. That means it gets error checked first as input. You can try this too.
" cp ../public/assignments/p4 p4.test "
" p4.test "
This last program is my executable code for you to test with all kinds of input and see if if works the same with your program.
How do I set up a tmp string to use after cin.getline(...) in Arithmetic::get_expr()?
In get_expr() you will have the code:
char buf[SIZE]; // String to convert into arith. expr char tmp[SIZE]; // Tmp string to convert into each operand and operator cin.getline(buf, SIZE, '\n'); // Read entire input line
Do I set up the tmp string to do all the error testing?
You test "buf" string and if it is a real number, then assign into "tmp" string.
Do I just "tmp[] = num", using the private data member like the example you gave?
Since "tmp" is a character array, must convert into a floating point number by using "atof()" and assigning that converted value into private data "num".
In Arithmetic::set(void), am I suppose to assign the input to memory?
Yes.
Is that ok for this assignment to use strcpy(), and those others from <string.h> ?
Yup, use those string manipulating functions. This is the lab to use everything in that "Character Strings" handout.
No, This will not work. (See Q7)
Yes. Looks good. Remember that operator is a single character so it will not need a loop. Code in these steps: First just a nondigit. Test for whole numbers. Second just a real numbers. Test for real numbers. Third a negative real numbers. Test for negative real numbers.
What exactly in Arithmetic::divisor (void) is being returned?
Just returns the private operand, num.
In get_expr (void) definition, how do I return an "op" value to main?
"return op; "
(Note Q9). get_expr() returns a character. That character will be the "op", operator IF it is a correct input. IF it is *NOT* correct input the character returned is '?'. Note on page 13, in the box for the last case label. This will match the character value returned from get_expr().
Notice on page 13 in the box of sample code. See how the case label in the switch statement determines which member function to call. For the '/' operator returned from get_expr() that the calc.div() function is called.
Yes. Therefore only ONE "cout << "ERROR! Only real numbers for operand" is in main() in the case '?':
No. Therefore get_expr() returns a character to main(). That one character may be "op" (the operand) OR '?'. This '?' is returned from get_expr() on error input.
How do I clean up my get_expr() so it isn't a bunch of " != " to get the operand from buf into tmp?
A14: Rethink.
1st character: You are only testing a single character for a negative number.
Also may be a digit or period. Therefore test for ONLY '-' digit '.'
If true then assign that single character to "tmp[0] = buf[i++];"
If false " return '?'; "
2nd, 3rd, ... characters: Copy only the digits and periods from "buf" to "tmp",
If digits or periods assign that single character from buf to tmp
ELSE " return '?'; ".
Upon seeing a space in "buf", then "tmp" is finished. "tmp" is assigned
the operand (-1.23). Append a '\0'. Now, convert "tmp" to a number, atof()
Skip white space to now get the opeerator.
What happens to buf[i] when there is nothing in the buf[i], not even a space?
After you get the operand into tmp, then all you have left is an operator. Assign that single character to "op". "return op; " You don't even have to test what kind of operator it is because in main() the switch statement tests what is returned from get_expr(). Forget the rest in buf.
In section e) can I just use the *this to do the same job without using tmp?
Yes, need to "return *this;" AND need tmp to hold new bigger string.
In Strg operator+(), do I need to use a tmp also?
Yes, a tmp is important to use here.
Do *NOT* edit p7.h (except update "const int SIZE = 78;"). Therefore *ONLY* type your code into p7.C
Yes, put pre and post conditions *ONLY* in p7.C just above the function header.
Now for strcat. First you do a strcpy right? Then add (append/concatenate) to the end of the 1st string.
e.g. s1 = Santa
s2 = Claus s3 = s1 + s2;
The strcpy on page 25 looks like:
for ( i = 0, p = rs.str ; i < len ; ++i )
str[i] = p[i];
str[i] = '\0';
// STRCAT part
So when strcpy finishes, "i" should be holding the value of "len" the
length of Santa ( s1[5] ) and str[i] is now positioned to hold the '\0'.
So this is where you want to start the appending. You really do NOT need
to search for the NULL, you have it!
Your strategy is right. It seems that your FALSE is getting returned FIRST all the time and the TRUE never is looked at AFTER. SOOO... the hint is WHEN are you testing and returning TRUE?