thelamb 163 Posting Pro in Training

You can create an std::map, that maps the strings to integral values. Then call map::find and use the returned iterator to retrieve the key and use that in the switch switch statement.

thelamb 163 Posting Pro in Training

Ok, then there probably are some left-overs in the input buffer, as I see now from the code you posted you are actually requesting input from the user.

Have a look at this sticky:
http://www.daniweb.com/forums/thread90228.html

It explains nicely how you can flush the input buffer, after that both methods (cin.get() at the end or running with ctrl+f5) should work.

thelamb 163 Posting Pro in Training

I may have misunderstood your question.

Do you want to be able to debug your program? I deduced from your last reply that you think 'ctrl+f5' will allow you to debug the code inside visual studio, and that you do not want this.

So let's summarize:
1) 'F5' - Run under debugger, window will close when returning from main
2) 'Ctrl+F5' - Do NOT run under debugger, window will not close when returning from main.

So if you want to be able to step through your code, you have to press F5 and insert a breakpoint somewhere. Your program will then stop on the breakpoint and hand control over to visual studio, so you can inspect variables that are in scope etc.

If you just want to run the program, but keep the window open when it finishes execution, press 'ctrl+5' or add something like std::cin.get(); before the return 0;

I hope it's clear now, if not please be very specific what behaviour you're looking for.

thelamb 163 Posting Pro in Training

I'm quite sure it does not run under a debugger when you press ctrl+F5. What makes you think that it does?

Try adding a breakpoint on a line of code that surely will be executed. It should not break if you 'run without debugging'

thelamb 163 Posting Pro in Training

When you return from main, your program is terminated, this also means that the console window will close.

In Visual studio, if you execute your program by pressing 'Ctrl + F5' it will not immediately close, instead it will wait for you to press a key.

In other situations, you can prevent termination by trying to read from stdin (and thus waiting for user input).

thelamb 163 Posting Pro in Training
thelamb 163 Posting Pro in Training

With very large data (a website) you can't really assume that recv is able to 'fetch' all of the data in one call. Try setting the socket to non blocking, and call recv in a loop until it returns 0 (socked is gracefully closed).

something like (without error handling):

iResult = 0;
do {
iResult = recv(client_sock, recv_buf + iResult, sizeof(recv_buf) - iResult, 0);
} while( iResult != 0 );

Also, are you sure the page you're trying to load is less than 10240?

Note that if you set the socket to non blocking, SOCKET_ERROR doesn't always mean a fatal error, you will have to check if WSAGetLastError wass WSAEWOULDBLOCK, in that case it just means there is no data ready to be received.

Edit:
You'll need to do some more things to make non blocking sockets work nicely, like using select or something simmilar - but let's focus on this problem first.

thelamb 163 Posting Pro in Training

To your first question:
cin by default 'splits' on the space character. So say:

string test;
cin >> test;

If the user enters "one two", test will be equal to "one".

