Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think writing this heavily object-orientated aspect, in assembly, would probably take the rest of my lifetime...

:eek: No, not the whole blasted program !

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>The nature of this application is that it needs to be extremely fast. So shaving nanoseconds is important to a degree.
If speed is that important then don't use c++, but use C instead. And code in assembly when needed to optimize some time-consuming algorithms.

I don't think virtuals make a program slower, just a little larger. The way I understand them virtuals are little more than function pointers and calling a function through a function pointer is only a couple clock ticks slower than calling the same function directly.

>>Also a very important thing which mars the performance of virtual functions is that they can't be inlined
Although that is a true statement, large functions are never inlined anyway unless using a very stupid compiler.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> Now how do I find the address where in the memory my object is created?
The address stored in the this pointer is the address of the object. Here is an example that illustrates this. Notice that the value displayed in lines 8 and 18 are the same address.

#include <iostream>

using namespace std;
class A
{
public:
	A() {x = 0;}
	int getX() {return this->x;}
	void printThis()
	{
		cout << this << endl;
	}
protected:
	int x;
};

int main()
{
	A a;
	a.printThis();
	cout << "&a = " << &a << endl;
	cin.ignore();

	return 0;
}

>>But damn this basic concept has slipped off my mind when I coded
Don't worry, it will come in time. Practice makes Perfect is one of the golden rules. We were all beginners at some time.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

argv[0] parameter in main contains that info.

