Author Topic: How to make this work in C++  (Read 850 times)

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
How to make this work in C++
« on: August 26, 2005, 09:58:17 pm »
I have two questions.


1.)
What should I do to make the computer rest for a period of time. Like lets say i need the computer to incremet a value \"x\" once every second. I could make a loop that retrieves the value of \"time(0)\"  (found in the library) and increments the value of \"x\" when a different value is returned.

Example:

Code: [Select]


// Increment  \"x\" every second until x == 10


for( int x = 0, int t = time(0);x<11; ){
     if(t != time(0)){
          cout << \"\\n\";
          cout << x;

          t = time(0);
     }
}



The problem with this kind of code, is that it forces the processor to be constantly active.

So is there a way to just tell the processor to stop paying attention to that part of the programm for a predetermined period of time, and let it concentrate on something else?





2.)
How can I clear the information that is already writen on the screen? Lets say I have a timer program (which would also tie in with the 1st question). The code for making the actual clock work is prety obvious (when seconds == 60, seconds = 0, minutes++).

The problem is that if I don\'t have a way to clear the screen, the output will look something like this:

Code: [Select]

00:00:01
00:00:02
00:00:03
...


So how would  I go about clearing the screen? Say, before every time that the time is writen?


=========================================

Does anyone know how to do that?



     



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

steuben

  • Veteran
  • *
  • Posts: 1834
    • View Profile
    • Myspace
(No subject)
« Reply #1 on: August 26, 2005, 10:25:32 pm »
to answer the second question. assuming that you\'re running in command line, rather than graphics cause this will probably weird graphics. and bearing in mind that this will work, but maybe a bit of a \"dirty\" way of doing in.

basically you can write the values that you want to display directly to the screen. i can\'t remember the exact memory address. but a quick search will dig that up. basically put the correct address in a variable and use it as a pointer run from there. edit: basically you can edit up the string that you want to display and write it to that memory.

