what is the header to time code isnt it;

#include "pctimer.h"

i keep gettin an error saying no such thing

Recommended Answers

All 7 Replies

>what is the header to time code isnt it;

I cannot quite parse what you are saying.

>#include "pctimer.h"
>i keep gettin an error saying no such thing

Unless you've created it, this is true.

Are you looking for time.h/ctime?

not my professor didnt teach us time.h he said use pctimer.h but did not tell us to create it? :confused:

so what do i have to do to create it?

he gave us this code in with the file we have to use. am i suppose to put this in my code or something? like i said he never said anything about creating this so i dunno.

#ifndef _PCTIMER_H

typedef double pctimer_t;

#if defined(_WIN32) || defined(__CYGWIN__)

#ifndef _WIN32
#define PCTIMER_NO_WIN32
#endif /* _WIN32 */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifdef PCTIMER_NO_WIN32
#undef PCTIMER_NO_WIN32
#undef _WIN32
#endif /* PCTIMER_NO_WIN32 */

__inline pctimer_t pctimer()
{
    static LARGE_INTEGER pcount, pcfreq;
    static int initflag;

    if (!initflag)
    {
        QueryPerformanceFrequency(&pcfreq);
        initflag++;
    }

    QueryPerformanceCounter(&pcount);
    return (double)pcount.QuadPart / (double)pcfreq.QuadPart;
}

#else /* Win32/Cygwin */

#include <sys/time.h>

__inline pctimer_t pctimer()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}

#endif /* Win32/Cygwin */

#endif /* _PCTIMER_H */

<< moderator edit: added [code][/code] tags >>

#include "pctimer.h"

...

pctimer_t begin = pctimer();

/* Code that you're timing */

printf ( "Time elapsed: %f\n", pctimer() - begin );

not my professor didnt teach us time.h he said use pctimer.h but did not tell us to create it? :confused:

so what do i have to do to create it?

he gave us this code in with the file we have to use. am i suppose to put this in my code or something? like i said he never said anything about creating this so i dunno.

Create a file with the contents that you posted, and save that file as pctimer.h.

ok i got it to work now thanks.

but now im getting an assertion error saying:

Program: F:\Debug\lab11.exe
File: fopen.c
Line: 54

Expression: *file!=_T("\0")

what does that mean? that code works if i hit ignore but the time wont be right cause it will time it untill i hit ignore.

#include <iostream>  // for cin and cout
#include "pctimer.h" // times the time taken to do the sort
#include <string>
#include <fstream>	 // for infile

using namespace std;

#define cls   system("cls")
#define pause system("pause")

typedef double _PCTIMER_T();
void prompt();
void bubbleSort(int nums[], int size);

void main()
{
	int nums[500],
		size = 500;

	string inFileName;

	pctimer_t t1,
			  t2;

	prompt();

	t1 = pctimer();

	bubbleSort(nums, size);

	t2 = pctimer();

	cout << "The time to sort the array in bubble sort method was: " << t2 - t1 << "seconds\n\n"
		 << endl;
}


void bubbleSort(int nums[], int size)
{
	ifstream inFile;

	string inFileName;

	bool swapped;

	int i,
		j;

	for(i = 0; i < size; i++)
	{
		for(j = 0; j < size - i - 1; j++)
			if(nums[j] > nums[j + 1])
			{
			   swap(nums[j], nums[j + 1]);
			   swapped = true;
			}
		if (swapped)
			swapped = false;
		else
			break;
	}

	inFile.open(inFileName.c_str());
}

void prompt()
{
	ifstream inFile;
	string inFileName;

	cout << "Enter the FQN of the file: " << endl;
	cin >> inFileName;

	inFile.open(inFileName.c_str()); 
	if(!inFile.is_open()) 
	{
		cerr << "cannot open input file: " << inFileName << "\n";
		pause;
		cls;
		exit(5);
	}
}

>string inFileName;
This is an empty string. If you call c_str() on it, you'll get an empty C-style string, which only has one character, the '\0'.

>inFile.open(inFileName.c_str());
This calls c_str() on the empty string. Your compiler doesn't like that.

Fortunately, you can remove both of those lines because neither inFile nor inFileName are used in your bubbleSort function.

ahh thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.