Banfa 597 Posting Pro Featured Poster

I'm afraid I'm really not an expert on that module so I can only really wish you luck in experimenting.

Banfa 597 Posting Pro Featured Poster

Read each name into a string and assign to a vector of strings.

Get the size of the vector and create a random number in the range 0 - (size-1). Record it.
Repeat 4 more times throwing away any duplicates of entries already selected.
Output the names corresponding to the 5 numbers that have been generated.

Banfa 597 Posting Pro Featured Poster

The CMUcam2 seems to be explained at http://www.cmucam.org/projects/cmucam2/wiki where a data sheet with serial commands is available.

Banfa 597 Posting Pro Featured Poster

temp provides temporary space in which to perform the sort and should be the same size as the original array. While doing the sort the merge function uses the temp array to store the sorted list until is has complete the merge operation when it copies the sorted list back into the number array.

Using additional temporary storage in this manor is a common implementation of merge sort and would only be a problem where the size of the data set to be merged is beginning to get close to a small fraction of the available storage for the machine.

Banfa 597 Posting Pro Featured Poster

In the block of your if statement simple check the last character received (or all characters received may be) to see if it is an '!' and if so set a flag that causes the while loop to finish.

iByteCount will only be 0 when the connection is closed so unless part of your process is to close the connection once all the data is recieved processing on that condition will not help.

Banfa 597 Posting Pro Featured Poster

Does the program crash if you select option 5 without selecting any other option?

The code you have posted does not contain any obvious errors which means the error is else where. This is not entirely unusual, if some sort of undefined behaviour has been invoked it would be quite normal for the crash to happen in a place in the code completely apart from the location of the error.

A ha, I've spotted the error.

You are passing BankAccounts into menuSelection by value. Assuming that your BankAccounts::~BankAccounts dealloctates all the data and given that BankAccounts does not have a copy constructor and given the recursive way you call menuSelection rather than using a loop an iterating that means that the first time you call menuSelection it copies h. You select a menu option, it performs the function and calls menuSelection again copying h again. When you exit it deletes h deleting the data that it uses but because you have no copy constuctor for BackAccounts that is the same data that all the copies use to. When you exit (option 5) menuSelection and the local copy of h is deleted trying to deleting the internal data which has already been deleted causing the crash.

That it randomly works for option 4 is a random artifact of the undefined behaviour of deleting the same memory twice.

You need to do the following

  1. Pass BackAccounts to menuSelection by reference
  2. Do not call menuSelection recursively, ultimately you will always run out of …
Banfa 597 Posting Pro Featured Poster

In C arrays can't be passed as arguments (or return values directly either) you can only pass pointers. Additionally C does not have 2 (or multi) dimension arrays when you declare int a[10][10] you are not declaring a 2 dimensional array but rather an array of arrays (a subtle but important distinction).

For any array using its name without an index provides a pointer to the first element in the array so for an array of arrays using the name provides a pointer to an array, the type of a is int (*)[10] and that is what you want to pass to your function which means that the prototype needs to be void input(int a[][10], int b, int c) or void input(int (*a)[10], int b, int c); and when you call it you can just pass the name of your array of arrays input(a, m, n);

Banfa 597 Posting Pro Featured Poster

The first line the order of the or'd conditions suggest that you check the data before validating the indexes you use to access the data. You need to validate the indexes first otherwise you could use out of range values.

Again in the loop you calulate xn and yn and use it without validating those values to check they are valid but if x == screen height then x+1 is out of range and if x = 0 x - 1 is out of range, similarly for y.

Banfa 597 Posting Pro Featured Poster

s("\n%s",&book); Because char book[50] is an array and book returns a pointer to the first item in the array. That means that when used in scanf you don't need to take its address as it is already an address so the above line should be s("\n%s",book); In your node structure you need to change the declaration of nbook to be an array as well.

and in

void addbook(nodepointer &head,char book[50])
{
  nodepointer newnode;
  newnode=(nodepointer)malloc(sizeof(struct node));
  newnode->nbook=book[50];
  newnode->next=head;
  head=newnode;
}

At line 5 the [50] is a syntax error you just need to refer to the parameter by name. Also you can't assign an array using =, as I said before at this line you need to make a call to the standard library function strcpy.

Banfa 597 Posting Pro Featured Poster

Every place in your code that you have used char book; only holds a single character but you want to hold a title which is multiple characters so you need not a single character but an array of characters char book[50]; in all those places.

