Thread: Help with an error with Sleep()

  1. #1
    Griffcub2002
    Guest

    Help with an error with Sleep()

    I have used Sleep() before in another program and never had any problems, but this time when I compile it is telling me that Sleep is an undeclared identifier. I #included <ctime> like I did the last time I used it, but it doesn't want to work this time. Hopefully someone can find what I did wrong and let me know.
    Thanks
    Van


    Code:
    #include "Date.h"
    #include "Classroom.h"
    #include <iostream>
    #include <ctime>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    template <typename T>
    void sortArray(T array[], int size);
    void printArray(Classroom array[], int size);
    void openFile(ifstream& fin);
    int fillArray(ifstream& fin, Classroom array[]);
    
    
    int main()
    {
    	Classroom c_array[20];
    	ifstream in;
    	int rooms;
    
    	
    	openFile(in);
    	
    	rooms = fillArray(in, c_array);
    	
    	printArray(c_array, rooms);
    	system("pause");
    	system("cls");
    	
    	sortArray(c_array, rooms);
    
    	printArray(c_array, rooms);
    	
    	system("pause");
            
                   return 0;
    
    }
    
    int fillArray(ifstream& fin, Classroom array[])
    {
    
    	Classroom temp;
    	int i=0;
    	int no_rooms;
    	
    	fin >> no_rooms;
    	fin.ignore(200, '\n');
    	
    	temp.ReadClassroom(fin);
    	while(!fin.eof() && i <= no_rooms)
    		{
    		array[i] = temp;
    		temp.ReadClassroom(fin);
    		i++;
    		}
    	return no_rooms;
    }
    
    
    void openFile(ifstream& fin)
    {
    	fin.open("a:studentData.txt");
    	if(fin.fail())
    	{
    		cout << "Error opening file";
    		system("pause");
    		exit(1);
    	}
    }
    
    void printArray(Classroom array[], int size)
    {
    	cout.setf(ios::fixed);
    	cout.precision(0);
    
    	for(int x=0; x<size; x++)
    	{
    		array[x].PrintStats(cout);
    		Sleep(1000);//<<<<<<<<<<<<<<	
                    }
    }
    
    
    template <typename T>
    void sortArray(T array[], int size)
    {
    	int pass, j;
    	T temp;
    	int smallIndex;
    
    	for(pass = 0; pass < size-1; pass++)
    	{
    
    		smallIndex = pass;
    
    		for(j= pass+1; j < size; j++)
    		{
    			if(array[j] < array[smallIndex])
    				smallIndex = j;
    		}
    
    			if(smallIndex != pass)
    			{
    				temp = array[pass];
    				array[pass] = array[smallIndex];
    				array[smallIndex] = temp;
    			}
    		
    	}
    	
    
    }

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Sleep() is in windows.h i think.

  3. #3
    Griffcub2002
    Guest
    Thanks Hybrid, that worked But it still remains a mystery why my last program was bale to use Sleep() without includeing windows.h? I hate things I can't figure out. Well as long as it works now.

    Griff

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    test

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>But it still remains a mystery why my last program was bale to use Sleep() without includeing windows.h?
    Maybe you included another header that included windows.h. It happens a lot that standard headers include other headers to help them out and you can access those other headers :-)
    *Cela*

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    I bet that's what it was. I have a header written by someone that allows me to use colors and that may be it. Thanks for the info

  7. #7
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    what is sleep()
    AIM: MarderIII

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Hint

    I'm not sure what compiler/IDE you're using, but with MSVC++, (and I assume with other compilers) you can always go to the compiler's help index and type-in the function name. It'll tell you which header file it's in.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Noobie,

    Sleep() pauses execution while allowing other programs (in other windows, etc.) to run.

    This is NOT standard ANSI C++ !!! (So your compiler might not have it.)

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    I am running .Net and I know it has it. It must have been another header I was including that was already using it's header.

    Griff

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    1
    im pretty sure that you need to include <window.h>.
    I used to have the same problem with using Sleep() until i found out about this header file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 02:30 AM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM