thelamb 163 Posting Pro in Training

Well that's all nice and dandy, but no one is going to read through your source if you just ignore the rules this board has (How did you even miss the fact that you should use code tags?).

"I need a code in string as much as possible but if not, anything will do as long as it solves my problem."

People will help you if you ask a concrete problem, where are you stuck, what have you tried yourself. If you expect source code you're in the wrong place: ask concrete questions, try out what people tell you, if it works, celebrate your new knowledge, else try again until you got it.

thelamb 163 Posting Pro in Training

Ask yourself questions (before asking google). And be precise!
You did not read my last post fully, as the error I mentioned is still in. Don't just do something, hit compile and google for the errors you are getting, think structurally what you want to achieve, break the problem up in pieces and step by step take them down, making changes precisely and everywhere where you need to (declaration and definition should match).

After fixing what I mentioned, think about what input is.

thelamb 163 Posting Pro in Training

Encryption is not a matter of "read a book and you can write a quality encryption scheme" there are extensive subjects to the matter.

It is always dangerous as a non-encryption-expert to create a encryption scheme, just use an existing encryption library, and don't say it will be less secure because it is a popular library so a lot of people have already attempted/succeeded to crack it.

thelamb 163 Posting Pro in Training

Ok you are on the right track empror.

The output you see is something like:
1234 etc. ?

Note that you have the "cout" inside the for loop, so it will be executed in every iteration of the for loop.
If you put it behind the } of the for loop, it will only output once.
If you want the next output to start on a new line, you need to write << "\n" behind the 'cout << count'

Secondly, your for loop goes from 0 up to _and including_ 30. and the variable str is an array of 30 elements.

So it starts:
str[0] // 1st element
str[1] // 2nd element
str[28] // 29th element
str[29] // 30th element
str[30] // error! str has no element 31 !

thelamb 163 Posting Pro in Training

Think about what you are doing. What you have right now makes no sense. You are creating a char array called 'a', then you say while a == 'a' you increase the count. But the a variable is never filled, it is just random data.

Think again what you do in real life. You first look at the first character of str, you look if it is an 'a', then you go to the second character of str, and look if it is an 'a'. You do that until you reach the end of str.

thelamb 163 Posting Pro in Training

Whenever you call a function, you do not need to include the argument's datatypes. For instance:

// Function that returns nothing and takes 1 int as argument
void myFunction( int myInt )
{
    cout << "Your number: " << myInt << "\n";
}

int main()
{
    int myInt = 5;
    myFunction( myInt );
}

Secondly, the declaration and definition of your Print_Results are different( on line 11, the ofstream argument is missing ).

thelamb 163 Posting Pro in Training

By using your brain and thinking about the problem rather than asking how it is done.

As with any problem, try to break it up into smaller, easier pieces.
There is a string and you want to count the number of a's... in the real world, how would you do this if you had written the word on a piece of paper? Would you take a picture of the piece of paper and analyse the handwriting? Or would you simple read through the word, increasing a counter every time you see an a?

thelamb 163 Posting Pro in Training

Be more precise in your question.. else you are unlikely to get homework help. This encrypt function you pasted(next time use code tags please), is it exactly like in your code? If so... the compiller errors should be obvious enough...

So, tell us what errors you are getting, why do _you_ think the errors appear? Then we can help you.

thelamb 163 Posting Pro in Training

Check your spelling of initRendering function.
Also, the drawscene function is spelled with a lower-case s while you try to call it with an upper-case S

thelamb 163 Posting Pro in Training

But you could solve that, if you know the size of the biggest type in the structure right?

Let's say 32 bit int is the largest type and you are reading a 16bit int.
Instead of advancing the pointer by 'size of a 16 bit int' you would advance it by 'size of a 32 bit int' to get to the next element.


Edit:

In this case it might just be easiest to fire up a debugger and look at your structure in memory.. see what the p1 is pointing to and what it points to after increasing it. That way you should be able to see what's going wrong.

thelamb 163 Posting Pro in Training

Can you explain why you want to "hide" the structure from the SetInt and SetString functions? This is a really dirty trick and should hardly ever be necessary in C++...

