Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

priyairani00> "program to swap two no. using third variable"

Maybe I misread the problem -- I thought "no." meant "not", which is why I posted the link I did.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
tmp = a;
a = b;
b = tmp;

you posted wrong solution. Can not use temp variable.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>This user has not registered and therefore does not have a profile to view.

I get this message when I click on someone's avatar.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster


csurfer :i retried gotoxy(x,y) it didnt work properly

The problem is not gotoxy() -- the problem is your code;

i am looking for a function if possible

There isn't one -- you have to write it yourself.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Another question ...
If I have a class like this one, I have to manually delete created structs of CUBE right?

Yes, but why bother with those pointers? You should be able to allocate 5 of those tiny structures on the stack with no problem. Using pointers in this situation does nothing more than makes the problem more complex then it needs to be.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what os and compiler ??? Looks like TurboC code, but can't be sure.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>cube = new CUBE;
You can't use the new operator on a vector. All you have to do is use its push_back() method to add a new item.

CUBE* cb = new CUBE;
cb->number = 0;
cb->changed = false;
cube.push_back(cb);

But you can make your life a little easier by adding a constructure to initialize the structure's variables.

And -- do not make it a vector of CUBE pointers. You could use pointers, but life is a little easier without them, especially since CUBE is such a small structure/class.

