Solved

Attempt of a basic header File.

Posted on 2004-08-10
9
329 Views
Last Modified: 2010-04-01
Hi all,
I'm rather new to C++, and that's why I've made some stupid mistakes (i'm sure) and can't spot them.
I've coded a very simple header file that can be used as an algorithm for Traffic Lights.

This is the header file below:

//  traffl.h
//  # A basic Traffic Light Algorithm.
//------------------------------------

#ifndef  _traffl_
#define  _traffl_

#define  BEFORE_RED          2
#define  BEFORE_AMBER        2
#define  BEFORE_GREEN        3
#define  GREEN_DURATION      12

#include <iostream>
#include <windows>
using namespace std;

class traffl
{
    public:
        void lightControl ( int *l, bool *end )
        {
            // int e_c  =  number of elements;
            e_c = sizeof(*l);

            while ( !(*end) ) {
                for ( int a = 0; a < e_c; a++ ) {
                    for ( int b = 0; b < e_c; c++ ) *l[b] = 0;
                    Sleep( 1000 * BEFORE_AMBER   );
                    *l[a] = 1;
                    Sleep( 1000 * BEFORE_GREEN   );
                    *l[a] = 2;
                    Sleep( 1000 * GREEN_DURATION );
                    *l[a] = 1;
                    Sleep( 1000 * BEFORE_RED     );
                }
            }


        }
};

#endif


The above header file is saved as: traffl.h  and saved in the same directory as the below program (that attempts to use it):

//  t_tl.cpp
//  # A prog to make use of the traffl class.
//------------------------------------------------

#include <iostream>
#include "traffl.h"
using namespace std;

void traffl::lightControl( int l[], bool end );    // Prototype.

int main()
{
    int ar[4] = { 0, 0, 0, 0 };

    bool e = false;
    traffl tl;
    tl.lightControl( ar, e );

    // Process data (altered by pointers in traffl.h) here.

    Sleep( 5000 );
    e = true;

    return 0;
}


However, when I compile the t_tl.cpp program, I get one error, which is:
Line 6: c:\docume~1\admin\mydocu~1\algori~1\traffi~1\t_tl.cpp
c:\docume~1\admin\mydocu~1\algori~1\traffi~1\traffl.h:14: windows: No such file or directory

I'm guessing it cannot find the header file I'm trying to import...? I have also copied and pasted the header file (traffl.h into my compilers library, (along with iostream.h, etc..) in the PATH directory, right?).

Any ideas what's going on here??

Thanks in advance!
[r.D]
0
Comment
Question by:DrWarezz
  • 3
  • 3
  • 3
9 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 40 total points
Comment Utility
Add the .h extension

#include <windows.h>
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 40 total points
Comment Utility
>>c:\docume~1\admin\mydocu~1\algori~1\traffi~1\traffl.h:14: windows: No such file or directory

That should be

#include <windows.h>

Furthermore,

        void lightControl ( int *l, bool *end )
       {
           // int e_c  =  number of elements;
           e_c = sizeof(*l);

           while ( !(*end) ) {
               for ( int a = 0; a < e_c; a++ ) {
                   for ( int b = 0; b < e_c; c++ ) *l[b] = 0;
                   Sleep( 1000 * BEFORE_AMBER   );
                   *l[a] = 1;
                   Sleep( 1000 * BEFORE_GREEN   );
                   *l[a] = 2;
                   Sleep( 1000 * GREEN_DURATION );
                   *l[a] = 1;
                   Sleep( 1000 * BEFORE_RED     );
               }
           }


       }
is very unlikely to work, since

 e_c = sizeof(*l);

will always return '4' and 'end' will never change.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Most header files have a .h extension.
The STD header files are the main exception to this rule. (<iostream> <string>, <map>, <vector>, etc.....).

All the windows header files have a *.h extension.
0
 
LVL 9

Author Comment

by:DrWarezz
Comment Utility
Hi,

>> "e_c = sizeof(*l);      \n will always return '4' and 'end' will never change"
Why exactly would "int e_c = sizeof(*l);" always return 4?
Also, why would 'end' never change?  I assumed that because it was pointing to the 'end' variable in the t_tl.cpp program, then when the t_tl.cpp changes the 'end' variable, then it would update it in the header class.
Would I need to use a reference instead of a pointer? Or, can what I'm trying to do simply not be done?

Thanks so far,
[r.D]
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Why exactly would "int e_c = sizeof(*l);" always return 4?

Because it's the size of an 'int' you are evaluating.

>>Also, why would 'end' never change?

Your processing is not parallel. The control will return to 'main()' only after 'lightControl()' has terminated.
0
 
LVL 9

Author Comment

by:DrWarezz
Comment Utility
:o\   Oh.

Is there anyway that I can allow the main() function to control how long the "lightControl()" runs for?
Hmm... Would I have to rely on threads for this?  If so, do you know of a decent "Thread tutorial" for C++ ?
I can do them fine in Java... But, this C++ is soo much harder. lol.

Thanks,
[r.D]
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Why do you want to do threads?
Is this for homework?

If you need the size of the array, you should pass it as an additional argument.

void lightControl ( int *l, int SizeOf_I, bool *end )

Example usuage:

tl.lightControl( ar, sizeof(ar)/sizeof(ar[0]), e );

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Hmm, I'd recommend http://msdn.microsoft.com/library/en-us/dndllpro/html/msdn_threads.asp ("Multithreading for Rookies")
0
 
LVL 9

Author Comment

by:DrWarezz
Comment Utility
Hmm.. That was really stupid what I done; To call the method, then expect control of it AFTER it's finished (assuming it was running as a seperate process (ie, a thread)).
You see, thinking back to my Java days :P lol, when I wanted the ability to run a process (by calling another method), then be able to end it at any point, I would create the process as a thread, then kill it when I wanted to.
For some reason, I assumed that I could call the method "lightControl( ... )", and that the main() function would continue to run, WHILE this lightControl() was also.. I know this wouldn't happen, yet I, for some reason attempted it!! lol :o\ Damn... how stupid! lol.

I also get what you mean by the "would always return "4"" thing now. lol.. It's ALL making sense!! :P

Thanks,
[r.D]

I shall look at the thread tutorial, recommended by "jkr" now. Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now