Clockowl 56 Posting Whiz
const int MAX = 13;
int a[MAX];
for (int i = 0; i < MAX; ++i) a[i] = i;
for (int i = 0; i < MAX; ++i) {
    int r = rand() % MAX;
    while(r == i) r = rand() % MAX;
    int t = a[i]; a[i] = a[r]; a[r] = t;
}

That oughta take care of the first thing. ;D Shuffling the same elements twice is something that has in chance in real life as well, so it's part of being random. :)

Clockowl 56 Posting Whiz

Okay, not 100% right. Darnit. xD

I've just created a single multithreaded program and it was (a dreaded task and) crashing constantly! So I ended up creating mutexes for every "group" of shared variables.

Clockowl 56 Posting Whiz

"Set pass/fail" I guess? Maybe the int is the subject indicator? Ugh, the variable names are horribly chosen. Just use that int as subject index and the char as.. P or F or something? Or grades? I dunno.

Clockowl 56 Posting Whiz

MrSpigot was 100% right, my bad. You don't need mutexes to write and read from one variable at the same time.

Tested with:

#include <iostream>
#include <windows.h>
using namespace std;

int victim = 10;

void thread()
{
    for(int n = 0; n < 99999999; n++){
        victim = n;
    }
}

int main()
{

    HANDLE thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);
    HANDLE thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);
    HANDLE thread3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);

    WaitForSingleObject(thread1, INFINITE);
    WaitForSingleObject(thread2, INFINITE);
    WaitForSingleObject(thread3, INFINITE);
    return 0;
}
Clockowl 56 Posting Whiz

Sorting any type of data with use of the STL:

http://www.cplusplus.com/reference/algorithm/sort/

Which may be a bit too much for 3 strings, but you'll know how to sort 2000 too! :D

Clockowl 56 Posting Whiz

If your coding C, why post in the C++ forum?

Anyway, show some code.

A quick google gives me this:

http://weseetips.com/2008/08/27/how-to-set-color-for-static-control-text-in-dialog/

and

http://www.programmersheaven.com/mb/mfc_coding/147975/147975/how-do-you-change-static-text-box-text-color/?S=B20000

Don't know if they are addressing your problem.

Clockowl 56 Posting Whiz

It's a slash in (/code), not a backslash. But A for effort. :D
And you can supply the language as well, (code=cpp)

Your code looks okay, but it looks a lot like C: it's not using OOP. Which would be a nice approach for a board game.

My output looks a bit different too:

||=== TMP, Debug ===|
main.cpp||In function `bool emptySquare()':|
main.cpp|40|error: `square' was not declared in this scope|
main.cpp|40|warning: unused variable 'square'|
main.cpp||In function `void gameDisplay()':|
main.cpp|69|error: `square' was not declared in this scope|
main.cpp|69|warning: unused variable 'square'|
main.cpp||In function `bool illegalMove1()':|
main.cpp|74|error: `square' was not declared in this scope|
main.cpp||In function `bool illegalMove2()':|
main.cpp|79|error: `square' was not declared in this scope|
main.cpp||In function `void Move_Player1()':|
main.cpp|86|error: `square' was not declared in this scope|
main.cpp||In function `void Move_Player2()':|
main.cpp|118|error: `square' was not declared in this scope|
||=== Build finished: 6 errors, 2 warnings ===|
Clockowl 56 Posting Whiz

main() is just reading it, so no protection is necessary in this case.

That's wrong, almost 100% sure, couldn't make sure with google, but it's just 2 threads trying to access the same memory: doesn't matter if they read or write, both will lead to errors.

Agreed on not using Sleep(). It's a useless function: wait for events to happen, don't guess how long such and such is going to take, and put that time in Sleep().

Clockowl 56 Posting Whiz

Nullsoft installer. Kicks ass!

Clockowl 56 Posting Whiz

Sure, it looks fine, but you need to write it yourself if you want to learn C++, not somebody else.

I mean, do you know what this &*(array + k) does? (It looks rather redundant...)

Clockowl 56 Posting Whiz

What doesn't work?

new doesn't set the allocated memory to 0, could that be a problem?

Be a bit more specific please.

Clockowl 56 Posting Whiz

Give it a shot here: http://www.cplusplus.com/doc/tutorial/

When you're stuck on a tutorial, you can ask for help of course.

Clockowl 56 Posting Whiz

Yeey self reply!

Calling abort() shows that message, exit() does not. Thanks Nick! You're welcome! :D:D:D:D

Clockowl 56 Posting Whiz

Hey guys,

How do I disable/get rid of that default exception message windows shows when you have an "unhandled" exception in your program?

Here's my example code (sorry for not limiting the code's size to the problem only, it's readable enough I think):

#include <iostream>
#include <fstream>
#include <string>
#include <new>

/* load_file(filepath, destination)

   -Returns whether it succeed or not. True meaning succeeded.
   -Takes a path and writes to a std::string, completely memory safe.
*/