struct CUBE
{
    int number;
    bool changed;
    CUBE() { number = 0; changed = false; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<CUBE> cube;
    cube.resize(5);
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The stats in Control Panel seems to have a few problems.
>>Forum Threads Started: 3,668
I have not started that many threads! :icon_eek: According to the link "search all threads started by ...", I only started 190 threads.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

class variables do not have to be passed as parameters to class methods because the methods already have full access to them.

Post an example of why you think you need global variables.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what compiler? what os? what kind of directives ? Do you mean pragmas or preprocessor directives or something else?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. pass the variable as a parameter to the function.

2. you mean something like this?

void foo(int x)
{
    cout << "x = " << x << "\n";
}

int main()
{
    int n = 123;
    foo(n);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you have a static array, such as struct students std[200]; then to delete on of the students you a couple choides

1) just reset the ID and other variables to some default value (such as 0). Then when searching just bypass any arrays whose id value is 0.

2) Move all the structures up one slot to fill up the structure you want deleted. For instance if there are 10 structures in the array and you want to delete #5, then move 6-10 up one to 5-9 and set the 10th one to default values, such as 0 or "" string.

If you use a vector instead of the above all you have to do is call the vector's erase() method.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

okay tnx but pls how can i create an array that can sort?

The array you created can be sorted. There are more efficient ways to do it, but what you did is ok. Google for "bubble sort" to get the algorithm for that one, which is the easiest to code.

And, as previously mentioned, you will have to change that structure, because it needs to contain strings for name, geneder and nationality. What you have will only hold a single character

struct student
{
  int ID;
  std::string name;
  std::string nationality;
  std::string gender;
};

If you are required to use character arrays instead of std::string you could do it like this too:

struct student
{
  int ID;
  char name[80];
  char nationality[80];
  char gender[2]; // either 'M' or 'F'
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what you need to do is write a switch statement in main() that will process each of the menu options. For example if the user selects option #1 you do not want to make him also do the other options.

After that last cout in main() you need to add something like the following

int answer;
cin >> answer;
switch(answer)
{
    case 1: // insert new record
        InsertNewRecord(array); // you will have to add this function
        break;
   // now do this for each of the other options
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Those are the requirements, not a question. Are you asking us to write the program for you?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is your question?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

which line of that code are you talking about?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

i am now working on another way to load in the data by tokenizing the data of each object (e.g. name, id, etc) into/from a plain text file...would that be a better way than to write in the whole object into the file?

thanks alot for your explaining :P

yes, I agree that would be an excellent idea. Binary files just don't work very well with c++ classes.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That line could have been coded more simply like this: writer.write(reinterpret_cast<char*>(this->myAdvisor) If you just write out the this pointer all that will get written is the value of the myAdvisor pointer, not the object to which it points.

Depending on how the rest of your program is written you may not have to write out myAdvisor at all.

When you read myAdvisor back you will first have to allocate memory for myAdvisor and then read into that pointer. I don't know how you set the value of that pointer initially, but you will have to do it again before reading back from the data file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>writer.write(reinterpret_cast<char*>(this)

The class can not be written that simply because it contains std::string and pointers. That makes the serialization algorithms much more complex and makes the file a variable length records, almost like any ordinary text file.

If you have to use binary format, I would suggest you use character arrays for Human class name and id to simplify the file and make it use fixed-length records.

class Human {
	protected:
		char name[80], id[80];
	public:
		Human(string n = "", string id = "") {
			changeName(n);
			changeID(id);
		}
		
		string getName() { return name; }
		
		string getId() { return id; }
		
		void changeName(string n) { strcpy(name,n.c_str()); }
		
		void changeId(string id) { strcpy(this->id,id.c_str()); }
		
		void showData() {
			cout << "Name: " << name << endl;
			cout << "ID: " << id << endl;
		}
};

All it needs to write out is the base class since the derived classes have no objects of their own that need to be serialized. writer.write(reinterpret_cast<char*>(&*this->myAdvisor)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A double click is nothing more than two clicks in rapid succession. Your program will get two click events, not one.

[edit]Never say never! If you are using Windows hooks then you might want to read this article, especially one of the comments at the end of the article.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Easy implementation. It becomes a little more complicated when you want to also use '*'. Much more complex implementations can be found in boost regex library (regular expression library).

bool wordPattern(string pattern, string word) {
    if( pattern.length() != word.length())
        return false;
    for(int i = 0; i < word.size(); i++)
    {
        if( pattern[i] != '?' && pattern[i] != word[i] )
            return false;
    }
    return true;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Forget about 2005 Express -- its been updated to 2008 Express. 2008 is a better compiler anyway and doesn't need the Windows SDK in order to produce windows gui programs. But you have to hurry because it won't be available for much longer, as soon as 2010 is released (its already in beta).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>if(_stricmp(value,gen_arr)== 0){

gen_arr a single character array? or is it an array of strings? If its a single character array then do this (without indexing) if(_stricmp(value,gen_arr])== 0){

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have not written the implementation code for the functions in those two classes.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The 'C' just means C-language.

>>Is that a common practice in software development?
No. Microsoft coders like to use such prefixes in their classes/structures, but not in filenames.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Answer to Ancient Dragon:
When I downloaded the SDK I got some samples. none of them uses directsound :(

I found several of them here (on my computer)
C:\Program Files (x86)\Microsoft DirectX SDK (August 2008)\Samples\C++\XAudio2\Bin\x86

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Look up the function you want in MSDN -- it will tell you what lib you need. Yes I know that link is for WinCE, but it also applies to desktop win32.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have to link with the appropriate DirectX libraries. When you downloaded the DirectX SDK you also got a lot of sample programs. Look in them and they will tell you what libraries you need. Or you can just read MSDN to find that out.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

win32 api functions

See link in my previous post

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Are you using MFC or win32 api functions? When you created the edit box you got a HANDLE to the object. See this article

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

must be something else wrong because this compiles ok with vc++ 2008 Express

class A
{
public:
    void* ptr;
    A() {ptr = 0;}
};

int main()
{
    A* a = new A;
    int* p = static_cast<int*>(a->ptr);
    *p = 1;
}

This one also has no problems

class A
{
public:
    void* ptr;
    A() {ptr = 0;}
};

class B 
{
public:
    int* ptr;
    B() {ptr = 0;}
};

int main()
{
    A* a = new A;
    B* b = new B;
    b->ptr = static_cast<int*>(a->ptr);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 6: >> fstream file(nome, ios::in || ios::out || ios::binary);

You are using the boolean || operator, not the bitwise | operator

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The rules change slightly in derived classes -- derived classes can not access private members of its parent class. Also non-class functions can access only public members of a class

class parent
{
private:
   int x;
protected:
   int y;
public:
   int z;

   parent()
   {
       x = y = z = 0; // ok
   }
};

class child : public parent
{
public:
    child()
    {
        y = z = 1; // ok
        x = 2; // illegal (private)
    }
};

int main()
{
    child c;
    c.z = 3; // ok
    c.y = 4; // illegal (protected)
    c.x = 4; // illegal (private)
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It works as its supposed to work. rv.i is accessing the private member of its own object.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You mean it crashes like that without even running your program?? My immediate guess is that your program has harmed either the video device driver or RAM chip. Try downloading and running a computer diagnostics program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to allocate memory for the string because GetDlgItemText() does not do that for you.

char text[255] = {0};
GetDlgItemText(hwndqw2, IDC_ANSQW, text, sizeof(text));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what operating system ?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is an example : Called by address.

void change1(A** source, A** destination)
{
  *destination = *source;
}

....
change1(&ob1, &ob2); // called (passed) by address

That may or may not be the correct solution, depending on whether he wants to copy the address of the two objects (set the two addresses to be the same) or copy the class and leave the addresses unchanged.

If he wants to copy the addresses as your function does then he needs to delete the allocation for the destination parameter because your function will cause a memory leak.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It doesn't work for the same reason that the change function doesn't work. *ob1 = *ob2; When you write ob1 = ob2 all that is doing is setting the address of ob1 to be the same as the address of ob2. You have to add the star to copy the object.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
void change(A* source, A* destination)
{
    destination = source;
}

That doesn't do what its supposed to do. All it is doing is swapping pointer addresses and not the objects to which they point. Therefore that function is useless.

This is the correction: (Note: you may have to write a copy constructor for the class)

void change(A* source, A* destination)
{
    *destination = *source;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Just try to open it -- if open files then its probably already opened (assuming COM1 exists and there are no hardware problems)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In Code::Blocks IDE, select menu item Settings --> Compiler and Debugger. Click the Linker Settings tab. Then click the Add button and enter the name of the library.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Instead of waiting for someone to answer you why don't you try browsing around your compiler's IDE options. Look for something like Linker options, and Additional Library Dependencies. It won't be those words but something similar.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have a *nix port of g++ compiler. So it uses *.a. Look in your <compiler's install directory>\lib

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are two ways to link: 1) static link, which normally does not require the DLL so the code is inside your .exe file, and 2) dynamic link, where the *.lib file only contains enough information for the linker to resolve references, and your program will need access to the DLL at runtime. There advantages and disadvantages to both link types. I normally do the dynamic link to keep the size of the *.exe as small as possible.

BTY: for your compiler you should have a library with *.a extension, not *.lib.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you use it the same way that you use any other standard library. Take time.h for example. That header file contains function prototypes for functions such as time(). Then all you do is call time(), passing the appropriate parameter. When you compile the program the compiler will link with the standard library that contains the source code for the time function.

You do that too. If you have a header file for that dll then include it in your program. If not, then just add the function prototypes to the top of your program, after all other include statements. Then all you have to do is call the function just like you would have called time() in the above example.

You have to tell your compiler the name of the *.lib file. I don't have Code::Blocks installed so I can't tell you how to do that, but I'm sure it should be fairly easy to find out by browsing around the program's project options.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Your program is already using several .lib files from the standard C and C++ libraries. If its a dll that you write and compiled, then most likely your compiler generated a lib file. If its a dll that someone else write/compiled then you might be able to get the *.lib file from whoever wrote it. AFAIK its not possible to generate a lib file from just the DLL.