i _vaguely_ recall a positioning functioning in one of the libraries but i couldn\'t tell you which one. which dodges the \"dirty\" method, and may be closer to oop, or whatever the concept of the day is.
« Last Edit: August 26, 2005, 10:35:47 pm by steuben »
may laanx frighten the shadow from my path.
hardly because the shadow built the lexx.
the shadow will frighten laanx from my path.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #2 on: August 26, 2005, 11:17:52 pm »
Hrmm, I\'m not sure what library it\'s in (iostream, perhaps?), but there is a very simple command to clear the screen:
system(\"cls\");

As for your first problem, there probably is a way to have a thread wait (I know how to do this in Java), but I don\'t know the syntax for it in C++. Have you Googled it? You can find most programming syntax answers through Google. ;)
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

Seytra

  • Forum Addict
  • *
  • Posts: 2052
  • No system can compensate lack of common sense.
    • View Profile
(No subject)
« Reply #3 on: August 27, 2005, 03:33:41 am »
For the timer question, this depends on what you need.
There is the sleep(seconds) system call, but it will not let you sleep for less than 1 second.

Another alternative may be to use select() with only the timeout parameter filled in, though this won\'t let the program do anything in parallel, it\'ll just let go of the CPU for what you set in timeout, so that other processes can use the CPU.

Therefore you may wish to look into timers (man setitimer).
You will also have to look into signals for this to work.

This is a quick example I ripped off a program of mine sloppily adapting it to fit what you seem to need.

Without the timer it will be simpler, though:
Code: [Select]

#include //setitimer,etc.
#include
#include

int main(void)
{

       unsigned int timeouts;
       struct itimerval itv;
        int exit_loop=0;
        itv.it_interval.tv_sec=MY_TIMEOUT_SEC;
   itv.it_interval.tv_usec=MY_TIMEOUT_USEC;
        while (!exit_loop)
       {
             selectresult=select(0,NULL,NULL,NULL,&itv);
...
       }
}


Example with timer:
Code: [Select]

#include //setitimer,etc.
#include
#include


unsigned int timeouts;
struct itimerval itv;

void MY_SIGALRM_Handler(int signal)
{
    timeouts++;
}

int Reset_Timer(void)
{
  itv.it_value=itv.it_interval;
   return setitimer(ITIMER_REAL,&itv,NULL);//start timer
}

int main(void)
{
        int exit_loop=0;
        sigset_t MYSignalMask;
        sigemptyset(&MYSignalMask);
        struct sigaction target;
  target.sa_handler=MYSIGALRM_Handler;
    target.sa_mask=MYSignalMask;
    target.sa_flags=SA_RESTART;
 target.sa_restorer=NULL;
    sigaction(SIGALRM,&target,NULL);
        itv.it_interval.tv_sec=MY_TIMEOUT_SEC;
  itv.it_interval.tv_usec=MY_TIMEOUT_USEC;
        Reset_Timer();
        while (!exit_loop)
       {
             selectresult=select(0,NULL,NULL,NULL,NULL);
...
        }
}


As I said... sloppy, as I\'m on PS ATM, but that\'s reasonably what it should look like. You get the idea, anyway.

Edit: For the second question: this obviously differs depending on what platform you use. In Linux, on the console, using (n)curses you\'d need to do erase() to clear the entire screen, or use mvprintw() to overwrite the screen at the specified position.
On the windoze console, it is called clrscr() and gotoxy().
In windoze mode, this will be RedrawWindow(), while in Linux this completely depends on what library you are using to interface to the X server.
« Last Edit: August 28, 2005, 06:54:34 am by Seytra »

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #4 on: August 30, 2005, 06:18:57 pm »
Ok, I opened up an old timer program that I\'ve writen, and fixed it up a bit so that now it clears the screen.

I still don\'t understand how to make a better counting system... But what ever... I\'ll learn that later.

Another question that Seytra just reminded me of was this:

What is the point of passing variables to the int main function? I read about it in a book (C++ A Beginners Guide), but the author was awfuly unclear on that part. From what I understood the variables were going to be filled when the program is run in command prompt...

Is that the same as in Linux where you can follow up commands with things like -l or -less?

I\'m just completely confused on on what that is and what it is used for.



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #5 on: August 30, 2005, 06:35:35 pm »
Yes, that\'s right. The same works in Java. When something is run from the command line, you can specify additional parameters which (if the main function has arguments) will be used in the main function. You should probably make all the arguments strings, to be safe, though.
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

Seytra

  • Forum Addict
  • *
  • Posts: 2052
  • No system can compensate lack of common sense.
    • View Profile
(No subject)
« Reply #6 on: September 01, 2005, 08:59:30 pm »
When you don\'t use void as parameter, then you use
int main(int argc,char **argv,char **env)
The env argument is optional even then, usually it isn\'t used.
(Technically, you can use argc only, but that would not make a lot of sense.) Be aware that it is not possible to leave out arguments in between. You can use
argc,argv,env
argc,argv
argc
void
but not
argv
or
argc,env
Neither can you swap their positions.
The reason is that compiler doesn\'t look at the names, neither are the names stored in the executable. You can just as well call them a, b and c; it\'s just a convention that they are commonly called argc (ARGument Count), argv (ARGument Vector) and env (ENvironment Vector).
Argc is the number of arguments that have been passed. It will always be one more than the actual number, because the very first argument is the command line that was used to call the program.
The argument argv contains an array of pointers to strings, each of them being one argument. Basically, they are what the shell parsed by looking at the spaces.
Example:
#./testprog -arg1 arg2 -arg3-d
would become:
argc=4
argv[0]: ./testprog
argv[1]: -arg1
argv[2]: arg2
argv[3]: -arg3-d

The env argument contains an array of pointers to strings containing the environment variables.

leji

  • Hydlaa Citizen
  • *
  • Posts: 305
    • View Profile
(No subject)
« Reply #7 on: September 02, 2005, 12:50:22 am »
Quote
There is the sleep(seconds) system call, but it will not let you sleep for less than 1 second.

not sure about C++ but in C you can use nanosleep, which will let you define how many nano seconds you want to wait (nano = 10^-9 )
there\'s no place like 203.81.47.91

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #8 on: September 02, 2005, 01:10:45 am »
The Sleep() function in c++ works on milliseconds, not seconds.  At least it does for Windows API.  

I think something like this is what you\'re looking for:
Code: [Select]
#include
#include
#include

using namespace std;

int main (int argc, char *argv[])
{
    int seconds = 0,mintues = 0,hours = 0;

    while(true){
        seconds++;
        if (seconds > 59) {
            mintues++;
            seconds = 0;
            if (mintues > 59) {
                hours++;
                mintues = 0;
                if (hours > 23) {
                    hours = 0;
                }
            }
        }
        Sleep(1000);
        system(\"cls\");
        cout << \"The time is \" << setw(2) << hours << \":\"
                               << setw(2) << mintues << \":\"
                               << setw(2) << seconds << endl;
    }
    cin.get();
}


Although, by cheating, I can do it alot easier:
Code: [Select]
#include
#include

using namespace std;

int main (int argc, char *argv[])
{
    while (true) {
        system(\"cls\");
        system(\"time /T\");
        Sleep(1000);
    }
}
« Last Edit: September 02, 2005, 01:14:23 am by ramlambmoo »