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

IMO phthon is a toy that non-programmer hobbyists use, in the same category as Quick Basic. C/C++ is used by professional programmers to write real professional programs.

Rashakil Fol commented: fail -1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In the while loop you need to change variable i to something else because that while loop is destroying the value of i that the for loop is supposed to increment. Copy the value of i into another variable, such as k and use k in that while loop.

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

lines 57 and 58: you do not have to initialize the arrays one letter at a time -- just do the entire string at one time

char currentWord[] = "......"; /* The dot word */
 char guessWord[] = "monkey"; /* The word that needs to be guessed */

line 65: >> while (currentWord != guessWord) {
You can not compare two strings using the != or == operators. You have to call the function strcmp(), like this: while( strcmp(currentWord, guessWord) != 0){ strcmp() returns 0 when the two strings are identical (including letter case).

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

The term "precompiled headers" means something completely different than what you are trying to do. You are tring to create a library of functions/classes that can be used in other projects. All you really need in those other projects is the *.h and *.o (or *.obj) files.

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

Get the input data as a string, then your program can check its contents to see if it's numeric data or not. Since you are posting in the c++ forum I'd suggestion using cin and std::string, not scanf() or fgets() which are from C language.

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

scnaf with %d will only accept numeric data -- anything else is ignored and scanf() does not change the value of the input parameter. Check the return value to see if scanf() successfuly converted any data.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int* A = new int[10]; // dynamic array

int A[10]; // simple array

If you want the explanation in words, then I'd suggest you look them up in your text book. There are also a lot of online explanations such as this one.

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

You can erase an element from the map, but you can't delete just one of the pointers because it belongs to an array. If you allocate the pointers one at a time then you could delete them in any order you want.

map<int,particle*> particle_map;
for(i==0;i<number_of_particles;i++)
{
   particle_map[i] =   new particle;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You could just say p1 = right.p1; but that could be dangerous if right ever goes out of scope or is destroyed. A safer approach is to allocate new memory for p1 and copy the contents of right.p1 into it.

A& operator=(const A& right)
{
    A a;
    a.p1 = new C;
    if( right.p1 != NULL)
       *a.p1 = *right.p1;
    return a;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You did not post anything that needed an assignment operator =. Assignment operators are not used in class declarations. Maybe you mean the overloaded = operator???

Your class doesn't have any public members, so none of its objects (p1 and p2) can be accessed outside the class.

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

You can specify the language, such as [code=html]. I don't know if you can successfully specify more than one language in the same post -- you'll just have to try it and find out what it does.

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

I already told you the problem -- line 55 doesn't work because 0 is never > than MONTHS. You changed the wrong line. You should have changed line 45, not line 55. Put line 55 back the way it was with < character.

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

What is wrong is that you are using some crappy compiler such as Turbo C to run an MS-Windows 32-bit program. Can't be done. Toss out that compiler and use a modern compiler such as Code::Blocks (with MinGW compiler) or VC++ 2008 Express -- both are free.

Also: the / character can not be escaped -- just delete the \ before /

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

The ordering is not done in the code snipped I posted, but in the sort function you posted. On line 48 of post #9, change > to <

>>When I changed < to > it prints out nothing.
Of course it will print nothing because 0 is never > MONTHS.

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

The program in post #9 works ok for me after making one small change. You are calling the sort function twice, and not displaying the results after sort is done.

cout << "Here is the sorted values\n";
    for(int i = 0; i < MONTHS; i++)
        cout << values[i] << " ";
	//selectionSort(values, name, MONTHS);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The extern keyword must be used in header files like global.h to avoid duplicate declaration errors when you include the header file in two or more .c files.

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

Functions know nothing about variables declared in other functions. If you want selectionSort() to use variables in another function then those variables have to be passed to selectionSort() as parameters.

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

What you are already doing should be ok. A semiphore is just another way of doing it.

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

If you intend to modify Andy's contents from different threads then you need to synchronize the threads so that access to Andy is limited to one thread at a time. The most common way is via semiphores.

For platform independece -- you might check out the Boost libraries to see if it contains something to create and synchronize threads.

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

use std::string's c_str() method

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

Probably the most efficient method that I could think of (in terms of line of code) would probably be something like this:

the replace() function may make your code smaller, but efficiency is probably the same. Somebody has to loop through the string one character at a time. Whether you do it or replace() doesn't really change the efficiency of the program at all.

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

The Seek() method returns a long long which is the length of the file

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

Here is another way to do it.

string space2underscore(string text)
{
    for(int i = 0; i < text.length(); i++)
    {
           if( isspace(text[i]) )
                text[i] = '_';
    }
    return text;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Now is when you need to learn how to use your compiler's debugger so that you can single-step through the program one line at a time. I already told you why your program doesn't work. The problem is in that Clear() method. Your program works ok for me when I made that change.

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

The general way to get the file size is to seek to the end of the file then get the current file position. I don't know how you would do that with CLR/C++ (managed code), but I'm certain if you read the available methods in FileStream you will find it.

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

The program doesn't work because the Clear() method destroys the value of MaxBytes by setting it to 0, which makes that Pack() return false when checking if size > MaxBytes.

void LengthTextBuffer::Init(int maxBytes)
{
	if(maxBytes<0)
	{
		maxBytes=0;
	}
	MaxBytes=maxBytes;
	Buffer=new char[MaxBytes];
	Clear();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You don't have to read the data back into a account class. You can do anything you want with the data, such as read it back into individual char arrays if you want to. The data file is not tied to any class or structure.

If you made the individual fields of the account class std::string instead of character arrays, then you would need to pack the data into fixed-length fields before writing them to the file. But as it is, the account class already contains fixed-length fields, such packing is not necessary. When you write a class to a data file only the data objects are written, not any of the methods.

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

There is no need for the pack() functionality. The account class is already packed (fixed-length). Just write the account object to a binary file.

void write(Account& obj)
{
    ofstream out("filename.dat", ios::binary | ios:: app);
    out.write( (char*)&obj, sizeof(Account));
    out.close();
}

similar to read it

void read(vector<Account>& accountList)
{
    Account obj;
    ifstream in("filename.dat", ios::binary);
    while( in.read((char *)&obj, sizeof(Account))
    {
         accountList.push_back(obj);
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The file was read on lines 28-30, why are you trying to read it again on lines 34-37? And line 37 will just read all the numbers into the same element of Array.

My guess is that on lines 34-37 you need to just display the contents of the array on the console screen ??? Use printf(), not scanf() to do that, and you don't need the file pointer at all there.

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

The first thing you should do is initialize BlockDataType array with all 0s before doing anything else with it -- that will clear the array of all random data. Now lin line 14 fwrite() will write 0s for all unused bytes in that array. fwrite() will write blocksize number of bytes to the file, regardless of what BlockDataByte contains. Note that the contents of binary files can not be read by text editors such as Notepad.

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

I didn't knew the question was confusing. However some geniuses have decoded it correctly. Here I go again. Suppose I want to use bass.dll which have functions written in C, is there special thing i need to include or just give it a go?

Thanks for suggestions and link. I really learn here at DW

I think everyone has already answered that question. First, create function prototypes for the functions your c++ program needs to call, surround them with extern "C", then put the DLL somewhere the OS can find it, such as one of the directories in your PATH environment variable, or in the current working directory.

Another way to do it is with dynamic loading -- First call LoadLibrary(), then GetProcAddress(), which will return a function pointer.

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

>>I'm speaking most particularly of GUI class frameworks.
There are no GUI frameworks in standard c++. You must be thinking of some 3d party libraries, such as wxWidgets, MFC, OpenGL, DirectX, etc. etc. None of them are part of standard c++. Maybe you should read a book about c++ to find out what all is included in the standard.

As for the op's question, he wants to know how to call C functions from C++. The answer is that c++ programmers do that all the time, such as by using any of the functions in stdio.h, string.h, stdlib.h, win32 api functions, etc (there are hundreds of them). All those functions are written in C language, and all be called by C++. Its just a matter of telling the c++ compiler that the functions are in fact C functions, not C++, so that the compiler will not mangle the function names.

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

you have to declare them extern "C".

#ifdef  __cplusplus
extern "C" {
#endif
// C function prototypes here
#ifdef  __cplusplus
}
#endif

You will find this example in stdio.h as well as other c header files that may also be used in c++ functions.

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

When I come to DW I have the C++ forum bookmarked. So the first thing I do is look at the threads in that forum to see if I need to reply to any of them (via the New button). Then I'd use the Favorite Forums links to do the same with the other favorite forums. In the 4+ years that I have been a member here I think I may have visited the DW Home page 5 or fewer times.

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

If you need the space, get rid of "Most Recently Viewed Threads" and put back My Favorite Forums.

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

Hey thx :)

btw I got my other code working too :P

... It was a problem with unicode -.-..

SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,"C:\\wallpaper.bmp", SPIF_SENDCHANGE);

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,_T("C:\\wallpaper.bmp"), SPIF_SENDCHANGE);

Tadaaa both working!

Tadaaa -- when compiled for UNICODE those two are identical.

tadaa this working too..

SystemParametersInfoW(SPI_SETDESKWALLPAPER,0,L"C:\\wallpaper.bmp", SPIF_SENDCHANGE);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok the issues is resolved thanks for all the help.

What turned out to be the problem was that i was not compiling and building my program in the IDE.I was always under the impression that to output to a file all i needed to do was compile and run the .exe

Lesson learned :D

It is NOT necessary to compile a program using an IDE. You can use command-line compiles with or without a makefile if you want to. What compiler was you using and how was you compiling the program (post code to compile).

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

The damed program works fine as it is, its not necessary to change the data type of age, nor would it even be desireable. You're trying to make a binary file act like a text file -- it doesn't. You can not look at a binary file with a text program such as Notepad or Wordpad.

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

Add this to the bottom of that program to verify that it worked (works ok for me on Windows 7 using VC++ 2008 Express compiler)

ifstream in("myFile.dat", ios::binary);
    int k = 0;
    if( in.is_open() )
    {
        in.read((char *)&k, sizeof(int));
        cout << "k = " << k << '\n';
    }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to include wininet.h and shlobj.h, then compile the program for UNICODE

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

1) you can not compare a single character such as aData with a string such as "Exit".

2) 'Exit' must be surrounded with double quotes, such as "Exit".

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

replace all 7 loops with something a function

void GetMartks(std::string prompt, float& mark)
{
    cout << prompt << '\n';
    cin >> mark;
    mark = decimal(mark);
}

And call it in main() like this:

GetMark("T1:", a[n].vert.t1);
          GetMark("T2:", a[n].vert.t2);
          GetMark("T3:", a[n].vert.t3);
          GetMark("T4:", a[n].vert.t4);
          GetMark("T5:", a[n].vert.t5);
          GetMark("T6:", a[n].vert.t6;
          GetMark("T7:", a[n].vert.t7);

2) Because your program does not validate input. The decimal() function is broken when I enter a value > 99 or < 10. There are standard floating point functions to extract the decimal value of a float. This is a list of all the standard C/C++ math functions.

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

Your text book should be able to answer that question. Yes, programs can have an unlimited number of functions which can be called from other functions such as main(). What your program is missing is the function prototype that needs to appear before main(). Just copy/paste line 33 somewhere above main() and replace the { with a semicolon ;

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

>>Can someone please help me with this?
Can we do what to help you ? Post the code you have written, compiler's error messages, and ask questions. We are not going to write the program for you.

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


how else would the condition of the for loop work ?

Just check for end-of-string null terminator for(int i = 0; string[i] != '\0'; i++)

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

Post code or a link to the code.

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

>>The you can just concat title with first name.

That was my objection. Those two can not be joined.

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

The you can just concat title with first name. Then the result of that
gets concated with the last name. Then finally display the final result.

Wrong answer. Those strings don't have enough space allocated to concatenate them (see original post). Each of those strings need to be copied into some buffer that is large enough to hold all the strings at one time.

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

You don't need to pass the length of each of those strings. The concat function can easily find the lengths itself.

Here is how to make concat() return the string

string concat(const char* title, const char* fname, const char* lname)
{
    string fullname;
    // concat all three names not shown here

   // now return the result
   return fullname;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have 64-bit HP computer, 5 Gig RAM and 750 Gig HD. At first I tried to install 32-bit Windows 7, but it had all sorts of problems because I had too much RAM and too large a HD, so I wound up erasing that and starting all over with 64-bit Windows 7.