cout formating question

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

cout formating question

Postby bwcal1999 » Thu Jul 15, 2004 8:06 am

What i want to do is make a spinner in linux to make it look like the computer is "thinking" while some other process is working. So i was just playing around and can't figure out how to get one to work. Attached is my code. Maybe someone can take a look at it and let me know how i can get it to work.

#include <iostream>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int main()
{
pid_t mypid;
mypid=fork();

if(mypid==0)
{

for(int i=0;i<4;i++)
{
if(i==0)
{
cout<<"|";
}
if(i==1)
{
cout<<"\b";
cout<<"-";
}
if(i==2)
{
cout<<"\b";
cout<<"|";
}
if(i==3)
{
cout<<"\b";
cout<<"-";
}

sleep(1);
}
return 1;
}

return 0;
}
bwcal1999
 

Postby tomcant » Thu Jul 15, 2004 8:32 am

This is your code with formatting, although I have changed it slightily. You will need to convert the Sleep() function from <windows.h> to something compatible with your Linux. It works nicely for me. :)

Code: Select all
#include <iostream>
#include <conio.h>
#include <windows.h>

int main()
{
   while(!kbhit())
   {
      for(int i = 0; i < 4; i++)
      {
         if((i == 0) | (i == 2)) 
            std::cout << "\r|";

         if((i == 1) | (i == 3))
            std::cout << "\r-";

         Sleep(250);
      } 
   }

   return(0);
}
Last edited by tomcant on Thu Jul 15, 2004 8:35 am, edited 1 time in total.
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby bwcal1999 » Thu Jul 15, 2004 8:34 am

what does the -\ do?
bwcal1999
 

Postby tomcant » Thu Jul 15, 2004 8:35 am

bwcal1999 wrote:what does the -\ do?


You'll have to show it to me in my code. I can't find it.
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby bwcal1999 » Thu Jul 15, 2004 8:37 am

This does not work for me, i think it has somethng to do with the thread and how the output is buffred.
Anyother suggestions
bwcal1999
 

Postby tomcant » Thu Jul 15, 2004 8:58 am

Well of course it doesn't work for you... your on a Linux! :D I did say it wouldn't...

You need to change the Windows specific code, which is just the Sleep() bit. I think before you had sleep(1) or something. Try using that again.
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby bwcal1999 » Thu Jul 15, 2004 9:02 am

Well i am not THAT stupid :) Ofcouse i changed the code! What i get when i run it is just one - on the screen after a few seconds.
what does the \r do?
bwcal1999
 

Postby Guest » Thu Jul 15, 2004 9:05 am

go home wrote:That is obviously Dave Sinkula so go back to http://cboard.cprogramming.com/index.php? and stay there.
Last edited by Guest on Thu Jan 06, 2005 9:08 am, edited 1 time in total.
Guest
 

Postby bwcal1999 » Thu Jul 15, 2004 10:46 am

YEP that did it thanks!!!!
bwcal1999
 

Postby tomcant » Thu Jul 15, 2004 2:14 pm

bwcal1999 wrote:YEP that did it thanks!!!!


:roll: But, my version worked great for me.... is this not what you were looking for?

Nevermind though, I guess you got it working now. :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Re: cout formating question

Postby aukahi » Thu Jul 15, 2004 8:23 pm

this is the wrong way to use processes, your main process didn't wait for the child process to end.

Code: Select all
#include <iostream>
#include <stdlib.h>
#include <unistd.h>

// added headers for wait(int &)
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
   pid_t mypid;
   mypid=fork();
        int status;
   
   if(mypid==0)
   {
      
      for(int i=0;i<4;i++)
      {
         if(i==0)
          {
           cout<<"|"<<flush;
           }
         if(i==1)
         {
         cout<<"\b";
         cout<<"-"<<flush;
         }
         if(i==2)
         {
         cout<<"\b";
         cout<<"|"<<flush;
         }
         if(i==3)
         {
         cout<<"\b";
         cout<<"-"<<flush;
         }
         
         sleep(1);   
      }   
      return 1;
   } else {  // your main function needs to get the return status of the child.

                 wait(&status);

         }

return 0;
}


if you don't want to use wait(int &) as it actaully pauses the main process, you need to use signals. anyway, your main process needs to get the child process's status or the child process will become "zombie".
aukahi
 
Posts: 198
Joined: Mon Jun 28, 2004 11:31 pm


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 0 guests