int main(int argc, char *argv[])
{
   printf("%s\n", argv[0]);
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>t of all, is it called a This pointer or a This operator?

this is a pointer. An operator is something else.

c;ass pbkects are private by default. If you want to make them public then you need to specify it.

functions need to have a return type, c++ does not permit default return types.

class A
{
private:
    int x;
public:
    int fn() { return this->x; }
};

>> am also recieving a warning like 'x' is assigned a value that is never used
Because you have to initialize variables to something (usually 0) before they can be used. If not initialized they will just contain some random value. Class variables are normally initialized in the class constructor, like this:

class A
{
private:
    int x;
public:
    A() {x = 0;}
    int fn() { return this->x; }
};

>> I am trying to find out the address of the memory which is held by the THIS pointer
The actual address is not important to most programmers, but it is the address of the class's instance. In the main() function you posted it would be the address of object a.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To convert a single character to an int just simply subtract '0' from its ascii value

char c = '1';
int num = c - '0';

When converting several characters to int you have to multiply the previous value by 10 to make room for the new digit.
Below num is an integer and digits is a character array that contains all the digits. This code snipped would be placed inside a loop.

num = (num * 10) + digits[i] - '0';
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

check the files in the project director with Windows Explorer -- that inData file is not where you think it is.

Second problem: the individual cases in the while statements need to end with break statements, otherwise without it the code will just keep executing all the cases below it.

Third: how do you expect that while loop to terminate? Isn't it supposed to process each line of the inData.txt file? Should be coded like this:

while( infile >> xactCode >> xactAmt )
{
  // blaba
}

[edit] Ok, I was typing while you were posting corrected information. Since you've already fixed it just ignore my comments above. [/edit]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>By the way the point of the HW are classes

Well, so much for your teacher's reputation, it just went into the gutter :eek: But we should be discussing your HW, not your teacher.

Had you left the pointers there it would have made your program just a small tad more complex but a whole lot safer because you could have allocated memory of the correct size for the strings, something like this:

PCourant->nom = new char[line.size()+1];
 strcpy (PCourant->nom, line.c_str());

Don't forget to delete[] to memory when done with it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>so now i give to the other function{funcA} a simple integer that contains only a number from 0 to 23....

that is not a time_t variables that is initialized from time() function. If all you are going to do is pass an integer between 0 and 23 then the check is trivial.

bool foo(int hourl)
{
   if( hour >= 5 && hour <= 5)
       return true;
   return false;
}

>>PS can anyone explain this line
The mod operator returns the remainder after division. In this case it will return the number of seconds that has passed since midnight. The time() function returns the number of seconds since 1 Jan 1970 (I think that's the date). There are 86,400 seconds in a 24 hour day and the remainder of any number divided by 86400 is the number of seconds in the current day.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )
If you want to post code, please at least post valid code.

>>time_t seconds_elapsed_today = epoch_time % NUM_SEC_IN_A_DAY ;
Good idea :) , which I had thought of that too.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Seems to me the concept here is to learn to process c-strings, because they are necessary in C++.

earlyriser, stop using any and all functions that have a definition of string and use only char* functions. Help us save Joe's sanity. :)

He's already said his instructor says he must use char arrays. He should be writing a pure C program, not a C++ program basterdised with C. If that is what his instructor is teaching then the instructor needs to take a few more programming courses.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

both of the answers don't solve my problem because basically what i wanted to do is pass the current time {in time_t format} in a function, that will check {among other things} if the time is between 5 and 6 am....

you must not have comprehended what we posted because the suggestions will do just what you want.

int foo(time_t time_to_check)
{
    struct tm *tm = localtime(time_to_check);
    const time_t five_am = 18000;
    const time_t six_am = 21600;
    // convert current time into seconds since midnight
    time_t now = ( (tm->tm_hour-1) * 60 * 60) 
                         + ((tm->tm_min-1) * 60)
                         + tm_tm_sec;
   if( now >= five_am && now <= six_am)
   {

          cout << "Great time";
   }

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I didn't know that C is better for char arrays than C++.

That's not true, character arrays can be handled equally by either language.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

5:00 am is (60*60*5) (60 seconds in an minute times 60 minutes in an hour) = 18000 seconds after midnight. 6:00 am is (60*60*6) = 21600 seconds. After getting local time, convert the hour, minutes and seconds to an integer ((hour-1) * 60 + (minute-1) * 60 + seconds), then check if it is between 18,000 and 21,600.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> strcpy (PCourant->nom, line);

line is a c++ class, strcpy() wants a c style pointer. You have to use std::string's c_str method to get the pointer, like this strcpy (PCourant->nom, line.c_str());

struct Professeur
{
    char nom [20];
    Professeur *suivant;
};

Since you are writing a c++ program you should use c++ std::string class, not character arrays, unless your teacher requires you to use character arrays. Using std::string will make your life a lot easier and eleminate several problems with using strcpy() and other similar standard c string handling functions. The main problem with those functions is they will allow you to scribble all over memory by copying strings beyond the allocation of the destination buffer. Example: copy a 25 character string into that buffer that has only room for 19 characters plus null terminator. After that happens your program will likely crash at some point and you will spend hours trying to find the problem.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Files transferred to MS-Windows from *nix will also have that problem unless the file is run through a translation program. Most FTP programs can correctly translate the file during file transfer operation.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you mean a vector like below? There are probably other ways too, but this is how I do it.

vector< vector< string> > theList;
vector<string>::iterator it;

vector<string>& innerList = theList[0];
it = innerList.begin();
for( ; it != innerList.end(); it++)
{
  // blabla
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what operating system? on *nix just open a pipe to the ps program than parse its output. There are probably other ways to accomplish it too.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

fscanf() does not differentiate between rows. So you will be better off not using that function at all. Instead, I would use fgets() to read one row at a time then parse the string that was read.

char line[80];
char *ptr = 0;
int i = 0;

while( fgets(line, sizeof(line), f) != NULL)
{
   // clear the array
   memset(v,0,sizeof(v));
   i = 0;
   // extract numbers from line string
   ptr = strtok(line," ");
   while( ptr != NULL)
   {
       // insert into array
       v[i] = atoi(ptr);
      ++i;
      // get next number from the string
      ptr = strtok(NULL, " ");
    }
    // now do something with these numbers

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't know about phthon but I know that can be done in C or C++ using sockets. Computers attached to a LAN (Local Area Network) do not require internet service provider but others do.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Again, use pencil & paper to write out the value on each iteration of the loop. If you know how to multiply something by 2 then this should not be difficult.

On the first iteration of the loop the value of i is 4. Then i is incremented to 5, then 6.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

after each if statement, the grid should look like this:

a   b   c   d
5   2   1   3
2   5   1   3
2   1   5   3
2   1   3   5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use paper & pencil to do this problem. Final answer should be

a = 2
b = 1
c = 3
d = 5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can do this: char* ptr = const_cast<char*>(param;) , but that defeats the purpose of using const in the first place. Casting out the const should be avoided whenever possible.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why do you need a toString function? cout will print out the integers quite well cout << tm->tm_hour << ;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>if i add one minute in the struct tm and then call mktime() it will fix all the other members?{
Yes -- that is what normalize means

time() function returns a time_t (normally unsigned integer). If all you want to do is add minutes, then just add (minutes * 60) to the value returned by time(), then call localtime() to get the structure.

Yes, you can do that in two functions if you want.

Here is basically the way you will probably want to implement the algorithm. Your function aa() might get the number of minutes from the keyboard, then pass that value to bb() to display the date/time.

int number_minutes = 1;
time_t today = time(0);
// add 1 minute to the current date/time
today += (60 * number_minutes);

struct tm* tm = localtime(&todlay);
// now just print out the struct tm
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The portable way is to use the functions in time.h. 1. get current time using time() function, which returns the number of seconds since some pre-determined data, normally 1 Jun 1970. '
2. call localtime() to get a pointer to struct tm, which contains each element of date/time as integers.
3. Add the number of minutes to the tm_min element of tm structure.
4. call mktime() to normalize the date/time. This function corrects all tm structure members.
5. using final version of tm structure to print out the results.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

congratulations on meeting the 1,000 posts mark :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Any idea why it is blank on my browser ? (see attached zip file)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you really wrote that program then it should not be all that difficult for you to complete the assignment. Just add two more variables to hold the highest and lowest values. If however you did not write that code, then you are committing plagiarism (claiming something is your work when you know it isn't).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I saw that yesterday too using IE6. And I saw a couple problems (?) just now on both IE7 and FireFox 2.0.0.1. There is some white space (rectangle) in the upper-left corner where an add probably belongs, and in the lower-right, just beneath "Todays Posts" (no posts listed)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have to add a function or class *.c or *.cpp to the library before the compiler will create the .lib file. Just putting the *.h in the project is not sufficient -- there must be some executable code. And you will find the .lib file in <project name/debug or <project name>/release directory.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is this the Adbrite site? I didn't have any problems with it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

fgets() doesn't know the difference between characters, such as 'a', 'b', 'c' ... and numbers such as '1', '2', '3' ... '9'. In the file they are all just text. If you want to treat all those numbers as text, read the file line by line, then just use fgets() the normal way

char line[80]
FILE* fp = fopen("data.inp","r");
while(fgets(line,1,sizeof(line),fp)
{
   // do something
}
fclose(fp);

If you just want to add more lines to that file, then it isn't necessary to read it at all. Open the file for append and start writing -- new lines will be appended to the end of the existing lines.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

maybe this short tutorial will help you. Follow the link near the bottom of the page for other tutorials.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

C++ compilers for MS-Windows operating system can compile either console programs, which normally opens a DOS-like window, or a GUI window like you see with your VB compiler. The reason is that many c++ programs do not need GUI windows because they do things that do not require a GUI or human interaction.

>>Is VB the only way the program will open up in a windows like window?
No, Visual Studio C++ can create them too (so can Borland and many other compilers). When you create a new project you have a choice of either creating a Console application or a Windows application. But windows programs are not as easy to code with C++ as they are with VB. Here is a tutorial to get you started, but you will need a basic understanding of C language.

Colin Mac commented: nice clear help +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is there any particular reason to start the quoted block with a backtick (`) and end with a single quote (') ? .

I had missed that part of your question. I suppose its just for aesthetics.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The quotes make it unambiguous what the user is supposed to type. If you left the quotes out, someone will probably try this

c:>hello to invoke system shutdown <Enter key>

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

there are hundreds of them -- just use google. Here are the ones from Microsoft

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

True, but always writing malloc calls in this form p = malloc ( number * sizeof *p ); will ensure that the scaling for the size of object being allocated will always be correct.

http://c-faq.com/malloc/sizeofchar.html

from your own link

It's never necessary to multiply by sizeof(char), since sizeof(char) is, by definition, exactly 1.

p = malloc ( number * sizeof(char) ); is the same thing as p = malloc ( number * 1 ); which is identical to p = malloc ( number ); . So why bother with the extra typing :eek: and extra reading :eek:

One problem (I know you are aware of this but others may not) with using malloc(), of course, is that when the value of number is positive and greater than the maximum value that can be stored in size_t, which will happen only if sizeof(int) is not the same as sizeof(long). Or when the value of number is a negative signed int, in that case the value actually passed to malloc() may be unpredictable and probably undefined.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

put a break point at the beginning of the function and step through the program one line at a time to see where it breaks. Also check the validity of all pointers -- If a pointer has a value something like "hchchchchc" that is what the compiler initializes them to when compiled for debug -- and is an uninitialized or bad pointer.

And please stop creating a new thread every time you have a new question about the same program. Just post your question(s) to the existing thread, that will bump it to the top of the list.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

sizeof(char) is not necessary because is it guarenteed by standard to be 1.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you might want to add some debugging print statements in some of those functions to see if they are writing beyond the boundries of the allocate memory for those arrays. For example, does loc_x + wd*loc_y exceed the allocation limits of the array?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

since it is a binary file

int numValidRecords = ???;
// write out the file
fwrite(&numValidRecords,sizeof(int),1,fp);
fwrite(&ph,sizeof(ph),1,fp);

// read the file
int numValidRecords = 0;
fread(&numValidRecords,sizeof(int),1,fp);
fread(&ph, sizeof(ph), 1, fp);

or

// write the file
for(int i = 0; i < NumValidRecords; i++)
    fwrite(&ph[i],sizeof(ph[0]),1,fp);

// read the file
int i = 0;
while ( i < 20 && fread(&ph[i], sizeof(ph[0]), 1, fp) > 0)
   ++i;
ankit_the_hawk commented: real help +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

why not just put the number of records in the file followed by all the records. Or just read one record at a time until end-of-file -- you don't need the number beforehand.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>#define M 100 *&^asdf
the compiler does not evaluate macros until used. If you coded something like below your compiler would complain bitterly at you int x = M;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> for(i=1;i<=count;i++)
arrays start with 0, not 1. should be like this: for(i=0; i < count;i++)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
while((ch=getc(f))!=EOF)
 {i++;
  fscanf(f,"%d",&v[i]);
  count++;
 }

The above is reading the file twice -- the byte read by getc() is tossed into the bit bucket.. Better algorithm is

i = 0;
while(i < 10 && fscanf(f,"%d",&v[i]) > 0)
{
  i++;
  count++;
 }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Back to the OP's question -- I doubt if he is using double or tripple buffering, just drawing on the screen as needed, which is why I said it would be too slow to erase the whole screen and redraw it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't know much about game programming, but I would think they actually use two (or more) screens -- the screen(s) being drawn is in the background then just flip screens when its time to display it. That way we don't see the flicker.