[EDIT]
My original 'easy' way was plain dirty as thankfully pointed out by mike_2000_17, use his solution as an easy way out (I'm not sure how getline slipped my mind ;))
[/EDIT]

The nice way, however is to create a class 'Student' and overload the >> operator, but for now the above should do fine.


To your second question, in C++ to create arrays that don't have a fixed length on compile time you need to create (and delete!) them dynamically.

Look up on google for 'new' and 'delete':

string* studentName = new string[ numberOfStudents ];
// use studenetName here
delete[] studentName; // delete studentName
thelamb 163 Posting Pro in Training

"locks up a lot when downloading certain pages"

That's not a very good description of your problem... can you be more specific?
Does it freeze completely? If so, does it every time if you try page X? Did you run under a debugger to see where it is freezing? etc.

thelamb 163 Posting Pro in Training

Dude, you're a hoot. I officially invite you to Daniweb's IRC channel so that you can entertain us in realtime chat.

Well, this thread has had one use... I didn't know there was a DaniWeb IRC!

thelamb 163 Posting Pro in Training

Ok, then there actually is an easier way. You can overload operator>> in your fraction class.

e.g.

struct fraction
{
	int num, denom;
};
istream &operator >> (istream &in, fraction &f)
{
	in >> f.num;
	in.ignore(1,'/');
	in >> f.denom;
	return in;
}

Hope that helps

thelamb 163 Posting Pro in Training

How will treating the / as whitespace help you? I assume that you want to convert the user's input (e.g. 3/4) to a float (0.75) in your code?

So you'll have to split the input string on the '/' character (and do something sensible if the '/' is not found), convert both halves to some numerical type, and then 'calculate' the fraction.

thelamb 163 Posting Pro in Training

Easy to test? Create two threads that run a function with a static variable, print the value of the variable and then increment it.
Make sure the first thread is finished before starting the second.

thelamb 163 Posting Pro in Training

Uhm, what about removing the while( true ) on line 68 ??

thelamb 163 Posting Pro in Training

I don't think it's possible to give a good answer to that question. It will depend on the person reading your resume whether it's good or bad.

In my eyes, it shows initiative and the ability to set something up from nothing. Also, if there are more people working on it it shows that you can work in a team (eventhough it is not the same as in a company).

So if your contribution to the project is significant, I would mention it.

thelamb 163 Posting Pro in Training

It depends on the instruction, there are shortcuts for longer commands, which in assembly are 2 or 3 'words' but in binary are just 1 opcode.

As an example:
xor eax,eax
Is smaller than
mov eax, 0

Also, take a relative jump for example - it's 5 bytes, but a far jump is 7 (32-bit).

thelamb 163 Posting Pro in Training

break is the right way.. but keep in mind that you can only break from 1 loop.

for( int i = 0; ...; ...) {
     for( int j = 0; ...; ... ) {
         if( something ) break;
         // You don't need to explicitly continue here, since it is the end of the j loop.. but if something else is coming, and you want to start from the beginning of j write:
         continue;
     }
}
thelamb 163 Posting Pro in Training

Google gets me to this page:
http://oprofile.sourceforge.net/docs/intel-P4-events.php

I don't know how good it is.

I don't know how closed source your algorithm is, but I have VS2010 through uni and I wouldn't mind running the profile for you, if it's not too much of a hassle to get it to compile.

thelamb 163 Posting Pro in Training

I didn't say 1.3 is not so long, but a difference of 30% on 0.2 seconds is not that much.

However since you ran it many times it is meaningful - so you can ignore that comment ;).

What gonefission posted is likely to be part of the 'problem'.
A cache miss won't generate a page fault though, so you won't see an increase of page faults with increasing cache misses.


Also, this may be silly, but just checking... you allocate the memory before starting the algorithm - do you then hand over a pointer to this block of memory? Or is it copied somewhere.

thelamb 163 Posting Pro in Training

1.3 times as long doesn't seem very significant, how long does the 'fastest' algorithm take? How many times did you run both algorithms.

Can you show us the exact difference in code between both approaches, your explanation is a little unclear to me.

thelamb 163 Posting Pro in Training

It will be a dangling pointer, so just be allocated for nothing.
Because you overwrote ptr, you can't call delete[] anymore on the original ptr.

thelamb 163 Posting Pro in Training

The result doesn´t need to be a number with a decimal.

How do you know? 0_o

Anyway, we need more info:
How is this function called? (e.g. how are arrayStart and arrayEnd initialized).

Could it be a problem of an edge case?

for (tempPtr = arrayStart; tempPtr != arrayEnd+1; ++tempPtr)

Do a print here, and call the function with, say, 3 elements... check if there are 3 iterations - or are there 4?

Clarify 'gives exception'

thelamb 163 Posting Pro in Training

Then my answer still stands, m_item will be initialized based on its type ;).

If DataType is an int, m_item will be initialized as if it were written 'int m_item;'.

So if you call insert, where DataType is deduced as an integer, the compiler will emit code that treats m_item exactly as if it were an integer.

Then your question boils down to "What is an uninitalized integer initialized to"

int m_item;

Same as if you could call insert where DataType is of class A, the compiler will emit code that treats m_item as of type A, and your question is "What is an uninitialized variable of type A initialized to?"

A m_item;
Akill10 commented: Thanks +1
thelamb 163 Posting Pro in Training

You mean what value m_item will hold? That depends on the type of DataType.
It's default constructor will be called, without any parameters. If DataType is an int, it will behave just like 'int m_item;' would. Not sure if this is exactly your question though (just had an exam from 18:30 -> 22:00... I might not think clearly any more ;)).

I'm not actually sure what you're trying to do with your simplified version.

m_array[p_index] = m_item;
m_array[p_index] = p_item;

// Same as:
m_array[p_index] = p_item; // ??
thelamb 163 Posting Pro in Training

Google for 'cin' and C++.

