mahlerfive 23 Junior Poster in Training

Vertices are the points or corners of a polygon. So a square is a polygon with 4 vertices, a triangle is a polygon with 3 vertices, etc.

mahlerfive 23 Junior Poster in Training

First of all, if you want 30 strings of up to 20 characters each, then your declaration should be the other way around, and you need an extra char for each null terminator (the character that ends a string):

char axx0[30][21]; // 30 strings of up to 20 character

Now for a little lesson on arrays/pointers. If you have a normal 1D array such as this:

char myString[10];

This declaration does a few things... it makes a pointer to char (type is char*), allocates space for 10 chars somewhere in memory, and then makes the pointer point to the first char in that allocated space. The pointer is named myString. So really, myString is of type char*, a pointer to char. Now when you do something like

myString[2] = 'A';

, what happens is it dereferences the myString pointer, moves 2 positions in memory, and puts the character 'A' there. Essentially myString[0] is the same as *myString, and myString[2] would be the same as *(myString + 2);

Now, when we move to 2D arrays, something similar happens. When we declare

char axx0[30][21];

, it makes a pointer to a pointer of char (type is char**) which is the variable axx0. Space is allocated for 30 pointers to char (char*), and axx0 will point to the first of those. Then, space is allocated for 21 characters for each pointer to char that was created, each pointer points to the first character of the corresponding 21 character …

mahlerfive 23 Junior Poster in Training

As someone asking a question, I would much rather prefer answers to be short, few, and useful, rather than have everyone post answers and having to sort out which one is correct. In some things there can be debate (style issues and such), but on correctness there is no room for that.

Also - you should only answer recent questions.. not ones that are 4 years old.

mahlerfive 23 Junior Poster in Training

Both answers are pretty much correct, depending on how you look at it. Passing a pointer to an object is considered passing by reference since the function now has a way to reference the object outside of itself. However, it is passing by value since you are really just passing the value of the pointer to the function, and that changing the value of the pointer itself will not affect anything outside the function.

What is the purpose of passing a pointer to a function though? The correct reason to do so would be to let the function see or alter the object that the pointer points to - which is the ultimate goal of passing by reference. It is on this point that I view passing a pointer as passing by reference over passing by value. I do find it important though that people understand both ways of thinking - however, for someone just learning the concept, I find that learning the REASON we do it first is most important.

mahlerfive 23 Junior Poster in Training

Oops I forgot to include a passing by reference example.. here it is:

#include <iostream>
using namespace std;

int main() {
   int x = 5;
   cout << "In main before calling function, x = " << x << endl; // result is 5
   MyFunction( &x ); // this time we send a pointer to x (passing by reference)
   cout << "In main after calling function, x = " << x << endl; // should be 10 now

   return 0;
}

void MyFunction( int* x ) { // accepts a pointer to x (reference)
   cout << "In MyFunction, x = " << x << endl; // result is 5
   *x = 10;
   cout << "In MyFunction, x = " << x << endl; // now it's 10
}
mahlerfive 23 Junior Poster in Training

A lot of important questions here, so I will try and answer them the best I can.

First, the main difference between passing by value and passing by reference.. When we pass by value, the receiving function obtains a copy of the variable. If the function then alters the contents of that variable, these changes are not seen outside the function. This means the function has it's own local copy of the variable which does not affect anything outside of it.
When we pass by reference, we are passing a pointer or reference to an object, not a copy. This means when the function alters the object being referenced, the object is also changed outside of this function. Here is a quick example:

Passing by Value:

#include <iostream>
using namespace std;

int main() {
   int x = 5;
   cout << "In main before we call the function, x = " << x << endl; // result is 5
   MyFunction( x );
   cout << "In main after we call the function, x = " << x << endl; // result is still 5, even though we changed x inside MyFunction
   return 0;
}

void MyFunction( int x ) {
   cout << "In MyFunction x = " << x << endl; // result is 5 
   x = 10;
   cout << "In MyFunction x = " << x << endl; // result is now 10
}

If this does not fully make sense, then you should read about "scope" …