thelamb 163 Posting Pro in Training

That's not a question, that's "Can someone please do my homework". Which unfortunately no one is going to do for you.

So, as with many problems, the first step is: break it up in parts.
-> Do you know how to open a file?
-> Do you know how to read from a file?
-> Do you know how to use arrays?
-> etc.

Maybe even those questions you can sub-divide. Then take what you think should be the first step, and try to implement it. If you don't know how... google, if you tried something but it doesn't work.. post here and give us your thoughts (how do YOU think it should by done, and why do YOU think it fails)... then someone will help you out.

Fbody commented: right on :) +1
Ancient Dragon commented: Yes, that's what I meant too. +27
thelamb 163 Posting Pro in Training

Sorry, i pasted the copy i edited. should be while .

Even that will not compile...
Fixing this while loop and running the program gives me the 'expected'
600 output, so I'm not sure what you're doing...(It is impossible to get 400 with CD as input).

Enter the roman numerals to convert
CD
The value in Roman numerals is 600

Anyway, I think the point of this code that you found is that it only converts one Roman numeral at a time and add it to the total... or the guy who wrote this doesn't know how Roman numerals work (as there are only additions in the code.. how would it ever work?)

thelamb 163 Posting Pro in Training

You have already done it...

Look at how you declare your 'request':

Request *request=new Request();

So 'request' is now allocated on the heap.. means you are responsible for cleaning it up (delete request; somewhere in the code).

Then, you say.. ok I want 'request' to point to a LoginRequest.

LoginRequest loginRequest;
request=&loginRequest;

So you are overwriting the previous pointer to 'new Request();' with a pointer to a local variable loginRequest.
Since loginRequest is local, it will be destroyed when it goes out of scope. Now your 'request' pointer points to garbage and there is no way that you can delete the original new Request()(remember you overwrote the pointer), so you have yourself a memory leak.

So I dont really understand why you do the first "new Request();".. doesnt this make more sense:

Request *request=new LoginRequest(); // I guess LoginRequests inherits from Request?
request->tcpMessage=line.toUtf8();
request->decodeFromTcpMessage(); 
if(request->requestType==REQUEST_LOGIN)
{
    requests.enqueue(request);
} else  // Request is not a LOGIN_REQUEST
{ 
     delete request;
}

// When you destroy the requests.. dont forget to delete the pointers.

P.S. think about your naming convention...
Is using 'requests' and 'request' a good idea? The names look very simmilar and just introduce confusion.

metdos commented: Good Point +1
thelamb 163 Posting Pro in Training

I used boost in a project where I needed regex. It is distributed as a library though so you need to link to it.

More info here:
http://www.boost.org/doc/libs/release/libs/regex/

With this library, you can put ( ) around the part that you want to return as a match. So e.g.:

Message:
"The price is: 40.00"
And your regex is:
".* ([0-9]{1,}\.[0-9]{1,})"

The boost library will store the price for you.
The example is just for illustration.. if there is a mistake in it, please don't cry a river :P

thelamb 163 Posting Pro in Training

You can create a map of "Specific Object" pointers.
E.g.:

class CMemoryMap
{
public:
  void newObj( const CObject& myObject )
  {
       m_mMemoryMap.insert( std::pair<CObject*, some_data>( &myObject, some_data ) );
  }
  void delObj( const CObject& myObject )
  {
        map_iterator  = m_mMemoryMap.find( myObject );
        m_mMemoryMap.erase( map_iterator );
  }
  private:
       map<CObject*, some_data> m_mMemoryMap;
};

Of course this code won't compile and a lot more sanity checking should be done.. but its just an idea.

So you actually store a pointer, not the actual object.

thelamb 163 Posting Pro in Training

First of all, use CODE TAGS when you post code on this forum (not sure how you missed that with all of the information about it jumping in your eyes).

You got the general idea of a mutex right... but there are some things you should note.
First of all: You are not closing the handle to the mutex.
I am not 100% sure if Windows does this clean up for you, but nevertheless you should not rely on this. So close the handle when you don't need it anymore.

It is also a good idea to initialize the mutex HANDLE, eventhough creating the mutex is the first thing you application does... initializing variables should become a habit.