You will need to use %s in scanf instead of %c and you will need to use function like strcpy instead of just using =

Banfa 597 Posting Pro Featured Poster

By catch I mean test to see if *number is 2 or 3.

Banfa 597 Posting Pro Featured Poster

Don't declare lots of individual vectors in Company declare a container of vectors, for example

a vector of vectors:
vector<vector<Employee*> > Location; // Each location has a position

a map of vectors:
map<int, vector<Employee*> > Location; // Each location has an id number

alternative map of vectors:
map<string, vector<Employee*> > Location; // Each location has an name

Then just iterator through or find in the container (map or vector) and call the Employee vector.

void Company::OrganizeLocation(const string& location){
  map<string, vector<Employee*> >::value_type office;

  office = Location.find(location);

  for(unsigned int i=0; i<office.second.size(); i++) {
    office.second[i]->AccessorFunction1();
    office.second[i]->AccessorFunction2();
    office.second[i]->AccessorFunction3();
  }
}

Or something like that.

Banfa 597 Posting Pro Featured Poster

You can only store a single character because in your node structure

struct node
{
 char nbook;
 struct node *next;
};

You only provide space to store a single character. You need an array of characters if you want to store a whole string.

Banfa 597 Posting Pro Featured Poster

You would perhaps do better to specifically catch the values 2 and 3 and return TRUE before the line in green.

The line in green could then check if the number is divisible by 2 and return FALSE if it is.

Numbers divisible by 3 are caught by your loop on the first iteration so no special case is required for them.

If you want to correct the line in green try saying out load the logic you want it to perform and then try reading out load the logic it does perform, you should be able to hear the differences.

Banfa 597 Posting Pro Featured Poster

You are not altering the size in remove.

Resize still refers to m_size

At line 95 you have index— but I think you mean index-- NOTE that is not a minus in what is currently in your source file.

Banfa 597 Posting Pro Featured Poster

Your problem is in your array class, you have declared half you data members at the top of the class (line 9) and then some more half way down (line 42) and as a result you have 2 size members which you use interchangeably but you only initialize one of them.

1. Put all of the class data members in the same place
2. Remove 1 of the 2 size members

Banfa 597 Posting Pro Featured Poster

dptr and bptr_1 both point to the same allocated objected. You should only delete an object once, as soon as you delete it the pointer becaomes invalid and any access to that pointer, either dereferencing it or trying to delete it again is undefined behavior.

You allocate the object pointed to by dptr and bptr_1 at line 22 but then you try to delete it at lines 32, 46 and 52. This will always cause an error.

Only delete an object once.

Banfa 597 Posting Pro Featured Poster

It rather depends on what else is implement and how you have implemented your division.

For example if you have implemented the division as while number is greater than divisor subtract divisor from number then whatever is left when you finish is the remainder.

If you want a specific operator to get the remainder after division (% operator) I do not believe you can do this without doing the division or as part of the division algorithm. For integer arithmetic the remainder from dividing x by y (i.e. x/y) can be expressed as

x - ((x/y) * y)

Banfa 597 Posting Pro Featured Poster

Compare line 33 and 36, you call printukas with no parameters but it expects 1. This is why you should provide prototypes for your functions before calling them. I expect you were getting and ignoring compiler warnings. Don't ignore compiler warnings they have been printed for a reason work out what it is and fix it.

When you call printukas without a parameter the parameter its expecting will get a random value, this probably wont be 0 (NULL) so the function will try to access the memory it is points to but since it is random it probably doesn't point to valid memory and your program crashes.

Banfa 597 Posting Pro Featured Poster

When you say it fails I assume you mean gives the wrong result?

This line of code is the issue

partial = a->word0 + b->word0;

word0 in your structure is 16 bits and you are on a 16 bit platform/compiler so this will perform the calculation in 16 bits and then convert it to 32 bits to store it in partial. Since the calculation is in 16 bits carry of the addition is discarded.

You need to do the calculation in 32 bits in order to keep the carry, you can do this by simply casting your variables in the calculation to 32 bits before doing the addition.

partial = (uint32)a->word0 + (uint32)b->word0;

Banfa 597 Posting Pro Featured Poster

In this case these line are declaring variables the * means pointer to so in

int* a;

This is read as a is a pointer to an int, that is a contains the memory location of an int variable.

int** b;

There are 2 * you read the pointer to twice, that is b is a pointer to a pointer to an int. b holds the memory location of an address that holds the memory location of an int.

