Click here to Skip to main content
14,700,108 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <iomanip>
#include "C1A7E1_MyTime.h"
using namespace std;

MyTime *DetermineElapsedTime(const MyTime *tp1, const MyTime *tp2);
int main()
{
   for (int loop_count = 0; loop_count < 3; loop_count++)
    {
    	
        cout << "Enter time point one, space separated:\n";
        cin >> tm1.hours >> tm1.minutes >> tm1.seconds;
        cout << "Enter time point one, space separated:\n";
        cin >> tm2.hours >> tm2.minutes >> tm2.seconds;
        cout << "entering function!";
        DetermineElapsedTime(const tm1, const tm2)
				 }
    const MyTime tm1 = { 2, 0, 0};
    const MyTime tm2 = { 0, 0, 0};
    MyTime time_dif = *DetermineElapsedTime(&tm1, &tm2);
    cout << "The time elapsed from ";
    cout << setfill('0') << setw(2) << tm1.hours << ":";
    cout << setfill('0') << setw(2) << tm1.minutes << ":";
    cout << setfill('0') << setw(2) << tm1.seconds;
    cout << " to ";
    cout << setfill('0') << setw(2) << tm2.hours << ":";
    cout << setfill('0') << setw(2) << tm2.minutes << ":";
    cout << setfill('0') << setw(2) << tm2.seconds;
    cout << " is ";
    cout << time_dif.hours << ":" << time_dif.minutes << ":" << time_dif.seconds << "\n";
}


C1A7E1_MyTime.h


#ifndef Class_Test_C1A7E1_MyTime_h
#define Class_Test_C1A7E1_MyTime_h
using namespace std;
struct MyTime { int hours, minutes, seconds; }; /* do not change this */

#endif

C1A7E1_DetermineElapsedTime.cpp


#include <iostream>
#include "C1A7E1_MyTime.h"
using namespace std;
MyTime *DetermineElapsedTime(const MyTime *tp1, const MyTime *tp2)
{
    static MyTime return_time;
    MyTime testy1, testy2;
    testy1 = *tp1;
    testy2 = *tp2;
    long time_point_dif;
    time_point_dif = (long(testy1.hours) * 3600 + long(testy1.minutes) * 60 + long(testy1.seconds)) - \
    (long(testy2.hours) * 3600 + long(testy2.minutes) * 60 + long(testy2.seconds));
    return_time.hours = int(time_point_dif / 3600);
    time_point_dif -= return_time.hours * 3600;
    return_time.minutes = int(time_point_dif / 60);
    time_point_dif -= return_time.minutes * 60;
    return_time.seconds = int(time_point_dif);
    return(&return_time);
  
}


What I have tried:

I tried to create data type int tm1, tm2? Not sure why I am getting an error not in scope.
Posted
Updated 19-Aug-16 8:18am
Comments
Richard MacCutchan 19-Aug-16 11:23am
   
You have declared tm1 and tm2 after the for loop so that won't work. You have also declared them const, so you are not allowed to change them.
Member 12622319 19-Aug-16 13:20pm
   
I have tried to put the variables before the for loop this does not work.
Please try it yourself to see the error message I am receiving?
Richard MacCutchan 20-Aug-16 3:43am
   
I have done, and I have told you how to correct it. Put the variables before the for loop, and remove the const keyword. And in future if you see an error message please post it in your message here, rather than telling other people to do it.
Member 12622319 19-Aug-16 11:25am
   
I have tried to put the variables before the for loop this does not work.
Please try it yourself to see the error message I am receiving?
Philippe Mori 19-Aug-16 15:08pm
   
Read compiler error message and fix the problem.

const MyTime tm1 = { 2, 0, 0};
const MyTime tm2 = { 0, 0, 0};

are declared after the for loop block. Move to before the for loop and try again.
   
Comments
Member 12622319 19-Aug-16 11:15am
   
I have tried this and it still does not work. Can you try it also?
This is not the solution?
jeron1 19-Aug-16 11:22am
   
How about moving them and take off the const declaration. Because you are writing user values into these variables, therefore they shouldn't be const. Maybe post the exact compiler errors you are seeing.
Member 12622319 19-Aug-16 11:27am
   
I have tried putting them before the for loop and changing the variables to int type still does not work. please try it yourself to see the error message?
Philippe Mori 19-Aug-16 15:04pm
   
It is not to us to try to co mpile your code. You need to give us the error message you get.
By the way, this answer is correct but you have to fix remaining errors. If you don't tell us the errors, it is harder to guess even though it is easy to see the line DetermineElapsedTime(const tm1, const tm2) which does not make sense. It is neither a valid declaration nor a valid instruction and it seems useless...
Quote:
I tried to create data type int tm1, tm2? Not sure why I am getting an error not in scope.
The compiler is unhappy because you use tm1 and tm2 before creating them.
By principle, you can't use something before declaring it.

[Update]
Quote:
Yes but what do I do about it I have tried to declare the variable before hand and it still does not work?
Don't declare them as const or don't write into them.
   
v2
Comments
Member 12622319 19-Aug-16 13:19pm
   
Yes but what do I do about it I have tried to declare the variable before hand and it still does not work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900