Also, you're now using the C function for output (printf), in C++ we generally use 'cout'.

thelamb 163 Posting Pro in Training

In C++ NULL is defined as 0, so it won't make a difference.
(The use of NULL is even discouraged by some, including Stroustrup).

thelamb 163 Posting Pro in Training

Why not just:

class A {
 static int currentID;
 int uid;

 public:
     A() : uid( currentID++ ) { }
};

int A::currentID = 0;

And you can just create the objects and put them in a vector, as long as you don't have multiple threads creating objects of A they will have a unique ID.

If this is not what you want, then it's still not very clear to me what the goal is.

thelamb 163 Posting Pro in Training

What do you want to achieve with this:

B () : x(y) {  
      B <t-1> (); // What do you think happens on this line?
}

The design currently makes very little sense.

Does the assignment specifically state 'array', if so then you probably are not allowed to use a vector.

thelamb 163 Posting Pro in Training

It will now work, but in C++ it's not advised to use malloc/free. It's better to use new and delete.

(Of course you should also check the return value of CreateFile before using the file handles in the second loop)

thelamb 163 Posting Pro in Training
HANDLE *fhndl = (HANDLE*)malloc(n);
delete [] fhndl

What is wrong here? (apart from the missing ; behind fhndl)

thelamb 163 Posting Pro in Training

Did you do a trace over or a trace into?
trace into should log everything.

To only log function calls you probably have to write a plugin for OllyDbg, I don't know of any existing way.

thelamb 163 Posting Pro in Training
thelamb 163 Posting Pro in Training

Consider this:

HANDLE fHandle;

for( int i = 0; i < 7; ++i )
{
   fHandle = CreateFile( ... );
}

close( fHandle );

First time CreateFile is called, let's assume that fHandle will be set to 0x00000001. The next time it's set to 0x00000002, until 0x0000007.

After the loop, you're closing only the last handle (0x00000007), what happens to the other 6?
(They will be closed by Windows automatically when your program terminates, but it is very bad practice to leave them open.
So close the file handle when you're done with it, inside the for loop.

(In future please use code tags).

thelamb 163 Posting Pro in Training

There are several ways, look at std::sort:

http://www.cplusplus.com/reference/algorithm/sort/

By default it uses the operator<, so you can create a class that inherits from vector and overload the operator<. Or, more easily create a 'compare' function and hand it over to sort.

After you've sorted the vectors, you can iterate through them and set the .id field (you can't really do this while sorting unless you implement your own sorting algorithm - which probably wont function as well as std::sort).

thelamb 163 Posting Pro in Training

It ran on a server, so no GUI.
In fact, I've never programmed a GUI application in my life

thelamb 163 Posting Pro in Training

Well, as you can imagine... if it were that easy it would've been built into IDA.

I do know of a 'run trace' in OllyDbg, if you use the latest version it is even quite fast (in 1.0 it's dead slow, especially if you 'trace into').
It can be a pain to analyze the trace log though.
Maybe it's also in IDA, but the debugger isn't very mature so I doubt the run trace would be better than in Olly.

Other than that... the only way I can think of is running the program in some virtualized environment which emulates all instructions and thus can log all calls, but I don't know if this is easily available anywhere.

thelamb 163 Posting Pro in Training

The platform will not matter much if your goal is to learn C++.
The way I learned is simply by doing a larger project, and learning on the way (I had the luxury that someone else proposed a fairly big project).

The project I started with ran for about 2 years after which I completely rewrote it (was a server application).

Unfortunately, my creativity for project-ideas is non-existing - so come up with something (it doesnt even need to be useful) and just start, really think about the design, create half of it and then realize that it's totally wrong and do it better. Repeat.

Of course if you have questions about your design, post it here ;)

You might also want to get a good book (there are links in a sticky post). I like to have 'The C++ Programming Language' nearby as a reference.

thelamb 163 Posting Pro in Training

Your method makes little sense to me...

By comparing these functions I will see witch functions are different, and the first function that differs between the 2 opcodes,

