What is wrong with this code...

Hi,

I cant figure out on what is wrong with the code I have made. Anyone can help. I am getting error while compiling.

'ABC' undeclared(first use this function)
'buy' undeclared(first use this function)
[Warning] the address of 'int TodaysDate()'. will always eevaluate as 'true'.

What does this error means.

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
#include <ctime>
#include <iostream>
using namespace std;

// Creates current date and loop it by adding 1 day only upto 5 days
int TodaysDate()
{
        time_t now = time(0);
        tm *ltm = localtime(&now);

        cout << ltm->tm_mday << '/' << 01 + ltm->tm_mon << '/' << 1900 + ltm->tm_year << endl;
}

// Declaration of main()    
int main()
{
    // variables declaration
    int Day1, Day2, Day3, Day4, e, XCode, Activity;
    float f;
    float g;
    float h;
    float i;
    float j;
    float k;

    //actual initialization
    XCode = ABC;
    Activity = buy;
    Day1 = 100;                    
    Day2 = 20;             
    Day3 = 200;            
    Day4 = 150;            
    e = 40;             
    f = 20.00;
    g = 24.00;
    h = 36.00;
    i = 30.00;
    j = 35.50;
    k = 27.55;

    cout << TodaysDate << ',' << "XCode" << ',' << "Activity" << ',' << Day1 << ',' << f <<endl; 
    return 0;
}



Thanks,


Marcus
Declare ABC and buy and initialize them with some value...
Some comments:

Your TodaysDate function is supposed to return an int, but there is no return statement. With std::cout it looks as though it should have a return type of void, but then you call the function on line 41. Perhaps you should get it to return a std::string ? #include <string> if you want to use strings. Or you could just call the function first - like this to the same effect:

1
2
TodaysDate(); //change the function so it doesn't print an endl. Notice the () in the function call
cout <<  ',' << "XCode" << ',' << "Activity" << ',' << Day1 << ',' << f <<endl;

It is a good idea to declare you functions before main, and define them after main. If there are lots of functions, we don't have to scroll through all of them to see what main() does - main() may not call all of them.

You can declare & initialise all in one go like this:

float f = 20.0;

So lines 18 to 24 & 27 to 39 could be combined together.

With the ABC & buy - did you mean them to be "ABC" & "buy" - string literals as opposed to variables?

The 01 on line 11 doesn't pre-pend a zero to the answer, a leading zero actually means the number is in octal format & 0x means hexadecimal. You could check if the value is less than 10, then use the std::string operator + to pre-pend the zero.

Hope this helps ! Have fun :D
Last edited on
Hi TheIdeasMan,

Appreciate your response.

With regards to ABC & buy, yes I meant it to be "ABC" & "buy". Because sooner on the code I will be changing it to another strings!

Once again appreciate all your responses.


Thanks,

Marcus
Topic archived. No new replies allowed.