C++ Help

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Im trying to write a program that will take a start time in HH:MM:SS and then a flight time in seconds, and then calculate the finish time in HH:MM:SS, It worked when I input the hours minutes and seconds seperately, but I want to be able to input them in the HH:MM:SS format, Im a new programmer, and I know im using the CString Class wrong, so any help is appreciated.

#include "stdafx.h"
#include <iostream.h>
void main()
{
int starthr, startmn, startsc, finishhr, finishmn, finishsc, finish, start1, start2, start3;
long flttime;
char start, strstart;
cout << &quot;Enter the birds start time in HH:MM:SS\n&quot;;
cin << start;
cout << &quot;\nBirds total flight time (seconds)? &quot;;
cin >> flttime;
CString strstart = start;
start1 = strstart.Left(2);
start2 = strstart.Mid(3, 2);
start3 = strstart.Right(2);
finish = (start1 * 3600) + (start2 * 60) + start3 + flttime;
finishmn = finish % 3600;
finishhr = (finish - finishmn) / 3600;
finishsc = finishmn % 60;
finishmn = (finishmn - finishsc) / 60;
cout << &quot;\nThe birds finishing flight time is &quot; << finishhr << &quot;:&quot; << finishmn << &quot;:&quot; << finishsc << &quot; (HH:MM:SS)\n&quot;;
}
 

br0wn

Senior member
Jun 22, 2000
572
0
0
I have never used CString before.
Do you have to use it ? what's wrong with your program above ?
what is that stdafx.h that you are including from your
current directory ?
One thing to check is that, are you sure Left, Mid, Right
return int (start1, start2, and start3 are defined as int) ?
(I don't know since I've never used CString)

If you don't need to use CString, this problem is simple enough,
just parse manually the start variable, to get hh, mm, ss.


 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
this is a fixed version of your program. I think that if you leaning towards MFC programming in VC++ then using CStrings is the correct way to go. The wrapper is fairly thin and you can always get the char* type using the LPCSTR operator. On top of that you don't have to worry about the memory managment, which is nice when you start out...anyhow here's the correted version ofyour code....



#include &quot;stdafx.h&quot;
#include <iostream.h>

void main(){
int starthr, startmn, startsc, finishhr, finishmn, finishsc, finish, start1, start2, start3;
long flttime;
char start[9]; // needs to be a char[] type to hold the full string
cout << &quot;Enter the birds start time in HH:MM:SS\n&quot;;
cin >> start;
cout << &quot;\nBirds total flight time (seconds)? &quot;;
cin >> flttime;
CString strstart(start); // construct the CString object
start1 = atoi((LPCSTR)strstart.Left(2)); // LPCSTR operator returs the char* object, atoi( ) converts it to integer
start2 = atoi((LPCSTR)strstart.Mid(3, 2)); // ...
start3 = atoi((LPCSTR)strstart.Right(2)); // ...
finish = (start1 * 3600) + (start2 * 60) + start3 + flttime;
finishmn = finish % 3600;
finishhr = (finish - finishmn) / 3600;
finishsc = finishmn % 60;
finishmn = (finishmn - finishsc) / 60;
cout << &quot;\nThe birds finishing flight time is &quot; << finishhr << &quot;:&quot; << finishmn << &quot;:&quot; << finishsc << &quot; (HH:MM:SS)\n&quot;;
}


thats it, i tried to keep the code as true to the original as possible, therefore I didn't modify the heart of the program, but here's a Csolution which does not rely on string input and checks the bounds of the incoming streams...


#include &quot;stdafx.h&quot;
#include <iostream.h>
#include <stdio.h>

#define HRS_BOUNDS(bound) (bound >= 0 &amp;&amp; bound < 24 ? true : false)
#define MIN_BOUNDS(bound) (bound >= 0 &amp;&amp; bound < 60 ? true : false)
#define SEC_BOUNDS(bound) (bound >= 0 &amp;&amp; bound < 60 ? true : false)

void main()
{
int start_timeHr, start_timeMin, start_timeSec=-1;
int flight_timeHr, flight_timeMin, flight_timeSec=-1;
while(!(HRS_BOUNDS(start_timeHr) &amp;&amp; MIN_BOUNDS(start_timeMin) &amp;&amp; SEC_BOUNDS(start_timeSec))) {
start_timeSec = -1;
printf(&quot;Enter the birds start time in HH:MM:SS\n&quot;);
scanf(&quot;%d:%d:%d&quot;, &amp;start_timeHr, &amp;start_timeMin, &amp;start_timeSec);
}

while(!(MIN_BOUNDS(flight_timeMin) &amp;&amp; SEC_BOUNDS(flight_timeSec))) {
flight_timeSec = -1;
printf(&quot;Enter the birds flight time in HH:MM:SS\n&quot;);
scanf(&quot;%d:%d:%d&quot;, &amp;flight_timeHr, &amp;flight_timeMin, &amp;flight_timeSec);
}

int total_time = start_timeHr*3600 + start_timeMin*60 + start_timeSec + flight_timeHr*3600 + flight_timeMin*60 + flight_timeSec;

int total_timeHr = total_time/3600;
int total_timeMin = (total_time - total_timeHr*3600)/60;
int total_timeSec = (total_time - total_timeHr*3600 - total_timeMin*60);
}
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Few questions
1. How does the #define command work?
2. How does the variable(bounds) (test) work?
3. What does the scanf command do?
4. How is the printf different from cout?
5. What does the stdio.h header allow you to do?

Thanks, your obviously a hell of alot more advanced programmer then me, how long have you been programming?
 

br0wn

Senior member
Jun 22, 2000
572
0
0
Let me help you to answer some of the questions :

>> 1. How does the #define command work?

a. #define works like declaration or substitution of names.
Everything that you define using #define in the program will get
substituted into the value of #define.

e.g :
#define MAX_BOUND 10

Thus, if you have MAX_BOUND anywhere in your program, it will
get substituted into 10.

b. #define also works for function define.
from HigherGround's example :
#define HRS_BOUNDS(bound) (bound >= 0 &amp;&amp; bound < 24 ? true : false)

This means that everywhere in program, where you call HRS_BOUND(test),
it will substituted into the (test >= 0 &amp;&amp; test < 24 ? true : false).

Very powerful tool eh ? #define is a common tool for
C programmer.

>> 2. How does the variable(bounds) (test) work?
See above explanation.

>> 3. What does the scanf command do?
This is C function for reading input (same as cin for C++)

>> 4. How is the printf different from cout?
This is C function for printing output (same as cout for C++).

>> 5. What does the stdio.h header allow you to do?
This is the include file for C for doing input/output
(same as iostream.h for C++).

You get confused because HigherGround wrote a C program
as he has noted for the second solution.

>> how long have you been programming ?
I let HigherGround to answer this question by himself.

edit : hey, what is that stdafx.h ? Anyone care to tell me ?

 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Not sure what stdafx.h is, visual c++ put it there :), so why would you use a define command over lets say a normal variable declaration and definition?
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
1. See above

2. the statement (condition ? true : false) can be represented as

if(condition) return true;
else return false;

3, 4, 5. See above

6. not long enough :)

7. stdafx.h is used in VC++ for precompiled header info (VC++ uses *.pch to store the header info and stdafx.obj to store its types). Usually you want to put there definition files that are used quite often, but are changed very infrequently (like base class files that are rock solid).
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Are there any good c++ dedicated forums? Or whats a good resource to help me, (ive already got two books, the vc++ 6 dummies book, and the vc++ 6 bible).