I keep getting an undeclared identifier error.

Pages: 12
Hey guys, I am trying to write a program for class and I keep getting an undeclared identifier error...
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
  #include <iostream>

using namespace std;

//define prototypes
int militaryToMinStrt(int miliStrt);
int militaryToMinEnd(int miliEnd);
int elapsedTime(int minStart, int minEnd);

int main()
{
    int strtTime;
    int endTime;
    int minStart;
    int minEnd;

    cout << "Please enter a start and end time for the same day in military time: ";
    cin >> strtTime >> endTime;
    cout << "Your start time in minutes is: " << militaryToMinStrt(minStart);
    cout << "Your end time in minutes is: " << militaryToMinEnd(minEnd);

    return EXIT_SUCCESS;
}

int militaryToMinStrt(int minStart)
{
    const int minPerHour = 60;
    
    ((strtTime / 100) * minPerHour) + (strtTime % 100); /*undeclared (strtTime)*/
    
    return minStart;
}

int militaryToMinEnd(int minEnd)
{
    const int minPerHour = 60;
    
    ((endTime / 100) * minPerHour) + (endTime % 100); //undeclared (endTime)
    
    return minEnd;
}
Think about scope.
1
2
3
4
5
6
7
8
9
int militaryToMinStrt(int minStart)
{
    const int minPerHour = 60;
    
    ((strtTime / 100) * minPerHour) + (strtTime % 100); /*undeclared (strtTime)*/
                                                        // where did you declare it?
    return minStart;                                                        
                                                                                     
}
How does the function know strtTime?If you can answer the question you can find why you get the error.
That's the thing, I thought it was being declared in the main function... Same with endTime
The function can be called from many places. How could it track all of them?

A function receives data via its parameters.

Even if your functions would see variables of their caller, the lines 29 and 38 do nothing. You don't store the result of expression.
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

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

int main()
{
    int strtTime;
    int endTime;

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

    cout << "Your start time in minutes is: " << militaryToMin(strtTime) << endl;
    cout << "Your end time in minutes is: " << militaryToMin(endTime) << endl;
    cout << "Your elapsed time in minutes is: " << elapsedTime( strtTime, endTime ) << endl;

    return 0;
}

int militaryToMin(int time)
{
    return ((time / 100) * 60) + (time % 100); /*undeclared (strtTime)*/
}

int elapsedTime(int start, int finish)
{
    return militaryToMin(finish) - militaryToMin(start);
}
The "militaryToMinStrt" function is same as "militaryToMinEnd".kemort gave a solution.Of course,admkrk talked about "scope" is the reason why the complier told you there was a undeclared identifier error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

//global variable,in this file every function can access this variable,and modify it.But it's not a good solution.
int strtTime;
int endTime; 
.......
int main(int argc, char **args)
{
    ......
    return 0;
}

.......
MJGilbert, objects declared in the main function have their scope limited to within the main function.

This counts for strtTime, endTime, minStart and minEnd.

Either make the variables global as AlexWang8212 has shown, or pass the variables as needed as parameters to the appropriate functions (I recommend this, as using global variables unnecessarily is generally a bad practise http://stackoverflow.com/a/485020 ).
Excellent... Thanks guys. Good tidbit of info knowing objects declared in the main function are limited to the main function. And thank you to everyone else for your show of solutions. I will work off of those..

Thanks again! :)
Scope is not limited to just the main function. Consider the example bellow.
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
#include <iostream>

using namespace std;

// Function to add 5 to a number
int addFive(int num1);
// Function to add 10 to number
int addTen(int num1);

int main()
{
    int num1 = 0;

    cout << "Enter a number: ";
    cin >> num1;

    // Print from 0 to given number
    for(int i = 0; i < num1; i++)
	cout << i << '\n';

    // Since the loop stops one number short of given number, try to add 1 again
    // i is now out of scope and will give error C2065: 'i' : undeclared identifier
    //cout << i + 1 << '\n';

    // Print given numner
    cout << num1 << '\n';
    // Print given number + 5
    cout << addFive(num1) << '\n';
    // Print given number + 10
    cout << addTen(num1) << '\n';
    // Print given number again
    cout << "num1 in main = " << num1 << '\n';

    return 0;
}

int addFive(int num1)
{
	num1 += 5;
	cout << "num1 in addFive function = " << num1 << '\n';
	return num1;
}

int addTen(int num1)
{
	num1 += 10;
	cout << "num1 in addTen function = " << num1 << '\n';
	return num1;
}