Lastly, a mutex is a kernel object, this means it can be shared between processes. You can make a mutex local, so accesible to your process only, by specifying NULL as name.
Since you are not sharing this mutex with another process this could be a good idea.
But that doesn't take away the fact that a mutex is a kernel object.
A slightly more effecient way of dealing with synchronization in Windows is by using CriticalSections (google it), these are usermode objects so slightly faster.


Edit: Also... don't use system( "PAUSE" )... read the stickies on these forum, they give you some nice tips that every c++ coder should know.

thelamb 163 Posting Pro in Training

No one is going to do your homework coder2009, but nice try.

If you have a problem with these questions.. think further and ask more specific what exactly you don't understand. For example in the first question.. do you know what a static cast is? etc.

If you show that you tried yourself first then people will be happy to help.

thelamb 163 Posting Pro in Training

How about if...else if ?

if ( Discriminant < 0)
{
	cout << "There are NO real roots" << endl;
}else if ( Discriminant > 0 ) // if Discriminant is not smaller than 0
{
	cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
	cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}
thelamb 163 Posting Pro in Training

You can't get root2 to output.... are you sure that you are entering values for A, B, C etc. so that Discriminant is 0? As when it's > 0 only Root1 is printed of course.

Also have a look at how you check if 'A' is non-zero. What happens if I enter a non-zero value twice in a row? Validating user-input is a very important aspect of coding, and a lot of people think lightly about it but they absolutely shouldn't.

Another note on your code... You want to check if Discriminant is < 0, == 0 or > 0... so you start with checking < 0.
If this is true ( Discriminant < 0 ), what can you say about the 'Discriminant > 0' and 'Discriminant == 0' checks? Are they useful?

thelamb 163 Posting Pro in Training

Think about what an LPCWSTRING is...
The example 2 lines what you give does not really make sense to do, it may help if you give some more information on what you want to do.
Most importantly: Where does 'name' come from

thelamb 163 Posting Pro in Training

Nothing is wrong with you PC, but it seems it would be wise for you to take a step back, and learn more general C practices before jumping into multithreaded programming.

But here is the answer anyway:
pthread is a library, which means that the code is in some external DLL. Now your compiler complains that you call pthread_create but it has no idea where it can find this function(including the header file will only tell the compiler what the function looks like, it wont tell it where to find it).

So you need to hand over the flag: '-lpthread' to your compiler. So:
'gcc p1.c -lpthread'

But I highly suggest that you learn more about how to compile / link. It was also a problem in our OS class that for most students this was their first encounter with C but they were expected to write an application already.

thelamb 163 Posting Pro in Training

Well you hit the nail on the head when we mentioned that problems can arise in multithreaded programming.

the silly function is first executed, in a separate thread and then immediately again in the main thread.
cout works by filling a buffer, so while the separate thread is filling the buffer, the main thread starts filling it too.
So if you want to print it sequentially, you need to 'protect' the cout call.

EDIT:

Do you mean the 'Operating System Concepts' book by Silberschatz and Galvin? As this is what we use in our OS class.

thelamb 163 Posting Pro in Training

Aren't there any examples in your OS-course book? I had an OS course(outside of my normal curriculum as I study mechanical engineering) too this semester, which included a 'lab' where we were given the task to write a multi threaded assignment.
I would suggest using the 'pthread' library, especially in Linux it is commonly used as the p stands for POSIX.
You can add threads easily to your application but as Dragon said.. the fun part starts when they share resources.

I don't think it is necessary to explain pthread, there are tons of good tutorials on the web.

thelamb 163 Posting Pro in Training

It's not possible to 'convert' a string to a class with the same name as the value of the string.

What you can do is:
- Use a switch case, it is much more friendly as 100 if statements.
- Create a hash table where you can look up the string, and 'get' a pointer to the correct class back.

thelamb 163 Posting Pro in Training

A lot of people use it, for example to 'pause' the application, or they clear the screen on startup which most of the time gives the problem I stated in my last post.

So all I say is that mostly the use of system() is wrong, except in special cases.

thelamb 163 Posting Pro in Training

Application that use system() to clear the screen are a downright pain in the ass, excuse my language.

