Solved

Attempt of a basic header File.

Posted on 2004-08-10
9
332 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
ID: 11766564
Add the .h extension

#include <windows.h>
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 40 total points
ID: 11766578
>>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
ID: 11766587
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
ID: 11766681
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 11766733
>>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
ID: 11766789
: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
ID: 11766836
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
ID: 11766852
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
ID: 11767220
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 113
Unable to start eclipse ? 17 128
Create a path if not exists 7 68
gdb doesn't stop on breakpoint 2 45
  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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

11 Experts available now in Live!

Get 1:1 Help Now