bool load_file(const char *filepath, std::string &dest)
throw (std::bad_alloc) {
    using namespace std;

    ifstream file;
    file.open(filepath, ios::binary);
    if (!file.good()) {
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
    //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if (!file.good()) {
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
    //allocate array, read file in array, set the term. null byte, set string to array

    char *file_content;
    try {
        file_content = new char [file_size+1];
    } catch (bad_alloc &memException) {
        cerr << "Probably went out of memory while allocating " << file_size+1 << " bytes!" << endl;
        file.close();
        throw;
    }

    file.read(file_content, file_size);
    file_content[file_size] = '\0';
    dest = file_content;

    //clean up
    //close file, free array
    file.close();
    delete[] file_content;

    return true;
}

void custom_terminate() {
    std::cerr << "--Aye, unrecoverable exception thrown!\n"
    << "Try doing what the messages say (if anything) and run the program again or ask someone with a bit more expertise to …
Clockowl 56 Posting Whiz

Meh, it's simply the null byte. new doesn't set the whole array to 0 like calloc() does. Thanks. :)

Working code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

/* load_file(filepath, destination)

   -Returns whether it succeed or not. True meaning succeeded.
   -Takes a path and writes to a std::string, completely memory safe.
*/

bool load_file(const char *filepath, std::string &dest){
    using namespace std;

    ifstream file;
    file.open(filepath, ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set the term. null byte, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    file_content[file_size] = '\0';
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    using namespace std;

    string file_content;
    load_file("main.cpp", file_content);
    cout << file_content;

    return 0;
}

Feel free to grab the function and use it.

Clockowl 56 Posting Whiz

Haha, no, that function of course needs to know what subject to set. Change it a bit, add a comment for your prof? The other functions do have such a parameter, so maybe he forgot?

Clockowl 56 Posting Whiz

I don't want to output the whole file, I want to have the whole file in memory.

And, I used that so I can use the char array as a null-terminated string. That was the point... The file is not guaranteed to end in a null byte, is it?

Say the file is 200 bytes, I need to allocate 201 bytes, 200 for the file, 1 for the terminating null byte.

Clockowl 56 Posting Whiz
// set the subject grade (pass or fail)         
void Student::setSpf(char score,int grade)
{
}

How does that function know what subject it is supposed to work on?

Clockowl 56 Posting Whiz

Doesn't new set the allocated memory to 0, like C's calloc() ?

Clockowl 56 Posting Whiz

It's a memory error (from the looks of it), presumably reading over EOF or forgetting the terminating null byte in the char array. If it's working for you, try adjusting the file it's reading a bit (add a comment or something).

But, I don't see where I'm doing something wrong, hahaha. :(

Clockowl 56 Posting Whiz

Ancient, even then it's wrong, file is a char pointer.

void loadText(list<char> &listId, char *file) //<----
{
   ifstream fin;
   fin.open(file);
   char mystring[100];
   
   while(fin >> mystring)
   {
       fgets (mystring, 100, file); //<-- fgets(char *, 100, char*)? xD
   }
   fin.close();
}
Clockowl 56 Posting Whiz

Hey guys,

I've this code:

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

bool load_file(const string &filepath, string &dest){
    ifstream file;
    file.open(filepath.c_str(), ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    cout << file_content;
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    string file_content;
    load_file("main.cpp", file_content);
    //cout << file_content;

    return 0;
}

And it should read itself, but the output is slightly different:

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

bool load_file(const string &filepath, string &dest){
    ifstream file;
    file.open(filepath.c_str(), ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    cout << file_content;
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    string …
Clockowl 56 Posting Whiz

Create an array/vector of the struct (or pointers to), iterate it, compare the member variables to what you are looking for.

Clockowl 56 Posting Whiz

char *file?

If you're using fgets(), I'm guessing it wants a C-style FILE *. ;)

Anyway, you're in C++ land now, try getline() or something like that. Safer, easier.

Other then that, the code is kinda.. bad..

You've a loop that fetches data from the ifstream into your char array, which is okay, but why do you want to read into that char array again in the loop?

Clockowl 56 Posting Whiz
void Employee::set(double payRate, int hours)
{
 cin >> payRate;
 cin >> hours;
}

Change that to...

void Employee::set(double payRate, int hours)
{
  this->payRate = payRate;
  this->hours = hours;
}

I'm not guarranteeing that it works on every compiler, but I think it should. If you don't understand the code, let me know.

Also, move your globals

double payRate;
int hours;

into your main:

int main()
{
 double payRate;
 int hours;
 Employee wage;
 cout << "What is the pay rate:";
 cin >> payRate;
 cout << "How many hours worked:";
 cin >> hours;
 wage.set(payRate, hours);
 wage.display();
}
Clockowl 56 Posting Whiz

xD Okay, sorry. I'd suggest sqrt() nonetheless, but it doesn't really matter.

Clockowl 56 Posting Whiz

To find the square root, you need to #include <cmath> and use the pow() function.http://www.cppreference.com/wiki/c/math/pow

He means the sqrt() function.

Clockowl 56 Posting Whiz

And, not related to programming, you're squaring it, not square rooting it.

Clockowl 56 Posting Whiz

This is not C, I have no clue what language this is, sorry.

Clockowl 56 Posting Whiz

You're better of posting this in the C forum, but meh.

The function takes 6 char pointers, pointing to a block of chars terminated by '\0' I presume. You need to separate 1 string into 5 others, guess what the other 5 are for?

char here[7];
char a[2];
char hint[5];

dissectCustLine("Here's a hint", here, a, hint);
Clockowl 56 Posting Whiz

While the current node's item is smaller than A and the current item isn't NULL (and thus we are at the end of the list), go to the next node.

Clockowl 56 Posting Whiz
while ( (Curr->Item < A) and (Curr->Succ != NULL) )
   {
      Prev = Curr;
      Curr = Curr->Succ;
      cout << Curr->Item;
   }

That code sets Curr to the last item in the set that's smaller than A.
And then you compare the last item (curr->item) to A (to guarantee uniqueness) or NULL for the last item. And there's your problem I think, hehe.

What if Curr is NULL and you try to dereference Curr->item? Segfault. I hope that's the error.

So, correct code would be something like..

if(Curr == NULL){
//create new item, append it to the set
} else if (Curr->Item != A) {
//create new item, insert it between the two items.
}
Clockowl 56 Posting Whiz

Scan the set until you find a value higher or lower (depends on how you sort) then the current value, insert it there?

Clockowl 56 Posting Whiz

This is a C++ forum, ask questions about C++. If you want to talk about software design in general (for instance, what merits your script will have compared to the other scripts already available), go to the Computer Science forum.. I guess.

Anyway, first define your script on paper. How will scripters create variables? How will scripters add them together?

Then write an interpreter that picks a file (or lines anyway) and parses them.

For instance, if you decided that people create integers with "int <name> = <val>;", you can decide to create a map<string, int> which will hold the users and then add one int when such a line is read.

Clockowl 56 Posting Whiz

So, you know what you have to do, do it. :)

class invoice { 
... 
};

Something like that.

Clockowl 56 Posting Whiz

Eh yeah, ArkM's right in every way. Ignore my previous post.

Clockowl 56 Posting Whiz

I know you like ambitious projects:

I am going to make OS. And I need help with GUI...how to make an GUI ?...I am not on Windows so CreateWindowEx() doesn't exist.

But I suggest starting of with the easy stuff, hehe. :D

Clockowl 56 Posting Whiz

I suggest you use ifstream: more people know ifstream on this forum.

What problem did you have with getline()?

Clockowl 56 Posting Whiz

Why don't you reply to that thread then? :?

Clockowl 56 Posting Whiz

I suggest you get rid of the threads anyway: the problem is not that it's not multithreaded. Other than that I suggest sticking to either MFC functions or C++ functions. I'm not sure if "ifstream" works in MFC, but you could try since you also try to use "ofstream".

Clockowl 56 Posting Whiz

errr.. what? Is this a question or.. an answer to no question? I don't get it.

Clockowl 56 Posting Whiz

singleIMEI singleIMEIarr[30] = {'\0'}; That's a NULL char. ;) By definition arrays in C NOT dynamically allocated are always completely 0.

Clockowl 56 Posting Whiz

Post the (relevant) sourcecode? :)

Clockowl 56 Posting Whiz

Could you try this simple program and tell me if it has any errors?

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

struct cluster {
    string name;
};

int main() {
    vector <cluster> clusters (3);

    clusters[0].name = "test";

    cout << clusters[0].name << endl;

    string overwrite("OH YEAH!");

    clusters[0].name = overwrite;

    cout << clusters[0].name << endl;
    return 0;
}

This is a program to narrow down the problem: Is it really the assignment that's causing errors? If it is, this program should also throw a segfault. We'll see. :)

Clockowl 56 Posting Whiz

File Dump? Clarify yourself a bit, maybe with a small program?

Clockowl 56 Posting Whiz

Well lionleo, welcome to Daniweb: where we don't code homework assignments for others. :D

What's the problem? Do you know how to read in a file? Grab lines from that file? Parse those lines?

Clockowl 56 Posting Whiz

Errr, that's probably because you included stdafx.. *-) You're working in MSVC++ I presume? If so, put your includes inside that include (stdafx.h), if they aren't there already. ;)

Clockowl 56 Posting Whiz

Well said! That's indeed the difference between them. Pure virtual functions are just there in the base class when you want to be able to dynamically look it up in derived classes, but don't have an implementation for it in the base: it MUST be defined in the derived classes! That's all I reckon.

Edit: Wiki has a good example:

As an example, an abstract base class "MathSymbol" may provide a pure virtual function doOperation(), and derived classes "Plus" and "Minus" implement doOperation() to provide concrete implementations. Implementing doOperation() would not make sense in the "MathSymbol" class as "MathSymbol" is an abstract concept whose behaviour is defined solely for each given kind (subclass) of "MathSymbol". Similarly, a given subclass of "MathSymbol" would not be complete without an implementation of doOperation().

So, split up those files, clean up the code a bit and restate your problem please.

Clockowl 56 Posting Whiz

Could I scrap the 'damage' as a virtual function, and replace it with 'reload' as a virtual func?

Decide that for yourself, do you know the difference between a virtual function and a non-virtual function? It's quite an advanced subject.

Clockowl 56 Posting Whiz

...