int*** c;

c is a pointer to a pointer to a pointer to an int. On the whole I have rarely (if ever) seen code that dereferences this far (each * is 1 level of dereferencing) but it follows the same pattern.

The same applies to the variables declared in this code.

Banfa 597 Posting Pro Featured Poster

That is correct but I believe you have misunderstood what the || operator does.

|| is the logical or operator, it's output is true or false, or in integer terms 1 or 0. The inputs are treated as logical inputs which means in C++ 0 = false anything else is true. Then the implements the boolean logic

OR truth table
A|B|Out
0|0| 0
0|1| 1
1|0| 1
1|1| 1

You could use bitwise |, that performs the boolean or operator on each pair of bits in the 2 variables, i.e. 0xF0 | 0x0F = 0xFF. But the output of | has the same number of bits as its largest input.

If that is pseudo code and a and b are 128 bit numbers then the easiest way to hold then is as an array of smaller integers, say an array of 4 32 bit integers.

Banfa 597 Posting Pro Featured Poster

You for loop condition is wrong i<=-lenght the - sign means that for a string with at least 1 character the loop never runs because 0 is greater than all negative numbers. Also remember that C++ indexes start at 0 so if an array has 4 entries its valid indexes are 0, 1, 2, 3.

In light of these your loop condition should be i < lenght .

Finally lenght is spelt length

Banfa 597 Posting Pro Featured Poster

You are missing an opening brace in your declaration of accounts

int accounts[SIZE] = [b]{[/b]
5658845, 4520125, 7895122,
8777541, 8451277, 1302850,
8080152, 4562555, 5552012,
5050552, 7825877, 1250255,
1005231, 6545231, 3852085,
7576651, 7881200, 4581002 };
Banfa 597 Posting Pro Featured Poster

The problem is the way you have written your outer for loop, even though you don't increment i when out output a factor the loop always increments i so you never test the same factor twice.

However if you remove the i++ from the loop you will have another problem, since the way your loops are written you end up testing 1 and since 1 is a factor of everything you end up in an infinite loop. You need to avoid testing 1.

Finally your inner loop to see if i is prime is superfluous. If i isn't prime then num % i will be non-zero because you will have already tested num for the prime factors of i. The inner is just a slow way of working out if you need to test num against i when if it fails that test itself will fail anyway. But if you insist on keeping that loop then j only needs to go up to the value of i, values greater than the current of i can not be factors of i.

Banfa 597 Posting Pro Featured Poster

The problem is that you only have 1 buffer word so as soon as you encrypt the string it contains you no longer have access to the original data.

What you need to do is have 2 buffers and then copy the input data to the second one and do the encryption on the second buffer. Then you will still have both buffers once you have done the encryption so will have both the clear and encrypted text.

You function GetWord is poorly written because it makes an unnecessary assumption about the entry value of size when it could simply initialize size itself.

You seem to be using C++ so you should consider using std::string rather than char arrays.

Banfa 597 Posting Pro Featured Poster

Your post links are not working for me and a quick scan of the code shows no obvious problem (not to me again at least)

Banfa 597 Posting Pro Featured Poster

You get an assertion error either because

  • Your asserts are poorly written and aren't actually performing a valid test assert(I >= 0 && !MyType.empty()); why test MyType when you are actually accessing P a vector<MyType>? surely assert(I < 0 P.size()); would be better or even better let the vector handle it instead of calling P[I] call P.at(I) . at is a member of vector that does the same as operator[] but range checks the input value first.
  • At line 10 you have access the array with first setting any size to it so 0 is out of bounds
Banfa 597 Posting Pro Featured Poster

Really?? How do you get ardav out of mississippi?

uuencode64 ?

Mine is an acronym for: BenAtNoFixedAbode

Which I use in a number of email addresses; this was chosen when I went on holiday to Vietnam in 1999. I wanted an web email address to keep in touch with my family while I traveled, I tried MobileBen, BenMobile and a number of other short 2 word terms but always got the reply "that's not available would you like to use MobileBen2745264" but I've always felt that having a random series of digits at the end of your email address just makes it hard to remember. Having got quite frustrated in a fit of pique I bashed that into the signup form and it was accepted and has stuck. I now use Banfa because it is shorter to type or if it is not long enough BenANFA.

Banfa 597 Posting Pro Featured Poster