Now if you wrote the first part like this printing i + 1 works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int num1 = 0;
    int i = 0;

    cout << "Enter a number: ";
    cin >> num1;

    // Print from 0 to given number
    for(i = 0; i < num1; i++)
	cout << i << '\n';

    // Since the loop stops one number short of given number, try to add 1 again
    // i is not out of scope 
    cout << i + 1 << '\n';

    return 0;
}


Awesome, so I'm learning a bunch of goodies here... So, my main take from all this and from you admkrk is that since you called your num1 in your prototype, it is declared. But because I did not call or set up my prototype right, I am getting my error..

I will revise and repost :)

Thanks a ton!
Here is my revision based on all of your help, guys.

I am still getting a couple errors now, I will show in the code.

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
#include <iostream>

using namespace std;

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

int main()
{
    int strtTime;
    int endTime;

    cout << "Please enter a start and end time for the same day in military time: ";
    cin >> strtTime >> endTime;
    
    cout << "Your start time in minutes is: " << militaryToMin(strtTime) << endl;
    cout << "Your end time in minutes is: " << militaryToMin(endTime) << endl;
    cout << "Your elapsed time in minutes is: " << elapsedTime(strtTime, endTime) << endl;

    return EXIT_SUCCESS;
}

int militaryToMin(int strtTime)
{
    const int minPerHour = 60;
    
    strtTime = ((strtTime / 100) * minPerHour) + (strtTime % 100);
    
    return strtTime;
}

int militaryToMin(int endTime) //Redefinition of 'militaryToMin'
{
    const int minPerHour = 60;
    
    endTime = ((endTime / 100) * minPerHour) + (endTime % 100);
    
    return endTime;
}

int elapsedTime(int strtTime, int endTime)
{
    return militaryToMin(int endTime) - militaryToMin(int strtTime); //Expected '(' for function-style cast or type construction
}


I see what you guys are saying, but something is still keeping me from understanding 100%... I will keep looking at it and see if I can figure something out. In the meantime, more advice would be wonderful!

P.S. by me setting strtTime = to my equation in the function, I lose the undeclared error... Same with endTime
Last edited on
In my example I had 3 variables with the same name, num1, to show that they were actually different. Generally that is not the best way to go since it can get confusing. To keep things clearer it would be better to keep your functions as they were and just call them differently.
1
2
3
4
5
cout << "Your start time in minutes is: " << militaryToMinStrt(strtTime);
cout << "Your end time in minutes is: " << militaryToMinStrt(endTime);
// Instead of
cout << "Your start time in minutes is: " << militaryToMinStrt(minStart);
cout << "Your end time in minutes is: " << militaryToMinEnd(minEnd);


Basically what it boils down to is minStart being the variable that has its scope inside the function and when you call it with strtTime as the argument if sets minStart = strTime. So you only needed a couple small changes to fix your original problem; using the right argument in the call and using the right variable in the function.
1
2
3
4
5
6
7
8
int militaryToMinStrt(int minStart)
{
    const int minPerHour = 60;
    
    minStart = ((minStart / 100) * minPerHour) + (minStart % 100); 
    
    return minStart;
}


You are also not going to get the answer from your expression that you expect because of integer division.
1
2
3
4
minStart = ((minStart / 100) * minPerHour) + (minStart % 100);
// If minStart < 100 then (minStart / 100) = 0
// If minStart >= 100 && minStart < 200 then (minStart / 100) = 1
// ,,, 
Last edited on
The functions are declared outside of the main function don't you see.The functions are declared passed the closing bracket there fore those functions cannot "see" that variable.
Hey guys, first off I want to say thank you for all the help. I spoke to my professor after revising my code using the info you guys gave me.

Here is my code now:
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
#include <iostream>

using namespace std;

//define prototypes
int militaryToMinStrt(int);
int militaryToMinEnd(int);
int elapsedTime(int , int );

//universal constant
const int MINUTES_PER_HOUR = 60;

int main()
{
    //define variables
    int strtTime;
    int endTime;
    int minStrt;
    int minEnd;
    
    //prompt user to input start and end time
    cout << "Please enter a start and end time for the same day in military time: ";
    cin >> strtTime >> endTime;
    
    //output time in minutes and output elapsed time
    cout << "Your start time is: " << militaryToMinStrt(minStrt) << " minutes" << endl;
    cout << "Your end time in minutes is: " << militaryToMinEnd(minEnd) << " minutes" << endl;
    cout << "Your elapsed time in minutes is: " << elapsedTime(minStrt, minEnd) << " minutes"<< endl;
    
    return EXIT_SUCCESS;
}