OmniX commented: Nice detailed explination, thankyou! +1
mahlerfive 23 Junior Poster in Training

This is a prime example of how I see some students graduate in computer science and don't understand basic object oriented programming concepts. You need to figure this out yourself. Of course if you try and have most of it worked out but are hung up on something very specific and need help then by all means, ask questions and learn - but just asking for us to answer the entire question for you means you learn nothing and pollute the computer science world with people who can't code and can't understand basic programming principles.

Salem commented: Absolutely! Well said. +20
mahlerfive 23 Junior Poster in Training

If you are asking "why is overloading functions useful", then the answer is because sometimes we want two or more functions with the same name which provide the same functionality, but which require different parameters.

Lets take an actual example and look at the append() function on std::string. Here are a few of the overloaded functions:

string& append ( const string& str );
string& append ( const char* s );
string& append ( const char* s, size_t n );

All three of these functions have the same name and do basically the same thing - they append something to a string. So why do we need more than one version? Well assume you want to append another string to your string, then you would call the first version which takes the string you want to add on as a parameter. But wait, what if you have an array of char instead of a string? Well, just use the second version of the function which takes a pointer to char. But what if you just wanted to append part of the char* string? Then use the 3rd version which takes the char* parameter as well as the number of characters that should be appended.

mahlerfive 23 Junior Poster in Training

To be a bit clearer, you can use the stringstream class like so:

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    stringstream ss; // our stringstream object
    int a = 1, b = 2, c = 3;
    
    ss << a << b << c; // we insert a b and c into the stringstream
    cout << ss.str(); // call str() to get the string out
    
    system("pause");
    return 0;
}

For more info on stringstream check out: http://www.cplusplus.com/reference/iostream/stringstream/

OmniX commented: Perfect example, thankyou. +1
mahlerfive 23 Junior Poster in Training

The way you've structured your headers (.h) and implementation (.cpp) files is not very good and is probably causing the problem.

Typically for a class you should always have a header file which you have header file "guards" and inside you declare the class. It should look something like this:

#ifndef _CLASSFILENAME_H_      //part of header guard
#define _CLASSFILENAME_H_     // part of header guard

// necessary includes and stuff go here

class MyClass {
   public:
      MyClass();
      void Foo();
};

#endif   // end of header guard

Then your .cpp file which contains the actual class definition should look like this:

#include "Classfilename.h"

MyClass::MyClass() {
   // do constructor stuff here
}

void MyClass::Foo() {
   // do foo stuff here
}

Then you should have a main which is completely separate from the class in main.cpp. You simply include the class file header and now you can use the class.

#include "Classfilename.h"

int main() {
   MyClass x;
   x.Foo();

   return 0;
}
mahlerfive 23 Junior Poster in Training

I agree with Radical Edward - you may save some time switching from vectors to arrays (maybe half the time), but you may be able to structure your data so that you can perform your most often called operations in log(n) time instead of linear time. In this case a linear solution that took 1 second might take 10ms - a much larger improvement. What exactly are you storing and what kind of operations are you doing that is taking your program too long?

mahlerfive 23 Junior Poster in Training

Oops, sorry, the post before me was 1 hour ago, didn't check the OP posting time

mahlerfive 23 Junior Poster in Training

Your code is fine, can you paste the compiler error?

mahlerfive 23 Junior Poster in Training

By "taking the address" I believe it means using the address-of operator (&). An example of taking the address of something:

int x;
int* y = &x; // here we take the address of x and assign it to the pointer y

The "definition" part it talks about should be implemented something like below. Assume you have GamePlayer class defined in GamePlayer.h and the actual implementation is in GamePlayer.cpp:

// GamePlayer.h
class GamePlayer{
   public:
      static const int NumTurns = 5;

      GamePlayer();
};
// GamePlayer.cpp
#include "GamePlayer.h"

const int GamePlayer::NumTurns; // definition of NumTurns... 

GamePlayer::GamePlayer() {
   // constructor code here
}
mahlerfive 23 Junior Poster in Training

Are you trying to convert a string of digits to a number you can do calculations with? If so, you can you atoi().

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

