2 things.

This is a discussion on 2 things. within the C++ Programming forums, part of the General Programming Boards category; Hi, I need help with 2 things. 1st thing is I want to make a header to hold my #includes ...

  1. #1
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    2 things.

    Hi, I need help with 2 things.

    1st thing is I want to make a header to hold my #includes and ints,floats,exc. When I do this it works ok with just my #includes out in this file, but when I move my ints(so on) into it also, it doesnt work at all. It gives me a error about something inside the file and says other things don't work.

    2end is: I want this line of code to refresh it self. Its a clock so I know the time, but it only tells me ones unless I reloop it when I hit enter or something.
    Code:
      time_t rawtime;
      struct tm * timeinfo;
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
    //--------menu------------------------
      printf ("Current date and time is: %s", asctime (timeinfo));

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. Not sure what you mean, but if you want to declare object such as int in a header file you need to preceed it with extern keyword, then declare it again in one, and only one, *.c or *.cpp file without extern
    Code:
    // *.h file
    extern int foo;
    2. If you want the time to keep ticking on the screen then you need to do that in another thread. create another thread, and an infinite loop in that thread that gets and displays current time once each second.

  3. #3
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok. Um, here look at this. Its a .h file from another program.
    Code:
    #include <iostream.h>	//cout & cin function
    #include <math.h>		//math functions
    #include <windows.h>	//system functions
    #include <conio.h>
    #include <stdio.h>
    
    void MENU();		//Decides what math option to execute
    void EXIT();		//Gives variable to exit
    void RorQ();		//Decides to restart or quit
    void clrscr();		//Clears the screen
    
    class variable		//Where variables are stored
    {
    public:
    	int I;
    	int menu;		//Variable for deciding the math option
    	int exit;		//Variable for deciding whether to exit or reloop
    	int exitRorQ;	//Variable for deciding whether to restart or exit
    	int num1;		//Variable for a math number for integers
    	int num2;		//Variable for a math number for integers
    	double num3;	//Variable for a math number for doubles
    	int equal;		//Variable for the math answer for integers
    	double equal1;	//Variable for the math answer for doubles
    	float equal2;	//Variable for the math answer for floats
    	char rq[1];		//Variable for 'r' or 'q' (restart or quit)
    	float per1;		//Variable for percent number in equation
    	float per2;		//Variable for percent number in equation
    };
    class variable C;	//Assigns class to C
    See, the main program calls on this file for the ints and includes. Mine on the other hand will only work with includes like this, and will give a error when I put my ints into it. Thes wont with its main program.


    2en thing. Ok, thanks but I cant seems to get that to work right with out stoping my 1st inft. loop. It kinda needs to be refresh with in my code. Maybe if I show more you will see.
    Code:
    bool pQuit = false;
     while( false == pQuit )
     {
         time_t rawtime;
         struct tm * timeinfo;
         time ( &rawtime );
         timeinfo = localtime ( &rawtime );
    //--------menu------------------------
      printf ("Current date and time is: %s", asctime (timeinfo));
      cout<<"0. Exit\n";              //--
      cout<<"1. Add\n";               //--
      cout<<"2. Subtract\n";          //--
      cout<<"3. Multiply\n";          //--
      cout<<"4. Divide\n";            //--
      cout<<"5. Absolute Value\n";    //--
      cout<<"6. Percent of\n";        //--
      cout<<"7. Polygons\n";          //--
      cout<<"8. Open File\n";         //--
      cout<<"9. Save File\n";         //--
      cout<<"10. Size of File\n";     //-- 
      cout<<"Selection: ";            //--
    //------------------------------------
    Under that is the cases for thos couts, the main code.
    Last edited by adr; 01-22-2006 at 06:32 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The first example actually creates an instance of the c++ class namded C. NEVER NEVER do that in a header file (*.h file) without using extern keyword.
    Code:
    extern class variable C;	//Assigns class to C
    Now, in the main.cpp (or some other *.cpp file), do the same thing but without the extern keyword.
    Code:
    class variable C;	//Assigns class to C
    Last edited by Ancient Dragon; 01-22-2006 at 11:19 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    as for the time, here is an example -- it may not give you the complete desired output, but its a start. You will probably have to move the cursor to a specific location then display the text. Here is some example code. Click Components link at the top of the page, then scroll down and find Console Object.

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    DWORD WINAPI ThreadProc(LPVOID param)
    // The is a thread that only displays the date/time on current
    // console line.
    {
        time_t t;
        struct tm* tm;
        char buf[126];
        for(;;)
        {
            // get current time
            t = time(0);
            tm = localtime(&t);
            // format for display
            sprintf(buf,"%02d/%02d/%04d %02d:%02d:%02d",
                  tm->tm_mon+1,tm->tm_mday,tm->tm_year+1900,
                  tm->tm_hour,tm->tm_min,tm->tm_sec);
            // move cursor to beginning of line then shot the text
            cout << "\r" << buf;
            // put thread to sleep for 1 second
            Sleep(1000);   
        }
        return 0;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        HANDLE hThred;
        DWORD dwThreadID;
        hThred = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by Ancient Dragon; 01-22-2006 at 11:27 PM.

  6. #6
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Hmm,Thanks. I got some questions on the thread making tho. I've been looking it up so I understand what you did, but didnt finde much. (i'm still perty new to this stuff) I try puting what you had in my code and it didnt come out right... Maybe I have this wrong or something. Ok to start from what I know now so I can understand it more: threads hold what your doing, but! seeing as I have a inft. loop right off in my thread and want to put another in it will stay in 1st inft. loop till its done (witch is never).
    Now with another thread, one can do my main program, and the other can do the time. The time one should be 1st to show the time (witch is where I want it so it shows 1st)
    Code:
    DWORD WINAPI ThreadProc(LPVOID param)
    This is the name of the thread right?
    Code:
    int main(int argc, char *argv[])
    {
        HANDLE hThred;
        DWORD dwThreadID;
        hThred = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    What is this then? I get this creads a thread (and pausing the thread in system) , but thats like all I know.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if you are writing a dialog, then you want to create the dialog if the program's main thread (where main() or winmain() exists), then in the second thread to the time stuff and display the time in a text control of the dialog. In my example, ThreadProd() is the entry point of the new thread, similar to main() is the entry point into the program itself. Programs may contain as may threads as desired -- there is no theorical limit, but there is a practial limit. Games use a lot of threads -- all those little critter you see moving all around are in different threads. You don't have to name the new thread "ThreadProc" -- you can name it anything you wish and can even be a static method of a c++ class.

  8. #8
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok, I thought that was what it was, wasnt sure (why you name the thread and the creat threat thing). You kinda got me lost on what you were saying tho, sorry not to be rud or anything.
    if you are writing a dialog, then you want to create the dialog in the program's main thread (where main() or winmain() exists), then in the second thread to the time stuff and display the time in a text control of the dialog. In my example, ThreadProd() is the entry point of the new thread, similar to main() is the entry point into the program itself.
    Ok, thanks I kinda get that, but I dont get something here tho too. So your example is a new thread right? Hmm it could be how I think the new thread should work maybe thats stoping me from understand. This is how I think the new thread works also.
    ~main program, things after ~int main(void)~>|
    this is the 1st thread, where I end int main~> / \ <~this is the 2end thread where I start after ending int main
    Last edited by adr; 01-23-2006 at 07:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 10:23 AM
  2. Plants that eat things
    By StinkyRyan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-05-2004, 04:41 PM
  3. How are things looking
    By cyberCLoWn in forum C++ Programming
    Replies: 8
    Last Post: 01-15-2004, 02:53 PM
  4. Selecting things on the canvas
    By Barjor in forum Windows Programming
    Replies: 0
    Last Post: 08-30-2001, 03:10 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21