As a user of the application, I might have some 'important' information on the console before running the application, if the application 'clears' the screen with system() all of this is gone.

William Hemsworth commented: Yup yup. +5
Ancient Dragon commented: good point :) +25
thelamb 163 Posting Pro in Training

This is a typical answer that can be found through 2 seconds of googling.

http://www.cplusplus.com/reference/clibrary/cstdlib/system/

Though I generally think calling system() is a bad design, the overhead is massive and mostly there are much better alternatives.

thelamb 163 Posting Pro in Training

There is a thread about clearing buffers as a sticky somewhere.

Think about the two differences in C++ with regard to 'assigning a value' and 'comparing two values'.

In the do..while loop you want to compare DoItAgain with 'true'.
But in your code you assign DoItAgain the value 'true' (By saying 'DoItAgain = true' ).
Now how could you fix this and instead compare DoItAgain and true?


Also, in future please use code tags when you are posting part of your source code.. it makes it much more readable for us.

thelamb 163 Posting Pro in Training

I don't think this is possible, as malloc is just allocating a block of memory for you, it won't actually put anything in.
Could you tell us why you really have to do this? It's often better to ask how to do something than 'why doesn't my implementation work'.

thelamb 163 Posting Pro in Training

<replied before reading... /ignore>

thelamb 163 Posting Pro in Training

You have to delete it at a point where you can guarantee that after the delete you won't access it anymore.
This can be at the end of an application, in the destructor of a class or just somewhere in a function that prints the variable and is then done with it.

thelamb 163 Posting Pro in Training

So now you expect us to read the whole code line by line, or compile it ourselves to find the error that you could just post in 2 seconds?

thelamb 163 Posting Pro in Training

I would say, give it a go in your 'real' application.
There is however one wrong thing though...

You allocate a new dynamic array of chars, but when you do the cleanup(delete) you are not telling the compiler that Result is in fact an array. The correct way to clean up is: delete[] Result

thelamb 163 Posting Pro in Training

Yes I do, but you will learn more from it by trying yourself.

Just remember that you cant change anything to the string.

thelamb 163 Posting Pro in Training

Ok I'm sorry I didn't realize you had no control over it.
But still, it probably is a problem them of constness. You are 'forcing' to convert a const char* to a char* which is never a good idea.

So try to think of a way to get a 'real' char*

thelamb 163 Posting Pro in Training

Instead of answering your question(which is not possible with the code you provided) let's take back a step and ask yourself why do you hand over a char*? If, for some obscure reason, it is needed then Dave Sinkula's question should be answered. What .c_str() does, is 'convert' the string to a 'const char*' where 'const' is the key, meaning that you can not edit it.

thelamb 163 Posting Pro in Training

