Quantcast

Maximum PC

It is currently Wed Jul 23, 2014 11:08 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: c++ "sleep" problem
PostPosted: Sat Dec 15, 2007 1:52 pm 
8086
8086

Joined: Sun Sep 16, 2007 9:53 pm
Posts: 7
Location: bulgariaa
Hi all
I want to print "Hello" and "World" with delay.
But when I start the program it first executes the "sleep" command,and after that prints on screen all the text,why? in my code the "sleep" command is not in the beggining,but in the middle..
Code:
#include <iostream.h>
#include <windows.h>


int main()

{
   cout<< "Hello \n";
   Sleep(1000);
  cout<<"World! \n";
   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 3:57 pm 
Team Member
Team Member
User avatar

Joined: Sun Jun 12, 2005 5:52 pm
Posts: 1319
Location: Infront of my Computer.
#include <iostream.h>
#include <windows.h>


int main()

{
cout<< "Hello \n";
Sleep(1000);
cout<<"World! \n";
return 0;
}

Notice there is no space between the cout<<"World!

You might want to fix that.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 7:05 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
Digital-Storm wrote:
Notice there is no space between the cout<<"World!

You might want to fix that.

That doesn't matter, C++ *generally* isn't space sensitive. The compiler strips out all the whitespace that isn't between quotes when it goes to compile.

marsianche: What compiler are you using? Everything compiles and works fine for me in dev c++.

Are you sure its not working correctly though? That number you run through Sleep() is milliseconds, so its only going to pause for 1 second.


Last edited by gamerfreak on Sat Dec 15, 2007 7:13 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 7:13 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
I compiled and ran your program, as is, and it worked fine, used Dev C++ on Windows XP. What did you use to compile and run it (os and compiler)?


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 11:59 am 
8086
8086

Joined: Sun Sep 16, 2007 9:53 pm
Posts: 7
Location: bulgariaa
Well I,m using Microsoft Visual C++ 6.0, and until now i had not any problems with it.. (and Windows XP)


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 8:11 pm 
Coppermine
Coppermine

Joined: Wed Jul 07, 2004 11:07 am
Posts: 666
Location: Woodland Hills, CA
Digital-Storm wrote:
#include <iostream.h>
#include <windows.h>


int main()

{
cout<< "Hello \n";
Sleep(1000);
cout<<"World! \n";
return 0;
}

Notice there is no space between the cout<<"World!

You might want to fix that.


Image


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 2:30 am 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Your code is ugly but it should work exactly as you wrote it. Your program is doing exactly as it's told. If you had actually taken the time to step through your program in a debugger, you'd find that your text actually *is* being sent before Sleep() is called. If you'd done a little more googling, you'd find out exactly what your problem is. Maybe you could look at some other code samples and see the differences? I know exactly why your program isn't doing what you want, but if I just tell you then you won't learn a thing! Do some research and post back here if you still need help.

PS: iostream.h was deprecated a *long* time ago -- start using iostream and start programming in C++ ;)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Sep 20, 2008 11:32 pm 
8086
8086

Joined: Sat Sep 20, 2008 11:25 pm
Posts: 1
OK i am new a noob i just downloaded dev c++ and i defenetly wanna learn i was trying stuff and got ur code to work btw i added so the color will be red on the cmd

Code:
#include <iostream.h>
#include <windows.h>


int main()

{
   cout<< "Hello \n";
   system("color c");
   Sleep(1000);
  cout<<"World! \n";
  system("pause");
   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2008 4:36 am 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i modified your code a little bit, and it works for me.

Code:
#include <iostream>
#include <windows.h>
using namespace std;

int main()

{
    cout<<"Hello\n";
    system("color c");
    Sleep(1000);
    cout<<"World\n";
    cin.get();

}


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group