That just ain't how you do it, you need to implement (overload) ArrayOfTypes::operator[] and MyType::operator= separately to achieve what you want not try and do it together which is presumably this

ArrayOfTypes array;
MyType value;

array[10] = value;

ArrayOfTypes::operator[] returns a reference to a location in the array which can then be used to invoke MyType::operator= to assign to that location

class ArrayOfTypes
{
public: // ?
  vector<MyType> P;
     
  MyType& operator [](int I)
  {
    // Validate I in range ?
    return P[I];
  }

  // Implement a const version too for calling in const contexts to read the array
  const MyType& operator [](int I) const
  {
    // Validate I in range ?
    return P[I];
  }
};
class MyType
{
public:
  const MyType& operator=(const MyType& cpy)
  {
    // Don't copy self
    if (this != &cpy)
    {
      // Code to copy a MyType
    }

    return *this;
  }
};
triumphost commented: THANLS!! I solved it! +6
Banfa 597 Posting Pro Featured Poster

I can't use a dense matrix. I'm trying to store a dense matrix filled with numerous 0 values as an array of structs, which only holds significant values (ones not equal to 0). I am then trying to display that array of structs in the same format as the original dense matrix, so main is unaware that the matrix is beeing stored differently from a full 2D array.

I'm aware of that, I not suggesting you use a dense matrix, I'm suggesting you use a sparse matrix internally to your own class implemented using a std::map to handle all the look-up and data storage and allocation needs leaving you to concentrate on implementing the logic of the sparse matrix and removing from you the need to implement memory allocation, look-up functions and other utilities.

Banfa 597 Posting Pro Featured Poster

You still have the altered setting of "menu->properties->general->character set", change it back to UNICODE.

Notice your errors are on ShellExecuteA and ShellExecuteW when you call ShellExecute? ShellExecute is a macro and is defined to the real functions ShellExecuteA or ShellExecuteW depending on your character set chosen for compilation. ShellExecuteA is the ASCII version of the function ShellExecuteW is the unicode version of the function.

You input parameters need to match the input parameters expected by the function you are calling.

Banfa 597 Posting Pro Featured Poster

Well call rand() 4 times assigning the result of each call to your array of integers in sucession.

Banfa 597 Posting Pro Featured Poster

That is fine, but generate a 128 bit number from what? What is the source of the number you wish to generate or do you just want a random value?

Banfa 597 Posting Pro Featured Poster

A-ha well the answer is yes there is, pre-pend a ::

::shutdown(socketId);

this will call the method defined at the global scope (i.e. the one in the C WinSock2 API).

Ketsuekiame commented: Solution as requested, with example. Clear and concise. :) +9
Banfa 597 Posting Pro Featured Poster

You really should have posted the errors at each step, then you could have tried to help with the original problem rather than the problems with the non-working solutions to the original problem.

For starters "www.facebook.com".c_str() the problem here is that "www.facebook.com" is not a string object so you can call the c_str method. You could correct this by creating a temporary string object std::string("www.facebook.com").c_str() however c_str returns const char* and a string constant is const char* so std::string("www.facebook.com").c_str() and "www.facebook.com" are functionally equivilent, it would not solve any problems.

Your initial problem appears to be you are trying to pass an ASCII string to a function that expects a UNICODE string, the easiest solution might be to use UNICODE string constants by pre-pending an L to them

ShellExecute(NULL, L"open", L"www.facebook.com", NULL, NULL, SW_SHOWNORMAL);

rather than messing around with your compiler settings.

Banfa 597 Posting Pro Featured Poster

Use 2 64 bit numbers in a class. What do you want to generate the key from?

Banfa 597 Posting Pro Featured Poster

You need to explain more fully. It sounds like this is really a C question, you appear to be calling a C API even if your program is written in C++.

Are you saying that you are trying to write you own method called shutdown but you still want to be able to call the shutdown method in the API? If so then just use a different name for your method.

Banfa 597 Posting Pro Featured Poster

The function pointer in the structure is a data member of the structure just like any other data member and can be initialized like so

struct foo bar = { func };

But you are always either going to have to explicitly initialize it or assign it.

Banfa 597 Posting Pro Featured Poster

You could implement this fairly easily with a std::map rather than writing all your own code.

However clearly in your valueAt method you need to be iterating over the entire array not just performing a single if test. Your MatrixType must surely store a std::vector (or array) of oneitem and the length of that array (vectors provide their length). You need to iterate over the whole array testing each location until you find one that matches or return 0.0 if none match.