I think what necrolin says is the way to go. Don't underestimate the fact that while you are writing games you are still learning C++, you won't stop learning when you start applying it ( as a matter of fact I think you'll never stop learning C++ ).

But of course there is a certain entry-level, it is probably a bad idea to _start_ with coding a game ( unless it is a simple console-based game ). But you say you know some C++ so I'd say go for it.

I don't want to put you off your goal - but becoming a game developer in 4 years is definitely not easy. I suppose you want to be working as a game developer - but there will be very few companies who will hire you unless you actually have a degree to show that you are capable. And besides that, being a game engine developer is definitely not all about programming, there is a massive amount of math involved so you need to be very comfortable with that kind of math if you want to be working on serious projects.

thelamb 163 Posting Pro in Training

Did you even try anything yourself to find the solution? The question sure looks like a homework assignment, which is not something you'll get help for here unless you show that you've actually tried and frankly I don't believe that you have.

So do the following:
Search for 'for loop' or something similar and try to understand what it does... then if you don't understand - post here some code of what _you_ have point out exactly which part _you_ don't understand. Then we will help you

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

Never mind, issue has been resolved by using a different design.

thelamb 163 Posting Pro in Training

Hello all,

I have a VB.net application and a C++ DLL.
The C++ DLL has an exported function that gets called by the VB.net application.

The C++ exported function is as following:

STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    TR::Log( "Starting ( %s )", m_fullPath.c_str() );

    // Start the child process.
     if( ! CreateProcess( const_cast<LPCSTR>(m_fullPath.c_str()), // filename of the process
        NULL,		// command line arguments if present
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        FALSE,            // Set handle inheritance to FALSE.
        DETACHED_PROCESS, // debug the process, but not spawned off childs
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
        )
    {
        TR::Log( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    m_hProcess      = pi.hProcess;

    m_dwProcessID   = pi.dwProcessId;
    TR::Log( "PID %i", m_dwProcessID );

The logfile contains:

Starting ( C:\testMe.exe )
PID 1356

So it would seems everything is working(as we have a PID and CreateProcess does not fail). But the application's window does not show up(tried both with Console application and normal application) and the PID is not visible in the task manager.
Besides the application I run a driver that registered a 'Process Create Notify'. This notification routine gets called with the same PID.

I added a messagebox in the DLL's main: The DLL is initialized before CreateProcess is …

thelamb 163 Posting Pro in Training

Learn about scopes.

You have more than 1 line of code 'inside' your while loop, but how does the compiler know which code is inside the while loop?

You do not tell the compiler how many lines are inside the while loop, so the compiler assumes that there is only 1 line, directly following the while loop. So that is the cout << "something";

To tell the compiler that there are more lines, surround them with brackets ( { and } ) like:

while( expression )
{
    cout << "something";
    cout << "Something else";
}

This will print "something" and "something else" in a while loop. Without the brackets it would only print "something"

thelamb 163 Posting Pro in Training

Ok, only the allocation should be enough - after this you know that it is properly allocated so it won't throw a bad_alloc anymore.

So you could do:

applicant *applicants;
try {
applicants = new applicant[num];
} catch ( whatever )
{
}
reference the applicants here

There really is no advantage to it, but I like to keep my try catch blocks small.

thelamb 163 Posting Pro in Training

Did you fix it the way you stated? If so ... it is not necessary to put the entire code in a try-catch block.

thelamb 163 Posting Pro in Training

Get ready to slap yourself in the face dgr.

You're defining the applicants variable as a local variable (a variable is only valid between the brackets ( { } ) where it is defined in.

try
{
applicant *applicants = new applicant[num];
}

After the } the applicants isn't valid anymore.

Also note that you are using 'new', so you are responsible for cleaning up the memory when applicants isn't needed anymore with 'delete[] applicants;' (it's an array so you need delete[] and not delete).

Hope that helped.

thelamb 163 Posting Pro in Training

A vector has a lot of functions associated with it that you can use, including removing elements (erase):
http://www.cplusplus.com/reference/stl/vector/

You should read up on vector iterators, they are really handy to use. Google is your friend here.

Of course if you don't understand something about it you can always ask ;)

thelamb 163 Posting Pro in Training
const int i=5;
building office(i);

You define the max level yourself before you hand it over to office - so you have full control over it. If later you want the user of your application to enter a maxlevel then you should check their input before creating the building object, and if they enter <= 0 you give them an error message and don't create the building.

thelamb 163 Posting Pro in Training

I've just read through your code a little(not detailed) and saw this:

building::building(int l)
{
       SetMaxLevel(l);
}

But because this is the constructor of building there is a faster way to initialize variables, you can do it like this:

building::building( int l ) :
     MaxLevel( l )

With the same method you can do the elevator constructor... but I will leave it up to you to find out how to do it ;).

Btw: the advantage of initializing your variables like this is that the compiler doesn't need to copy the 'l' variable, but you can find the exact details on google.

Also it is good practice to prefix private variables with m_ (so for example m_MaxLevel). Of course this is personal preference but later when you are using MaxLevel you will always see that it is private then.

thelamb 163 Posting Pro in Training

And how exactly do you expect us to help you?

I doubt anyone is going through your source(I could be blind but I don't even see any) to pinpoint for you where your bottlenecks are.

There exist 'code profilers' that can show you what section of your code is taking the most time, that is where you want to start looking to optimize your code. If you have found it and you are not sure what you can do to make it run faster you can post that and other relevant sections and we will have a look with you. But mostly in the beginning you can gain some performance with pure logic.