Why would different functions be called if you send different 'opcodes'? They will both fail the same verification function(from your story, I guess that is what you're looking for).

It handles a packet and responses to it.

This is doing 'something', the data has to come from somewhere.

So why don't you break ok recv/recvfrom, then you are probably already close to your function. Next you put an 'access' breakpoint on the buffer that was passed to recv/recvfrom, so the next time the program reads/writes to this buffer (your opcodes) it breaks again.

Apologies if I completely misunderstand your problem

thelamb 163 Posting Pro in Training

That's not an easy thing to do.

Do you have any other information what this function supposedly does? Does it show a message box? Does it print something? Does it write a file? etc.

thelamb 163 Posting Pro in Training

The program compiles, but doesn't do anything like I want it to

Help us out here:
- What do you want it to do?
- What does it do instead?

thelamb 163 Posting Pro in Training

compile with symbols, execute `ulimit -c 1024` (will create a core dump when your program segfaults), then use gdb on the core dump, and tell it where your source files are so that it can tell you where the error originates.

How can you manage a 14k line program without having debugging skills?

thelamb 163 Posting Pro in Training

>> is simply a function call, which can fail.

So check if the cin >> userGuess succeeded or not:

if( ! (cin >> userGuess) )
   // not an integer, do something

(the () around cin >> userGuess are important.)

thelamb 163 Posting Pro in Training

A templated function doesn't _need_ to be defined in the header file, however it is usually easier to do so (see http://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file ).

Inline functions however need to be in the header file, this has to do with 'translation units'. If you have an inline declaration in 'inline.h' and it's implementation in inline.cpp and then include inline.h in A.h, the definition of the inlined function won't be available to A.h because it is in a different translation unit.

Also note, when you define a function inside a class body it is automatically inlined:

struct test {
 void returnSomething() { return 1; }    // Implicitly inlined
 inline void definedElseWhereInHeader(); // Explicitly inlined
};

// This must still be in the header file
void test::definedElseWhereInHeader() { 
  // do something
}
Annettest commented: Thanks a lot theLamb. Very helpful answer. +2
thelamb 163 Posting Pro in Training

You're not actually modifying the inTables[q].Items in your first loop.

for each (AoE2Wide::DrsItem item in inTables[q].Items)
{
   // item is a copy of what's at inTables[q].Items, modifying item will leave the item in inTables[q].Items in tact
}
thelamb 163 Posting Pro in Training

You declared the EnterP and EnterU functions as part of a class, however in the implementation you forgot the classname:: infront of the function name.

I haven't really looked at your 'remove' problem yet.. are you sure all instances to the stream object are closed before you call remove?

thelamb 163 Posting Pro in Training

Can you show us the exact code you have now?

thelamb 163 Posting Pro in Training

Read carefully when the eofbit is set (http://www.cplusplus.com/reference/iostream/istream/read/):
"eofbit | The end of the source of characters is reached before n characters have been read. This also sets failbit."

In the 4th call, all 'n' bytes are read before the end of the source is reached, eventhough the file pointer is now at the end of the file. In the 5th call you request n bytes again, but the end is immediately reached so the eofbit is set.

Hope that makes sense

thelamb 163 Posting Pro in Training

That's because of your while loop, think about it:

while( ! Dat.eof() ) {
 Dat.read( ... );
 // Use what was read
}

Let's say there are 4 valid data structures in the file.
So Dat.read() will be called 4 times successfully, but it will not set the eof() flag after the 4th call yet.
So your loop has an extra iteration, where Dat.read() is called and all it does is set the eof flag. When this happens you still treat it as if it's a 5th data entry.

So you need to check Dat.eof() after every Dat.read call, before using the result of Dat.read

thelamb 163 Posting Pro in Training

Sorry, I didn't really look at your code and misunderstood your question.

Something goes very wrong in your read data function:

while(!Dat.eof()) {
	Dat.read((char *)newptr, sizeof(qu));

Before the while loop you allocate one new qu object, but in the while loop you constantly overwrite whats written at this pointer.

So you'll need to allocate a new qu* every time you read it from the file, and add that new pointer to your linked list.

I dont know if that solves your problem.

thelamb 163 Posting Pro in Training

I dont think you fully understand what templates are for, let's look at the name setter for example:

template <typename N>
N setName(N n)
{  
   Name = n;
   return n; 
}

Why do you need a template function here? Name is always a string.
So this would do just as well:

void setName(string n)
{  
   Name = n;
   //return n;  // Why do you need to return n? The caller already knows it
}

But now, every time you call setName( someString ); a copy is made of 'someString', so even better:

void setName( const string& n )
{
    Name = n;
}

Even this can be further 'optimized' if you call setName like: setName( "My New name" );
But let's not get too much into optimization 'details'

Read up on when and how to use templates ;)