mahlerfive 23 Junior Poster in Training

At first glance I don't see the problem. I do have a question though - where is cdstock coming from? It's not a parameter to your function. If it is a global variable, you should really change that...

Anyway, it might even be a problem with your testing code where you are printing the albums, can you post that?

mahlerfive 23 Junior Poster in Training

Having all those numbers in front and parsing each line is much more difficult than just having the number of tracks listed after the genre.

mahlerfive 23 Junior Poster in Training

If you dont want to waste space in your array then you need to use something like a vector which I already recommended. If this is for an assignment or something and you're "not allowed" to use any STL containers (like vector or list), then you would have to make your own implementation of a linked list. This is not a trivial task if the concept is new to you, however it is worth learning.

Basically, STL containers, and linked lists, among other structures, dynamically allocate memory for items as you insert into them. The basic concept for a linked list is that it keeps a pointer to the first time, and the first item has a pointer to the second item, and so on. So you can just keep creating items and linking them on to the end whenever you want.

mahlerfive 23 Junior Poster in Training

The first error means that the function ingresar_datos has never been declared - if you look through the code, there is indeed no function with that name.
The rest of the errors are because the function prototype is slightly different than the function definition. For example, for menu you have the following as the prototype:

char menu (char);

But the following in the actual definition:

char menu (char &ingresar)
mahlerfive 23 Junior Poster in Training

Sounds like a graph problem. You can use a graph, where each item (A, B, etc.) are nodes/vertices, and each one-directional link/edge implies a relationship (i.e. owns). There are a few ways to represent graphs in code, one way is with an adjacency list. Essentially this is a 2D list where the first dimension is a list of the objects (A, B, etc) and for each object, there is a list of all the objects it has edges to (owns).

I've never made a graph where there are multiple kinds of relationships, although you could have one graph for each relationship type if that is feasible.

mahlerfive 23 Junior Poster in Training

Yes you are going to have to change the code in your loop a bit to reflect the changes in the structure.

For example, if x is counting to the number of tracks in the album, replace:

ins.getline(cdstock[x].tracks.trackName, 256);

with:

ins.getline( cdstock[i].tracks[x].trackName, 256 );

You want to alter the ith cd, but the xth track.

mahlerfive 23 Junior Poster in Training

If you can't use a vector, then yes, you could have an array of struct track of size 20, but then you must store the number of tracks in the album information as well.

so replace:

vector<struct track> tracklist;

with:

struct track tracklist[20];
mahlerfive 23 Junior Poster in Training

Here is how I load a bitmap in my bitmap class (note that my bitmap class only supports 24bit bitmaps):

// Loads bitmap from a file, converts to grayscale
bool Bitmap::load_bitmap_file( char *filename ) {
    ifstream fileStream;
    int fileLength;
    bool previouslyLoaded = this->loaded;
    
    this->loaded = false;
    
    //open filename in read binary mode
    fileStream.open( filename, ifstream::binary );
    if( !fileStream.is_open() )
        return false;
    
    // get length of file:
    fileStream.seekg (0, ios::end);
    fileLength = fileStream.tellg();
    fileStream.seekg (0, ios::beg);
  
    //read the bitmap file header
    fileStream.read( (char*)&( (this->bitmapFileHeader).bfType ), sizeof( WORD ) );
    fileStream.read( (char*)&( (this->bitmapFileHeader).bfSize ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapFileHeader).bfReserved1 ), sizeof( WORD ) );
    fileStream.read( (char*)&( (this->bitmapFileHeader).bfReserved2 ), sizeof( WORD ) );
    fileStream.read( (char*)&( (this->bitmapFileHeader).bfOffBits ), sizeof( DWORD ) );
    
    //verify that this is a bmp file by check bitmap id
    if( (this->bitmapFileHeader).bfType != 0x4D42 ) {
        fileStream.close();
        return false;
    }
    
    //read the bitmap info header
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biSize ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biWidth ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biHeight ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biPlanes ), sizeof( WORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biBitCount ), sizeof( WORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biCompression ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biSizeImage ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biXPelsPerMeter ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biYPelsPerMeter ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biClrUsed ), sizeof( DWORD ) );
    fileStream.read( (char*)&( (this->bitmapInfoHeader).biClrImportant ), sizeof( DWORD ) );
    
    //move file point to the begging of bitmap data
    fileStream.seekg( (this->bitmapFileHeader).bfOffBits );
    
    //allocate enough memory …