int militaryToMinStrt(int minStrt)
{
    minStrt = ((strtTime / 100) * 60) + (strtTime % 100);
    
    return minStrt;
}

int militaryToMinEnd(int minEnd)
{
    minEnd = ((endTime / 100) * 60) + (endTime % 100);
    
    return minEnd;
}

int elapsedTime(int strtTime, int endTime)
{
    return (militaryToMinEnd(minEnd)) - (militaryToMinStrt(minStrt));
}


I made the variables being called inside the functions, universal variables. This made the code work perfect. But since my professor does not want that, and she said we would learn next week how to call a variable in a function that is in the main function, that I would not have to worry.

I also had to make militaryToMinStrt & militaryToMinEnd instead of using miltaryToMin(int ) because when I would try to use the single function for 2 different calls, I would get an error saying I was trying to redefine militaryToMin. This makes perfect sense...

So all in all, I want to say thanks again for all the help because I truly do appreciate it. I have learned plenty from this post alone!

I will post on here how to call a variable from the main function when we learn. I am sure 95% of you people already know this, but I will do it for a personal preference and for those who stumble upon this post and don't know how to do it!

Thanks again! :)
Last edited on
I also had to make militaryToMinStrt & militaryToMinEnd instead of using miltaryToMin(int ) because when I would try to use the single function for 2 different calls, I would get an error saying I was trying to redefine militaryToMin. This makes perfect sense...

No it does not. They have the exact same formula so it is redundant to duplicate the function with two different names. The idea behind functions is to be able to reuse them. Renaming it to something like milToStand() would make more sense though.
1
2
3
4
5
6
int milToStand(int milTime)
{
    milTime = ((milTime / 100) * 60) + (milTime % 100);
    
    return milTime;
}

This is not redefined when it is called with minStrt or minEnd or someother variable. Only when you change the argument variable is it redefined, such as:

1
2
3
4
5
6
7
8
9
10
11
12
13
int milToStand(int minStrt)
{
    minStrt = ((strtTime / 100) * 60) + (strtTime % 100);
    
    return minStrt;
}

int milToStand(int minEnd)
{
    minEnd = ((endTime / 100) * 60) + (endTime % 100);
    
    return minEnd;
}

None of that solves your integer division problem though.
There is no integer division problem. The function(s) convert "military" integer (e.g. 0745) into hours (7) and minutes (45), turn the hours to minutes (7*60=420) and sum up (465). The unit of output is "minutes from midnight".


The real issue is the lack of understanding of the purpose of function parameters. Lets make an example function:
1
2
3
4
int foo( int bar ) {
  bar = 42;
  return bar;
}

It does not matter what number we call this function with, because the number we give is always thrown away and replaced with 42. We could as well write:
1
2
3
int foo() {
  return 42;
}

Now foo does not even pretend that it would care about what numbers we might have.

The other apparent error comes from using same variable names in caller (main) and called (militaryToM*). It is not a syntax error, but it distracts our thinking.
1
2
3
4
5
6
7
int foo( int input ) {
  int result = 0;
  // compute result from input
  result = (input / 100) * 60 + (input % 100);

  return result;
}

Or shorter:
1
2
3
int foo( int input ) {
  return (input / 100) * 60 + (input % 100);
}

@OP i was like... "what?!"

you may want to read more about the functions.

you are using the local variable of main function which is have to use for passing the parameter to function militaryToMinStrt which is a returning function
Last edited on
There is no integer division problem. The function(s) convert "military" integer (e.g. 0745) into hours (7)

I see that I was not understanding the intended input and integer division is intended.

No it does not. They have the exact same formula so it is redundant to duplicate the function with two different names. The idea behind functions is to be able to reuse them. Renaming it to something like milToStand() would make more sense though.
1
2
3
4
5
6
int milToStand(int milTime)
{
milTime = ((milTime / 100) * 60) + (milTime % 100);

return milTime;
}


They aren't the exact same formula.. One is converting the military START time to minutes and the other is converting END time to minutes. I guess in a sense it is the same formula, just different variables.

@OP i was like... "what?!"

you may want to read more about the functions.

you are using the local variable of main function which is have to use for passing the parameter to function militaryToMinStrt which is a returning function


Which variable are we talking about?

I see that I was not understanding the intended input and integer division is intended.


Adding the remainder solves my problem of losing it in the first place.. If that's what you're wondering. If not, sorry for the confusion on my end.
Pages: 12