I keep getting an undeclared identifier error.

Pages: 12
Btw guys, the reason I have militaryToMinStrt(int ) & militaryToMinEnd(int ) is because inside each of those is a different equation.. One is converting start and one is converting end as I stated above, if I am confusing anyone or myself, I am sorry.
They both convert from military time to standard time, the equation is the same.
I guess in a sense it is the same formula, just different variables.

That is exactly what I am trying to say. You only need to pass the different variables as arguments. Imagine you had a function to convert temperature from Degrees to Celsius and you wanted to do the conversion in the morning and in the evening. Would you do this
1
2
3
4
5
6
7
8
9
float degToCelMorn(float mornTemp)
{
	// do conversion
}

float degToCelEve(float eveTemp)
{
	// do conversion
}

or this
1
2
3
4
float degToCel(float Temp)
{
	// do conversion
}

Then just call it like
1
2
morning_temp = degToCel(mornTemp);
evening_temp = degToCel(eveTemp); 
Last edited on
closed account (48T7M4Gy)
One is converting start and one is converting end as I stated above, if I am confusing anyone or myself, I am sorry.


@MJGilbert
You only need one function here, not two separate ones.

The function is like a coffee grinder. Send it the beans (the start in military hours or end time in military hours) and it comes back (returns) ground up coffee (the start time in minutes or end time in minutes)

The grinder will always do the same and it couldn't care less what time (beans) it gets sent to it.

You don't (normally) have a breakfast grinder and another one doing exactly the same for dinner and another one for lunch ...

Provided you have beans and want them ground you only need one grinder.

If you're confused by this try a cup of tea. (You'll need a different set of functions for that. )
inside each of those is a different equation.. One is converting start and one is converting end

Different equation, you say. Set your "start time" equal to "end time". Call these two functions. Compare the results. If the computation is different, then the results should be different. Are they?
Which variable are we talking about?

sorry it was just confusing reading code when you are sleepy.

i think youre getting such error because of the local variable ,which will be discarded after the main func, from main func to protoype func.

are you using VS? if not change return EXIT_SUCCESS into return 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

using namespace std;

//define prototypes
void instruct();
int militaryToMin(int );
int elapsedTime(int , int );

//universal constant
const int MINUTES_PER_HOUR = 60;

int main()
{
    //define variables
    int strtTime;
    int endTime;
    
    //prompt user to input start and end time
    instruct();
    cin >> strtTime >> endTime;
    
    //output start and end time
    cout << "Start Time: " << strtTime << endl;
    cout << "End Time: " << endTime << endl;
    
    //pass variables through functions
    militaryToMin(strtTime);
    militaryToMin(endTime);
    
    //output elapsed time
    cout << "Elapsed Time: " << elapsedTime(endTime, strtTime) << " minutes"<< endl;
    
    return 0;
}

void instruct()
{
    cout << "Please enter a start and end time for the same day in military time: ";
    
}

//Pre: input time is defined
//Post: sum is returned
int militaryToMin(int time)
{
    int sum;

    sum = ((time / 100) * MINUTES_PER_HOUR) + (time % 100);
    
    return sum;
}

//Pre: input end and start are both defined
//Post: the difference is returned
int elapsedTime(int end, int start)
{
    return (militaryToMin(end)) - (militaryToMin(start));
}


This is my code now. My biggest problem was I was not realizing exactly what everyone meant by "passing something through a function"... I have learned a ton from this topic alone and I am very appreciative.

admkrk, I see exactly what you are saying now!
closed account (48T7M4Gy)
Looking good but lines 28 and 29 are a bit odd. Don't you want to print out the the start and end times in minutes? If you don't then you can delete these two lines. If you do then you have to either:

cout << militaryToMin(strtTime);

or

int srtTimeInMinutes = militaryToMin(strtTime);
cout srtTimeInMinutes;

In other words, at the moment, you aren't using the value of sum which is the returned value from the militaryToMin function. The beans went off, they were ground up and then ... nothing.

I just noticed your comment on line 27. Make sure you realise that lines 28 and 29 only pass the times through to the function you call on those lines ie militaryToMin() only.
Last edited on
closed account (48T7M4Gy)
Also, can I suggest you use 'startTime' instead of 'strtTime'. Good code is readable.
admkrk, I see exactly what you are saying now!

At least the holes in the wall from me banging my head against it did not go to waste. :) It looks a lot better now except for
1
2
3
4
5
void instruct()
{
    cout << "Please enter a start and end time for the same day in military time: ";
    
}

Assuming this even works, there is no reason to create a function to do one thing one time.
1
2
3
4
5
6
7
8
//prompt user to input start and end time
instruct();
cin >> strtTime >> endTime;

// would be better just as

cout << "Please enter a start and end time for the same day in military time: ";
cin >> strtTime >> endTime;

Do not get caught up in the "main should just call functions" mentality, that is just wrong for so many reasons.

<edit>

This is another example
1
2
3
4
5
6
7
8
9
10
//Pre: input end and start are both defined
//Post: the difference is returned
int elapsedTime(int end, int start)
{
    return (militaryToMin(end)) - (militaryToMin(start));
}

// Could just be eliminated  by 

cout << (militaryToMin(end)) - (militaryToMin(start));

Last edited on
Assuming this even works, there is no reason to create a function to do one thing one time.
1
2
3
4
5
6
7
8
//prompt user to input start and end time
instruct();
cin >> strtTime >> endTime;

// would be better just as

cout << "Please enter a start and end time for the same day in military time: ";
cin >> strtTime >> endTime;

Do not get caught up in the "main should just call functions" mentality, that is just wrong for so many reasons.

<edit>

This is another example
1
2
3
4
5
6
7
8
9
10
//Pre: input end and start are both defined
//Post: the difference is returned
int elapsedTime(int end, int start)
{
return (militaryToMin(end)) - (militaryToMin(start));
}

// Could just be eliminated by

cout << (militaryToMin(end)) - (militaryToMin(start));


I know exactly what you're saying, but the professor wanted to practice using a void function and she wanted a function for each execution in this program.. I've seen plenty of stuff like you're talking about though..

Thanks a million for all the help!
closed account (48T7M4Gy)
I thought that was what your prof might be doing MJ. It's all good practice.

The other aspect is that where a huge number of functions are required on a project with a large (or even small) team the functions can be shared around separately without everybody being required to work on just the one file.

The manager can concentrate on the main() and shunt the rest around. So there is no such thing as the 'mains ...' mentality. That's a red herring. There is just good software engineering and project management. Ask you prof.
Topic archived. No new replies allowed.
Pages: 12