Solved

Attempt of a basic header File.

Posted on 2004-08-10
9
334 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  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 …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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