mahlerfive 23 Junior Poster in Training

First thing, you should really rename tracklist to track since that struct is only storing info about one track.

The main problem is that your album structure only can store one track. You will need to alter your album structure to be able to store a list of tracks that is variable in size. The easiest way to do this is to use an STL container such as vector. Instead of having:

struct tracklist {
   char trackName[32];
   int trackMins;
   int trackSecs;
};

struct albums {
   char idNo[10];
   char albumName[32];
   char artistName[32];
   tracklist tracks;
   float price;
   int stock;
   char genre[32];
};

Do something like this:

struct track {
   char trackName[32];
   int trackMins;
   int trackSecs;
};

struct albums {
   char idNo[10];
   char albumName[32];
   char artistName[32];
   vector<struct track> tracklist;
   float price;
   int stock;
   char genre[32];
};

Now the problem becomes how can you determine how many tracks are in each album? Either you can store the number of tracks for that specific album in the file above the track listings so you know how many times to loop, or if you can assume track names don't start with numbers, once you read in a track name starting with a number, you know it is really just the id of the next album. I would go with the first option myself if you can change the format of the file.

If you haven't used vectors before, check out: http://www.cplusplus.com/reference/stl/vector/

mahlerfive 23 Junior Poster in Training

You could try and write a very simplified chatroom program. You would need to write two programs:
1. The server, which accepts connections from all the clients. Collects messages from clients, and sends them to all the other clients.
2. The client, which gets a message from the user, sends it to the server. It also receives messages from the server and displays them on the screen along with the name of the client that sent the message.

In doing this you would need to learn about basic networking and threads - two very important topics that if you learn them now will help you breeze through some parts of your courses later in school.

If you have never done any networking or threads before, I would suggest trying to learn both individually first by just making some small test programs, then once you have a feel for the concepts, try and put the chat program together. If you finish quickly you can always extend it to have multiple chat channels, add commands that the client can use, add password authentication, etc.

mahlerfive 23 Junior Poster in Training

I have only dealt with bitmaps (.bmp), but essentially you need to open the file, read in the header information (stuff about how big the file is, width, height, # of colours, etc.) followed by the image data.

I'm assuming by dither or halftone, you mean to cut down the number of colours (like to black and white). If so, you would loop through the image data and change each pixel depending on the RGB value already there. If you are aiming to reduce the size of the image, you may need to process the pixels a bit differently and you will need to mess with the header.

The BMP file format can be found here: http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html

mahlerfive 23 Junior Poster in Training

My goal here is to write a small class to keep track of memory I allocate/deallocate so that I can more easily find memory leaks. To do this I overloaded the operators new, new[], delete, and delete[]. In those methods, I allocate/free memory as usual, but also make a call to my MemoryTracker object which stores this information. The MemoryTracker is a singleton.

My problem is that whenever I call new, the Allocate() method in my memory tracker inserts into a map which ends up calling new, thus causing infinite recursion. What I am looking for is an elegant solution around this that will work in linux/windows and in both single-threaded and multi-threaded environments.

So far I've had a few ideas:
1. Right before inserting into the map, set a flag in the memory tracker to ignore tracking the next allocation. Unfortunately this won't work in a multi-threaded environment.

2. Somehow grab the pointer of the pair object as it's being created and tell the memory tracker to ignore it. I don't think this is possible, since we don't have the pointer to the object until we are inside operator new.

3. Inside operator new, determine what kind of pointer is being created. If it is of type pair< void*, size_t >, then ignore it. The problem here is that I can never use pair< void*, size_t > ever again anywhere in my projects.

Here is my main:

#include <cstdlib>
#include <iostream>
#include …