Banfa 597 Posting Pro Featured Poster

Youve missed to top of your code

  1. The easiest way to put a repeat into a game once it is already working is to move the whole game into a function, then add your loop that askes the user if they want to repeat in main and call the function. A do ... while loop works well for this since you always want to play the first game.

  2. I'm slightly surprised that the loop at line 31 doesn't just do into an infinite loop when you guess a letter that is in the word. Line 34 - 36 actually replace a single correctly guessed letter in the word and are only done once for each letter guessed.

You code could use a proper refactor it is messy and confusing. I can tell this because the lines

    //Prompt the user to guess a character in the word.
    cout << "(Guess) Enter a letter in word " << word << " > ";
    cin >> letter;

Appear 3 times in your code. This should not be required, this program can easil be written with the guess request in only 1 place (I would say just inside the do ... while loop. The only other loop required would be to look for multiple occurances of the same letter that was guessed.

Banfa 597 Posting Pro Featured Poster

Your method, BankAccount::NewBankAccount seems to be strangely named and you haven't included the code for it.

I would more normally expect to see this sort of polymorpic behaviour implemented through a base class pointer

BankAccount* acct = NULL;

if (TypeToCreate == Normal)
{
    acct = new BankAccount(name);
}
else if (TypeToCreate == Limited)
{
    acct = new LimitedAccount(name);
}
else if (TypeToCreate == Overdraft)
{
    acct = new OverDraftAccount(name);
}
else
{
    // error type unknown
}

if (acct != NULL)
{
    // use or store account
}

If you need to use functionality specific to the account type (i.e. for Limited or OverDraft account) then either dynamic_cast to the account type required or use the Visitor Pattern to access it.

You sound like you are trying to create a BankAccount and then convert it to a LimitedAccount which is not a good way to set about doing this.

Banfa 597 Posting Pro Featured Poster

In both cases you have the following problems

  1. scanf("%d ", &shift); the space after the %d is superfluous and could cause issues, remove it.

  2. scanf will likely leave the '\n' (newline) after the number in the input stream which means in both cases the first character read by getchar will be a '\n' ending your program. Better to treat '\n' as just another non-translateable character and use a sentinel character, say '~' as the EOT (end of text) marker.

  3. You state that all non letter characters will be passed through untranslated, i.e. 0-9, ' ' etc, however what your code does in all cases is not output them at all, you need an else at the end of your if ... else if.

To my mind you orignal code is better than your revised code. Your revised code has multiple putchar statements, you are mixing logic and output. In the original code the decision is made what needs outputing, then it is output. it splits the operation into 2 distinct steps which is good because it then makes changing either step more easy to do independantly of the other step.

Banfa 597 Posting Pro Featured Poster

"Why i can compile the program ?" this is a strange question, you can compile the program because you have a computer with a compiler on it ???

I suggest you try asking the question you actually want answered, are you getting compiler errors? Then post them.

You have an extra ; at line 19 that effectively prevents your for loop doing what it should, remove it.

Line 36 is missing a while(...);

Banfa 597 Posting Pro Featured Poster

You could simplify this by only putting the calculation of the monthly reward in the switch statement, i.e.

    double reward;

    /* printf/scanf statements to get membership and monthly purchase */

    switch(membershiptype)
    {
        /* Cases to calculate the monthly reward and store in reward */
    }

    /* printf statements to output final messages and the achieved monthly reward */

Effectively separate out the logic from the user interface. This is alwaysa good thing to learn to do because in real life the UI often changes while the logic stays the same so you don't want your logic code mixed up with your UI code.

Banfa 597 Posting Pro Featured Poster

Remember that strtok (which frankly I would avoid using if possible) works by overwriting the delimiters with '\0', it is not deleting anything. It returns pointers into the original string.

So when you call

char *token;
token = strtok("greetings pe()ple", "()");

it scans forward until it finds the '(', replaces it with '\0' and returns a pointer to the 'g' giving a string of "greetings pe". If you then call strtok again it

token = strtok(NULL, "()");

It scans past the ')' to the end of the string, finding no more delimiters it returns a pointer to "ple". At the end of this your original string now contains

"greetings pe\0)ple"

Nothing has been deleted but the '(' has been overwritten.

Banfa 597 Posting Pro Featured Poster

Firstly there is no need to use capitals that is generally consider rude as it is normally taken to equate to shouting.

Posting the code will make easier for us to help.

Posting a copy and past of the actual compiler output will make easier for us to help.

I guessing you have a character in your code that is illegal in the context used, that could be because you have used an illegal character or it could be that what you have is legal but there is something missing earlier in the code. From the supplied data all I can tell you to do is carefully check you file and ensure it is all syntactically correct.

Borland C++ 5.02 is now 17 years out of date and predates the first C++ standard, I suggest if you can you use a more up to date standard compliant compiler.

Banfa 597 Posting Pro Featured Poster

What will or won't be copied is entirely determined by what operator = does, not by whether or not the implementation of array uses pointers.

Yes I agree with everything you've been saying given the assumption that what is being attempted is copying from a class instance to another class instance via a void*, but that is a very strange use for a void* when a bytearray* would work better.

So I am not sure that is what the OP is trying to do and I wonder if they are trying to copy data out of a class instance and into a byte array because of this strange use of a void*. In this case the whole pointer cast and assignment will not work regardless of class implementation because what is required is something like std::copy

std:vector<unsigned char> dataclass(10, 3);
unsigned char rawBuffer[10];

// Copy data in vector into raw memory buffer
std::copy(dataclass.begin(), dataclass.end() rawBuffer);

Which comes back to the issue that the actual problem has not been well explained, what is the purpose of "assigning" the class to a void pointer?

Banfa 597 Posting Pro Featured Poster

Line 171 is redundent, it repeats line 168.

The problem is line 169

  temp->previous = new_node->previous;

You are inserting the new node after temp so temp->previous should not change. new_node is ... well new, and so its previous pointer is set to NULL, you overwrite a value that does not need to be changed with NULL.

You actually need to alter the previous pointer of the now after temp in the orignal list to point at new_node.

Line 37 is strange, you set data to NULL but data is not a pointer it is an anmial and you can't assign NULL to it.

You have no polymorphism because of how you have declared your list

list<animal> * list1 = new list<animal>();

Instead of a list of base class pointers animal* which could point to all sorts of polymorphic animal derivertives you are actually storing an actua animal class that means anything you put in the list will be truncated to an animal rather than maintaining its full class.

You would be able to see what I meant if you fixed you list and output the contents.

Banfa 597 Posting Pro Featured Poster

That completely depends on what operator = does. If the OP's array class acts at all like a standard container class, assigning one array to another should make the assigned-to array have a pointer to newly allocated memory that contains a copy of the other array's contents.

Yes but the point I was trying to make is that if what the OP is trying to do is copy the data out of the bytearray class into a raw memory buffer then

*((bytearray*) ptr ) = temp; 

is not going to do it, it will copy the contents of the class into the raw memory buffer, rightly as you point out with the assignment operator, and if the class contents is not actually the array data, because for example it uses a pointer to allocated memory, then what gets stored in the rawdata buffer is not the array contained by bytearray at all.

I went down this route because I see no other reason for using a void*, if you wanted to copy into a bytearray why not use bytearray*?

I don't know if my reasoning for why this is being done like this is correct but if it is then the line of code

*((bytearray*) ptr ) = temp; 

is ver unlikely to work even if the implementation of bytearray is completely correct for the very reason that the assignment operator is being used rather than specifically copying data out of bytearray and into …

Banfa 597 Posting Pro Featured Poster

It rather depends on what the function is supposed to do, the function you have written assumes that the calling code has cast a bytearray to a void pointer to pass into the function (a somewhat pointless exercise in C++) liek this

bytearry data;

function((void*)&data);

The bytearray create in func (your first one) is copied into the external bytearray. However the only really good reason to use void* in C++ is when calling old legacy C interfaces and that makes be wonder if you were trying to copy data out of a bytearray into a memory buffer which is then used in some mannor, like this

void* data = (void*)malloc(50);

function(data);

If this is what you are trying then what you have written wont necessarily work, it rather depends on the implementation of bytearray, whether it holds the actual data to an internal pointer to the data because in the second case your memory buffer would end up containing the pointer rather than the data.

The problem is that you have not explained what you are doing in very much detail so it is hard to work out what the correct answer is.

Banfa 597 Posting Pro Featured Poster

That is your choice but it seems fairly general, I would just make it a whole separate class.

Banfa 597 Posting Pro Featured Poster

The problem stems from the %c format specifier which reads a character and since newline is a character it can be read by %c. All other format specifiers, %s, %d etc use white space as field delimiters and so the white space, including newlines is automatically consumed.

What happens is on the first read the newline after the number read by %d is left in the input string but is then skipped in the second read to find the first non-white space character for the %s

First loop read R, XXXX
File stream contains \nR DISNEY\n
Second loop skip \n (it is white space), read R DISNEY

Although of course now you are reading a number so the input data must have changed but you get the idea.

Banfa 597 Posting Pro Featured Poster

Looks like you are compiling very old legancy c code and like you are using a c compiler.

C does not support function overloading so basically this error is saying you declared the function create_timer one way and now it has come accross another declaration that is different, has a different parameter list.

How to fix? There is no easy fix, time.h is declaring the POSIX defined function timer_create you have code that declares a different function with the same name. Unless there are some preprocessor flags that will change the contents of time.h or the code you are compiling you are going to need to rename the code in the C file you are trying to compile, or possibly exclude it entirely and see if what your compiling can work without it.

Banfa 597 Posting Pro Featured Poster

There are all sorts of algorithms you can use, the problem with sorting a singley linked list is in removing an item from the centre of the list requires that you have a pointer to the the item before the item you want to remove (because it has a pointer that needs updating) and that is not easy to come by unless you have remebered it as you scan down the list.

For a short list an easy way to do this is just to remove items from the front of your original list and build them into a new list but putting the in the right order as you build the list rather than just always adding at the beginning or end. This is not too hard but it is also not the most efficient sorting algorithm so you wouldn't want to do it for a very large list.

You could do a bubble sort, this just compares adjacent items and if they are in the wrong order swaps them and keeps going until there is nothing to swap. If you are going to do this given the simplicity of your data (a single int) rather than swapping the items in the list you could swap the values in the items

if (current->data > current->next->data)
{
    int temp = current->data;
    current->data = current->next->data;
    current->next->data = temp;
}

Although some might consider this cheating and it doesn't really scale well to more complex data types.

Swapping items is …

Banfa 597 Posting Pro Featured Poster

You probably forgot the definition of Person, or forgot to include the Person.h header file.

Banfa 597 Posting Pro Featured Poster

This is because fgets effective consumes the newline for you and fscanf doesn't

I assume your file is

R XXXX
R DISNEY

Remember at the end of each line is a newline character so the file can be presented by the character stream

R XXXX\nR DISNEY\n

This is an important point, a newline character starting a new line is just a visual clue for the dumb humans using the computer, to the program itself it is just another character value (or 2) in the file stream that in some circumstances it puts a little bit of special meaning into (like fgets) but it really isn't that remarkable.

With fscanf you read a character then a white space delimited string so

First loop read R, XXXX (skip the space)
File stream contains \nR DISNEY\n
Second loop read \n (it is a character), R (it is a string)
File stream contains DISNEY\n
Third loop read ' ' (it is a character), DISNEY

When you use the fgets to read followed by sscanf you read line by line, the new line gets parsed by the sscanf because it is always just white space at the end of the current line.

First loop parse R XXXX\n to R and XXXX ignore \n
Second loop parse R DISNEY\n to R and DISNEY ignore \n

Using fgets provides 2 advantages

  1. More control, you know you have exactly 1 line the are no extra characters interfering with your …
Banfa 597 Posting Pro Featured Poster

I think that rather begs the question why would you want to create a package that either doesn't have a sender or doesn't have a receiver? Almost by definition a package should have both.

Building on what @rubberman provided you can easily do this just by using the default constructor of package and then only setting the sender or receiver, you don't have to try and do everything with a constructor.

On a side note I thing a good addition to @rubberman Person class would be a method to indicate if the person was valid. Since you can clearly default construct a package without any people you would probably want to know that both the sender and receiver where valid before trying to dispatch the package. You will have to decide what criteria defines a valid Person, just name filled in or just name and zip or everything for example.

You need to create a copy constructor and assignment operator (they should always be created together) if you envisage a situation in the code where you will need to construct a new Package (or Person) from an existing one. However the code as written has the advantage that since it contains no allocated data it does not require an explicit deep copy (a constructor/assignment that allocates new memory) and therefore the default copy constructor and assignment operator provided by the compiler should work fine.

However you might still want to explicitly do it, our company coding standard states that you …

Banfa 597 Posting Pro Featured Poster

Wrapper classes don't have to encapsulate another class, they can for instance encapsulte a legacy C interface or in your case a propriatory interface to some board.

If you are trying to implement the same wrapper interface for 2 different boards it is worth using an interface class so that the majority of you code just deals with the common interface.

class Interface
{
public:
  virtual ~Interface(){} // Virtual destructor for class designed as a base

  virtual int initialiseBoard() = 0;

  virtual int setRegister(int register, unsigned value) = 0;
}

class Fona : public Interface
{
public:
  Fona();

  virtual ~Fona();

  virtual int initialiseBoard();

  virtual int setRegister(int register, unsigned value);
}

int Fona::initialiseBoard()
{
  int result = 0;
  // Code to send initialisation parameters and commands to the board

  return result;
}

All you code can deal with the Interface class via references and pointers you instantiate the classes you need (possibly using a factory class or function).

However having seen the 2 boards you want to interface to, a GSM module and an FM radio it seems to me that their interfaces may have little in common but I haven't actually checked the data sheets.

gadgets1010 commented: If they have little in common, does that make this task very difficult or does it make it impossible? +0
Banfa 597 Posting Pro Featured Poster

For the main to work you would have to change the output lines to

cout << v1 <<" * "<< v2<< " = " << dot(v1, v2) << endl;

All of your methods pass by value, Vector2D does not contain 2 much data, 2 doubles and a bool so about 20 bytes but I would say it is beginning to get to the point where passing in const references would not hurt

double dot(Vector2D v, Vector2D w);

becomes

double dot(const Vector2D& v, const Vector2D& w);

with the way you have already implemented operator*

Vector2D operator*(Vector2D v, Vector2D w);

there is no way for you to overload the operator so that it produces a dot product because your method dot and operator* have the same parameter list. I suspect you were not supposed to implement operator* as a multiplication in the first place.

Banfa 597 Posting Pro Featured Poster

It is iterative because you can make as many exchanges as you like so for example starting with 1 9000g block you swap for 4500g, 3000g and 2250g blocks but then you can continue and swap the 4500g for 2250g, 1500g and 1125g blocks further increasing the value (2250+1500+1125 = 4875) and I can keep on swaping until I get to block sizes that are not worth swaping because they are too small hence I iteratively swap the blocks until reach an optimal size, remember the aim of the program is to calculate the maximum value I can attain for my original block.

The method you suggest only performs 1 swap so 9000g becomes 4500g, 3000g and 2250g giving a value of 9750 which is greater than the original 9000 but by continue swapping you can get a final value of 14620 which is significantly higher than either the start value or your calculated value.

In your actual code you never initialise m to anything before using it which is actually undefined behaviour.

Banfa 597 Posting Pro Featured Poster

If you exchange a block of m grams, you get three blocks of weight m/2, m/3 and m/4 grams each.

Is the important part of this question because 1/2 + 1/3 + 1/4 = 13/12 or more than you started with. However, fractions are rounded down, so for example a 24g block can be exchanged for a 12g, 8g and 6g block or 26g in total, an increase in value but a 5g block would be exchanged for 2g, 1g and 1g block or 4g in total a reduction in value.

This means that for the 5g block you would be better off exchanging it for money because 5g = 5 units of cash.

The question is to print the largest value of cash you can obtain for a given block size so for any given block size you need to work out if you loose money by splitting it into smalller blocks blocks and if so change it for cash. You probably want to keep doing this in an iterative process until you nolonger have any blocks only cash and at that point see how much cash you have.

The values 2 and 1,000,000,000 denote the minimum and maximum value you have to deal with as input to your program.

Banfa 597 Posting Pro Featured Poster

At line 19 and 20 you are using the symbols sys_nerr and sys_errlist but this is the first time the compiler has seen them, you have not previously declared them anywhere hence they are "undeclared".

You may have missed out a header file in which they are declared or you may have failed to write their declarations.

Banfa 597 Posting Pro Featured Poster

Your while loop, lines 17 - 26 puts your list together in reverse order, so the first numbered entered appears at the end of the list and the last number entered at the beginning, because you keep adding to the front by altering first. That's OK it is the simplest way to add to a single linked list if you don't care about the order of items in the list (or you want that order) as you might use for managing blocks of memory.

However if you want the list in the reverse order you need to be a bit clever about how you add items to the list. Typically a pointer is maintained to the last member of the list as well as the first and that way you can add a new member to the end of the list by just creating a new node and assigning it to last->next and then assign last to the new last node last = last->next. You also have to handle the edge case of the list being empty where you need to assign both the first and last pointers.

Banfa 597 Posting Pro Featured Poster

The way you've approached the algorithm is flawed. It is doubling the result each time, instead of multiplying it by the base number.

That's not quite true, the line doing the calculation is

 return numDigits*(place(numDigits,exp-1));

That's not doubling that is multiplying by the base (the base in this case being named numDigits).

I agree that raising number of digits to power rather than base to number of digits is strange but it is what the question says is required.

Banfa 597 Posting Pro Featured Poster

At line 24 remove the this because that makes the current class (window/widget) the parent but line 36 tries to make the _verticalLayout the parent too and causes the warning since the parent is already set.

Banfa 597 Posting Pro Featured Poster

Actually your code compiles fine, but it fails to link because in binTree.h you declare all the functions in the template class binTree but you don't define any of them. You need to write the imlementation of all those functions.

Banfa 597 Posting Pro Featured Poster

Your condition at line 45 if(exp >= 10) is wrong. Remeber that any number raise to the power of 0 is 1 but this is not what is embodied by your condition.

I would also say you are missing a newline or 2 at line 43.

Also storing exp as a long double in the function place could lead to error because all floating point types are only approximations so 10.0 minus 1.0 10 times may not equal exactly 0.0; however since you originally request the exponent as an integer, line 37, you can safely hold it as an integer for the whole calculation avoiding the floating point issue altogether.

Banfa 597 Posting Pro Featured Poster

libpcap is just the library that enables you to capture the network traffic, if the data you are capturing is not a protocol that WireShark already handles then your only options are to extract the data by hand (i.e. copy it off the screen or export to a text format and do the maths required yourself) or write a plug-in for WireShark to handle your unknown protocol which is not an entirely trivial task.

Banfa 597 Posting Pro Featured Poster

Everything works fine if I use #include "Error_Handler.h" in the code.

But

#include "ErrorHandler.h"

Typo in post or actual mistake?

That said it would still help to see a few lines of code round the line producing the error. Also is this the first compiler error you get, if not what is the first one, remember it is always possible that subsequent compiler errors after the first one are just an artifact of the first error reported.

Banfa 597 Posting Pro Featured Poster

This definately does not sound like the observer pattern (which is OK you never said that you were implementing that) since in the observer pattern the publisher (thing adding the message) has no knowledge of who is subscribing where as in your implementation it is paramount that they do.

I think that your subscribers need to have and identifier (be that a number or a string) because otherwise the publisher needs to somehow know the pointer to the subscriber, with an id the ids used by the program can be static global data or even exist in a configuration file.

But then you also have the problem of only storing 1 string because in a multi-threaded program it seems entirely possible that you may get multiple updates happening on Blackboard in different threads it would be possible for a Blackboard::post call in 1 thread to overwrite the message from the previous Blackboard::post call before the subscribers had picked it up.

You then need to store a message for every possible id, luckily C++ has a structure for this ... std::map.

However ignoring the multi-threaded case and sticking with a single string you have 2 options both use a modified post in the form of void Blackboard::post(std::string msg, Identifier toWhom); (note I am generalising the type of the identifier as Identifier you can still make this either a string or an integer)

1: Blackboard::post checks all the identities of its registered subscribers and only calls Subscriber::update on subscribers with a matching …

Banfa 597 Posting Pro Featured Poster

Your are deleting memory that is in use and you are mixing heap and stack memory and trying to delete stack memory, all in Sport::add.

You are using dynamic arrays of pointers so

Line 194 - 201: Allocate a new array of pointers (temp) copy pointers from array to temp

resulting in the arrays temp and array containing the same pointers but

Line 206 - 211: Delete the data pointed to by the pointers in array, delete array. You delete the data pointed to by the pointers in array but this is also the data pointed to by the pointers in temp. You have deleted data you are not yet finished with. You only need to delete array not what the pointers in array are pointing to.

then

Line 216 - 221: Copy the now invalid pointers in temp back to a newly allocated array

Line 222: Store a pointer to fresh which is on the stack and won't exist past the end of the method in array where later it will be treated as if it was on the heap and deleted.

and finally

Line 225 - 230: Delete the data pointed to by the pointers in temp this has already been deleted at line 206 - 211 so is a double delete.

At line 206-211 you delete data you have not yet finished using
At line 222 you store a stack pointer that will become invalid in an array you are assuming contains heap pointers
At …

Banfa 597 Posting Pro Featured Poster

I supose it may be the compiler and it certainly wont hurt to use a more up to date one but from my recollection I would have thought the Visual C++ 6 compiler should be capable of handling this code.

Banfa 597 Posting Pro Featured Poster

Your code seems to work fine for me when I compiled it with MinGW after I commented out #include "stdAfx.h".

Note that "Visual Basic C++ 6.0" is not a thing did you mean Visual C++ 6 which is rather old?

Banfa 597 Posting Pro Featured Poster

Subscriber::update, Subscriber::alter and Subscriber::noMessage all sound like they are doing the same thing and should be a single method.

For the operation you want to perform you are missing an operation in Blackboard because there is no way to get the current message, you need a BlackBoard::getMessage method.

Blackboard::notify and Blackboard::notifyAll sound like they should not be public methods of Blackboard. You post data to the Blackboard, it decides if anyone needs notifying, so the notify operations should not be something that are available for anyone they are internal operations.

Finally Blackboard::post has an odd prototype, in the Observer pattern the idea us that the publisher does not need to have any knowledge of who is receiving the data, they just publish (post) the data and the publishing object (Blackboard in this case) takes care of informing all subscribers. In this case Blackboard::post would have the prototype Blackboard::post(const std::string& msg)

Banfa 597 Posting Pro Featured Poster

You will need code in your main function and you will need your other functions to return either a status or a sentinal value that can be tested in main to determin if the execution path needs to return to the start of the switch statement.

Banfa 597 Posting Pro Featured Poster

Like I said if you are having trouble with templates then start by working out how a normal function would work, then templatise it.

So a function to print all entries in an array of any type given a pointer to the array and it's size, i.e. display_array becomes a function to print all entries in an array of int.

That is not too hard it is something like

void display_array( const int array[], const int size)
{
    for(int ix=0; ix < size; ix++)
    {
      std::cout << array[ix] << std::endl;
    }
}

Now templateise it, the array can be of any type so int array becomes T array, not much else changes; we assume that ostreamn::operator<< is defined for type T.

template<typename T>
void display_array( const T array[], const int size)
{
    for(int ix=0; ix < size; ix++)
    {
      std::cout << array[ix] << std::endl;
    }
}

I have simply changed those types that are to be part of the template definition to use the template parameter. I leave arrayLocation as an exercise for you to do yourself noting it probably relies on there being an operator== (and operator>) for the type used in the template.

Once you have created your template functions then you can initially test them using int types which should produce the same results as your original non-templated code and then you can define your alternate types (structure) that you wish to use.

Banfa 597 Posting Pro Featured Poster

Line 54

template<class T >
T  display_array(T ItemType[10] , T const int arraySize)

This is invalid. Remeber that the compiler will substitude the type T in the template into the code provided in order to instatiate the function for you. You can do this by hand and should get the valid code. In this code if T is int (as is the case in main) then the code becomes

int  display_array(int ItemType[10] , int const int arraySize)

Hopefully it is obvious that the type of the 2nd parameter int const int is not valid. It should presumably be const int which would be removing the T from the beginning of the parameter type.

Line 59

cout << array1 << ' ' << arraySize;

array1 is not visible here, it is a local variable to main. Even if you changed it to ItemType, which is a valid local variable, you are just going to get an address (because ItemType is a pointer) or alternitively a compiler warning/error.

Line 66

 template<class T>


 streuct Item Type{
     int length;
     int array1[arraySize];
 }

You appear to be trying to declare struct ItemType, ignoring spelling an spaces you appear to be confusing type names and variable names. In various places in this code you use ItemType as a type name (here and top of main) or a variable name template at line 54. Also you have declared this structure as a template dependent on type T and …

Banfa 597 Posting Pro Featured Poster

This issue here is because MyTimerProc is static, and must be static to be passed to the C WIN API Function SetTimer it has no this pointer i.e. it does not require or have an instance of the object. It is in effect a normal function. However tmrprocedure is member data and can only be accessed through a this pointer, i.e. it needs to have an instance of the object.

Every single one of your instanticated Timer classes will be calling the same callback, MyTimerProc, with no differentiation between what which instance of the class is being refered to.

However you do know which timer is caused the call because the timer id (which you store in class data, is passed to the callback.

The only way I can think of to get round this problem is to use a collection (say a map) to store the relationship between timer id and class instance in your Start function (e.g. std::map<TimerId, Timer*). Then you can use this map in MyTimerProc to look up the class instance from the timer id and call the right tmrprocedure.

Obviously since you will be using the collection in MyTimerProc you need to be able to do it without needing a class instance so your collection would need to be static member data which also means that if this is a multithreaded program that you will need to make access to the collect thread safe.

Banfa 597 Posting Pro Featured Poster

My compiler does not support scanf_s (it's a Microsoft extension I think) but if I change your scanf_s to a scanf call it works as expected.

So I looked up scanf_s in the Microsoft help and it said

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if you are reading a string, the buffer size for that string is passed as follows:

and then gave this example of reading a char

char c;
scanf_s("%c", &c, 1);

Next time try reading the manual.

ddanbe commented: Great! +15
Banfa 597 Posting Pro Featured Poster

Describe what you mean by not working?

You already had everything in a loop, I think there is no need to add a second one.

Banfa 597 Posting Pro Featured Poster

I doubt it is compiling.

Line 4: You have used assignment = when you meant to use compare ==. '36' is not a valid character and it is not a number, if it compiled I expect it had the value 51 which is the ASCII value for the character 3. If 36 was the ASCII value of space then you want to use the value not a character/string of the value i.e. if (hit == 36) however 36 is actually the ASCII value for $ 32 is the ASCII value of space. A much better solution is to use the character ' ' not ASCII the value at all; this will protect you if your code happens to be ported to a system that uses an execution character set that is not ASCII (although that would be rather unusual these days) and is better practice in general giving if (hit == ' ')

Line 6/9: either use cout or use printf (preferably cout) but not both, they are not guaranteed to interoperate.

Finally your loop has no pauses, I imagin it will run so quickly you will not see anything happen. Also it has no end condition so once it's going you are stuck.

Banfa 597 Posting Pro Featured Poster

You don't really need to re-write the code so it works with HTTP, HTTP uses TCP/IP sockets. HTTP is from the application layer of the protocol stack and TCP from the Transport layer and IP from the Internet layer; respectively layers 4,3 and 2 of the communication stack. That is HTTP defines the data that should be sent to a webserver using a TCP/IP socket.

Generally most webservers (certainly all those publically accessable on the internet) use port 80, this port is reserved for that purpose.

All you have to do is send a valid command on the right port to the right server, for example

GET /index.php HTTP/1.1
Host: www.daniweb.com

Would retrieve the main index page from DaniWeb. Not that every line ends in a carridge-return, new-line pair '\r\n' in C parlance and that the request must end with a blank line (Which is not showing up even though I have marked it as code).

The frist line is the command, subsequent lines are headers and the blank line finishes the command. you will get a result a bit like

HTTP/1.1 200 OK
Date: Mon, 23 May 2014 17:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 10 May 2014 17:11:55 GMT
ETag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Accept-Ranges: bytes
Connection: close

<html>
<head>
  <title>DaniWeb blah blah blah</title>
</head>
<body>
  all the stuff that makes up DaniWeb
</body>
</html>

The first line is the result, again followed by headers terminated with a blank line and …

Banfa 597 Posting Pro Featured Poster

I assume you have actually checked out the Twitter API?

Banfa 597 Posting Pro Featured Poster

Actually since this is C and the structures are also in an array you could just use qsort to sort them.

Banfa 597 Posting Pro Featured Poster

It's not exactly good practice to leave out what type malloc needs to allocate.

Rather than this:

score[i].FirstName = malloc(name_size + 1);

I would write this:

score[i].FirstName = (char *)malloc((name_size + 1) * sizeof(char));

I can not agree with this. Firstly malloc does not allocate types, it allocates raw data it is the program that decides how to treat that data. malloc handles this correctly by returning void*, ie data without a type, which the C compiler, again correctly, can convert to any other pointer type. If your compiler is producing errors then you most likely are compiling your code with a C++ compiler. In this case you should either get a differenet compiler/add the switch that tells the compiler this is C or alternitively use new; note that I do understand that what you should do and what you can do are sometimes different things.

However what is not good practice is putting in unnecessary casts. A cast switches off all the type checking a compiler might do, it is like waving a big flag at the compiler saying "You don't need to bother checking any of this because I know exactly what I am doing". In my experience more often than not the compiler knows what it is doing better then most programmers particularly new ones and I have seen too many problems caused by programmers who didn't know any better fixing a problem with a cast.

Banfa 597 Posting Pro Featured Poster

And it also looks like you are using pointers in the members of your structures when it looks like you did not intend to.

struct the_struct
{
 char *FirstName[20];
 char *LastName[32];
 int  *Score[20];
};

FirstName and LastName are an arrays of pointers to char, did you in fact mean to use an array of char, since that is what you use to hold a string in C?
Score is an array of pointers to int, did you actually mean an array of int to hold several scores for a single person?

Banfa 597 Posting Pro Featured Poster

If for some reason you can't or don't want to implement operator<, for example it is already implemented to do something else you can always use the 2nd form of std::sort

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Which takes 3 parameters the 3rd one being a comparison functor (or function) that embodies your sort algorithm (i.e. compares 2 objects and decideds which is smaller).

Banfa 597 Posting Pro Featured Poster

You have a few small problems but the one causing the error you are seeing is in Queue<ItemType>::Enqueue, if rear is NULL you set front but you do not set rear so the next time you enter this function rear is still NULL and you just overwrite front, incidentally causing a memory leak, and not actually setting up a queue.

Other errors

size is not set in the copy constructor

In Queue<ItemType>::Dequeue you don't set x

You have multiple unnecessary ; after every function definition.

And finally: in Queue<ItemType>::Dequeue you decrement size regardless of if you actually deleted an item from the queue which means if called on an empty queue it goes negative and gets out of step with the actual queue size.

Banfa 597 Posting Pro Featured Poster

That is because you only have 1 buffer, buff. At line 13 all you do is store a pointer to buff in the array token so all entries in token end up pointing an the same buffer, buff and when you print out your tokens they all contain the same thing, the last thing you entered, because they are literally pointing at the same object.

change the declaration of token to char token[20][20]; and strcpy from buff into token (or just get rid of buff altogether.