With a std::map you could have a key class

class MatrixKey
{
public:
  MatrixKey(int r, int c) : row(r), col(c)
  {
  }

  // Implement various getters, setters and comparison operators

private:
   int row, col;
};

Then declare your map std::map<MatrixKey, float> , use it internally to MatrixType when you need to find a value create a MatrixKey for row/col look it up in the map and if it isn't found return 0.0;

Banfa 597 Posting Pro Featured Poster

Sure that shouldn't be typeinfo.lib?

Banfa 597 Posting Pro Featured Poster

The real answer is that the overhead of calling a virtual function is so low that it is unlikely to cause you a problem in the majority of cases. This is definitely a case of trying to solve a problem before you know it exists.

In all likely hood your implementation with virtual functions will work plenty fast enough, you should build your solution correctly, with virtual functions, and then only start looking for ways of speeding it up if it turns out that it doesn't meet its performance criteria (go fast enough).

The problem with your proposed solution is that you can't call the functions through the base class, or more likely a base class pointer or reference. If you try you will call the functions in the base class rather than the functions in the derived class. i.i. std::vector<BasicShape&> will not have the behaviour you expect or probably want.

On the other hand if you don't need to call the functions through the base class then why do you need a base class at all, get rid of it and then at least there wont be an issue in the code waiting to rear its head when someone who doesn't know the functions aren't virtual treats them like they are.

Banfa 597 Posting Pro Featured Poster

You can't do it will a structure alone, you will need some code too.

Banfa 597 Posting Pro Featured Poster

getchar returns int not char. You need to store the value as an int to test against EOF because EOF is defined as an int (normally with the value of -1).

If you converted the value of getchar to a char before comparing to EOF you would have trouble in 1 of 2 ways

On systems with default signed char you would be unable to distinguish between the character value 0xFF and EOF because 0xFF is -1 in 8 bits.

On systems with default unsigned char you would never see the EOF because when getchar returned EOF (-1 in 16 or 32 bits 0xFFFFFFFF) it would be truncated to 0xFF. Then 0xFF would be converted to int for the comparison resulting in the value 255 and 255 is != -1 so you would loop forever.

It is more than perferable for c to be an int, it is essential.

Banfa 597 Posting Pro Featured Poster

It would help if you posted your code in CODE tags.

As well has having functions called smallest and largest you also have variables (int) called smallest and largest.

slygoth commented: Thanks this helps +0
Banfa 597 Posting Pro Featured Poster

You can use Visual Studio its OK. But if you are using Linux and Windows you may want to try something like Code:Blocks or Eclipse which are multi-platform and run on both Linux and Windows, that way you only have to learn the ins and outs of 1 IDE that you can use everywhere.

Both those IDEs use gcc on Linux and MinGW(a gcc port) on Windows.

Banfa 597 Posting Pro Featured Poster

In 32 bit Windows compiles far is #defined to nothing #define far (line 91 of WinDef.h.

On old 16bit compilers you will need to keep the far keyword in place as they will still be using the segmented memory model of 16bit windows. WIN32 systems have a WIN16 emulator (or thunk).

Note it appears that WIN64 does not have a WIN16 sub-system and you can not run WIN16 programs on WIN64 systems.

Banfa 597 Posting Pro Featured Poster

The only place I have come across far pointers in far pointers is as an extension in WIN16 C compilers.

16 bit Windows (WIN16, Windows 3.11 and earlier) had 16 bit addressing, which allowed you to access all of your 64kbyte memory segment, the pointer was the offset in the segment and was also called a near pointer although the near keyword was not required as it was implied by default.

However you could have other segments and to access these other segments you need a pointer that holds the segment number and the offset. The far pointer was the implementation of this, twice as large as a near pointer it held a 16 bit segment number as well as the 16 bit offset in the segment.

With the advent of WIN32 and a flat memory model the need for far pointers disappeared as you had 32bit pointers that could address the whole of the memory space for a process.

Banfa 597 Posting Pro Featured Poster

A-ha you are compiling and linking your program as a console application (which expects the entry to be int main()).

You need to compile and link it as a Windows application so that it expects the entry point to be WinMian.

You are either using the wrong project type in your IDE or you are using the wrong switches on your linker command line.

Banfa 597 Posting Pro Featured Poster

It would help if you posted the entire error message because following the text "unresolved external" is usually the name of the symbol it is unable to find.