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

line 59: use index variable instead of numstudents. in>>person[index].studentFName>>person[index].studentLName>>

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

>>From what I understand from MSDN, dllexport is supposed to substitute the export section in the .DEF file -

Your understanding is incorrect. What MSDN means is that you can export the function in one of two ways:

  • use the __dllspec( __dllexport ) tags
  • add an entry into the .DEF file

The compiler does not make any entries into the .DEF file for you -- you have to do that yourself if you want them there.

Prototyping the function as you did in a.cpp is not sufficient. You have to add dllexport in the function itself

a.cpp

__dllspec( __dllexport ) void a()
{
   // blabla
}

You can also export an entire c++ class

__dllspec( __dllexport) class MyClass
{
   // blabla
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no standard way to do that in C or C++ because the languages don't know a thing about the function or arrow keys. But there are work-arounds and they all depend on your compiler and operating system.

One way is to use the curses library, or pdcurses on MS-Windows platforms. Its a little complicated and I'm not all that familiar with it.

Probably the easiest work-around is to use conio.h, if your compiler supports it. Below is a little program that illustrates one way to get function and arrow keys using getche(). When a special key is pressed you have to call getche() twice because the first time getche() returns 0 and the second time it returns the keycode for the key that was pressed. Special keys use the same key code as all other normal keys so you need to do something that will make them unique. I like to make them negative, but you could also just add 256 to them. Note this is the complexity of the MS-Windows operating system, not of the getche() function.

Run this program yourself and it will tell you the value of the key that was pressed, then you can use that value in your program for F1, F2, etc.

#include <iostream>
#include <conio.h>
using namespace std;
#pragma warning(disable:  4996)

int main()
{
    int x = 0;
    x = getche();
    if(x == 0)
    {
        // function and arrow keys here
        // They are made negative values …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is AnsiScring ? Why are you not using normal std::string ? If AnsiString is something you created yourself then you need to write an overloaded >> operator for it so that you can use it with fstream. If you can't do that then use std::stream and assign AnsiString to it. You could also use c style character arrays.

Also -- remove the semicolons at the end of each of those lines.

friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
      std::string vardas;
      std::string pavarde;
      std::string adresas;
        fs >> vardas   // <<<<< remove this semicolon
           >> pavarde   // <<<<< remove this semicolon
           >> adresas   // <<<<< remove this semicolon

           >> obj.gydid  // <<<<< remove this semicolon
           >> obj.amzius  // <<<<< remove this semicolon
           >> obj.specialyb  // <<<<< remove this semicolon
           >> obj.telefonas  // <<<<< remove this semicolon
           >> obj.asmkod;
      // reading code

      obj.vardas = vardas;
      obj.pavarde = pavarde;
      obj.adresas = adresas;
      
      return fs;
     };
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>1) I do not know how to use other style.
Yes you do because I just showed you. And you used it correctly in the code you posted!


>>Why does the DLL did not get other values of the array??? I do not understand that.
I don't know either because I need to see what the calling function sent.

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

Welcome to DaniWeb. This discussion was moved to VB.NET technical board. Continue it there instead of here.

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

You need to be careful about transferring files from MS-Windows platform to *nix or other platforms. MS-Windows uses two bytes to indicate end-of-line while *nix, MAC and others use only one character. So if you read text files in binary mode and send that to other operating systems the receiving program has to translate the CR/LF pair to whatever the target os expects.

File transfer programs, the receiving program does the translations because the sending program may or may not know what platform it is communicating with. So, the receiving program on *nix platforms need to translate 0x0a 0x0d pair to just 0x0a and on MAC they would be translated to 0x0d (unless I have that backwards).

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

Do you know how to create a class? Lets start with the easiest one -- date

class date
{


};

Next you want to add a few data objects, such as day, month and year

class date
{
private:
    int day;
    int month;
    int year;


};

Next, add the methods that will make the class do something. First create a constructor, which is a method that will initialize the class objects to 0

class date
{
private:
    int day;
    int month;
    int year;
public:
    // class constructor
    date();

};

Then add more methods as needed.

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

lines 21-30. The operator >>. When inputting std::string you don't use the c_str() method. There's how to code that

friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
        fs >> obj.vardas;
           >> obj.pavarde;
           >> obj.adresas;
           >> obj.gydid;
           >> obj.amzius;
           >> obj.specialyb;
           >> obj.telefonas;
           >> obj.asmkod;
      // reading code

      return fs;
     };
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The loops that read the data are also coded wrong -- there is no need for using eof() at the top of the loop. Here is a better and more accurate way to code those loops

while(n < MAX && inf1 >> names[n].id >> names[n].lname >> names[n].fname)
  {
      fixstring (names[n].lname);
      fixstring (names[n].fname);
      n++;
  }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The best way (I find) of troubleshooting is to just stick a whole bunch of print statements in your code ( printf()...or I guess you could use cout no difference).

The best was to do it is to learn how to use your compiler's debugger so that you can easily see that the program is doing and the value of all variables at any given time.

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

The infinit loop is here: for (int hw_r = 0; hw_r < n; n ++) That should be hw_r++, not n++. That's an easy mistake to make, I think we have all done that at one time or another.

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

Yes, writing GUI in c++ is pretty difficult stuff. Other languages such as python, vb, and C# are better suited for that.

I don't think you can process mouse events like that in console programs. The program needs a message pump in order to get and process windows event messages, and console programs don't have that capability.

Read this about windows console functions

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

line 87: array elements are numberd from 0 to but not includeing the number of elements in the array. So that loop should be coded like this:
for (int hw_c = 0; hw_c < MAX_HW; hw_c ++)

line 89: that's initializing the wrong array. It should be initialzing array hw.

I'll need the two text file in order to actually test your code.

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

Yup, that's how to do it.

VernonDozier commented: Good answer +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1) Stop coding in ancient K&R original style. That style has not been used for at least 20 years that I know of.

__declspec(dllexport) void simuser (double t,double delt,double* in,double* out)
{

}

2) I don't see in the main() that you posted where simuser() is getting called.

static double y[21];              //controlled variable
    static double m_A[21];         //modulation index

why are those declared static in main() ? That makes no sense. Surly can't be to minimize stack usage because with VC++ 2008 Express and most other 32-bit compilers those arrays won't take up a very noticeable amount of stack space.

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

Oh yes, here the problem if(student[j].lastname[0] > student[j+1].lastname[0]) change it to this and it will work if(student[j].lastname > student[j+1].lastname) Actually I would think you need to sort by both last name and first name, so that when two or more people have the same last name the function will sort those by first name.

void alphasort(if1 student[], int n)
//the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of 
//names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
{
    if1 temp;//used as a swapping mechanism
    int i; int j;// used for implementing for loop checks
    int f=1;//used for checking letters after the first
    for (i=0; i < n-1; i++)
    {
        for (j=0; j < n-(i+1); j++)
        {
            string n1 = student[j].lastname;
            string n2 = student[j+1].lastname;
            if(n1 == n2)
            {
                n1 = student[j].firstname;
                n2 = student[j+1].firstname;
            }
            if(n1 > n2)
            {
                temp = student[j];
                student[j] = student[j+1];
                student[j+1] = temp;
            }
        }
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The program crashes during sort because it is attempting to reference non-existant index value. I simplified alphasort() algorithm as shown below and everything works ok.

struct if1
{
  string idnum;//id number for the student
  string lastname;//last name of student
  string firstname;//first name of student
  int examscore[maxexam];//array of all the exam scores of the student
  int hwscore[maxhw];//array of all the home work scores of the student

  void operator=(if1& f1)
  {
      idnum = f1.idnum;
      lastname = f1.lastname;
      firstname = f1.firstname;
      memcpy(examscore, f1.examscore, sizeof(examscore));
      memcpy(hwscore,f1.hwscore,sizeof(hwscore));
  }
};

...
...
<snip>
...

void alphasort(if1 student[], int n)
//the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of 
//names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
{
    if1 temp;//used as a swapping mechanism
    int i; int j;// used for implementing for loop checks
    int f=1;//used for checking letters after the first
    for (i=0; i < n-1; i++)
    {
        for (j=0; j < n-(i+1); j++)
        {
            if(student[j].lastname[0] > student[j+1].lastname[0])
            {
                temp = student[j];
                student[j] = student[j+1];
                student[j+1] = temp;
            }
        }
    }
}

Attached is the output file it produced

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

Two suggestions:

1) Learn to write better English. Practice makes perfect.

2) Post your resume in the Post Your Resume board.

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

Would you also post the input files? Just attach them to your post .

Here is what I had in mind for the structure, but I need the input files for testing.

struct if1
{
  string idnum;//id number for the student
  string lastname;//last name of student
  string firstname;//first name of student
  int examscore[maxexam];//array of all the exam scores of the student
  int hwscore[maxhw];//array of all the home work scores of the student

  void operator=(if1& f1)
  {
      idnum = f1.idnum;
      lastname = f1.lastname;
      firstname = f1.firstname;
      memcpy(examscore, f1.examscore, sizeof(examscore));
      memcpy(hwscore,f1.hwscore,sizeof(hwscore));
  }
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

please post your code. Its kind of hard for me to see your monitor from where I am sitting.

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

What I'm doing is writing my own version of the = operator. So when you way temp = student[j]; the compiler will call my version of the = operator instead of the compiler's default version. The parameter to the operator is the equilivent of passing student[j] by reference to any normal function.

I believe the reason your sort does not work as you wrote it is because the structure contains c++ STL class -- std::string. In such cases when the structure includes other class objects you should write the overloaded = operator.

That might also be similar to writing a swap function

void swap(if1& f1, if1& f2)
{
    if1 temp;
    temp.idnum = f1.idnum;
    temp.lastname = f1.lastname;
    temp.firstname = f1.firstname;
    temp.examscore = f1.examscore;
   temp.hwscore = f1.hwscore;

   f1.idnum = f2.idnum;
   // etc for f1

   f2.idnum = temp.idnum;
   // etc for f2
}

As you can see, the above code is pretty lengthly. The overloaded = operator is consideratly shorter and less pron to typing errors.

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

That because its called the "stupid test". You have to be a real idot to finish it and respond to some of those adverts at the end. Once they have you on their email list you will never get off.

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

structs are nearly identical to classes, so maybe you need to add an = operator

struct if1
{
  string idnum;//id number for the student
  string lastname;//last name of student
  string firstname;//first name of student
  int examscore[maxexam];//array of all the exam scores of the student
  int hwscore[maxhw];//array of all the home work scores of the student

  void operator=(if1& f1)
  {
       idnum = f1.idnum;
       // etc for the rest of the structure members

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

>>I have taken if(infile.is_open()) away as seen because when having this I received the size of: 0. I am not sure if this was okay to do ?

Don't do that. If is_open() fails that means the file could not be opened for some reason. And in that case all other fstream functions will fail too. You need to find out why the file can not be opened.

>>Getsize2 = Getsize.str();
You don't need Getsize2 string. String^ sizeee = gcnew String(GetSize.str());

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

1) file size is an unsigned integer not a double.

2) >> double filesize = path.size();
All that is doing is getting the number of characters in the string, not the file size

Here is how to get the file size

size_t fileSize = 0;
string path = "C:\\Folder1\\Folder2\\OneFile.txt";
ifstream infile(path.c_str());
if(infile.is_open())
{
    infile.seekg(0, ios::end ); // move to end of file
    fileSize = infile.tellg();
}
cout << "file size is " << fileSize << "\n";

There are a couple other ways to get it, such as calling the stat() function.

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

I don't need no stinking "stupid test" to prove I'm stupid :)

majestic0110 commented: hehe! +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think the only people who write in assembly to day are either 1) compiler writers, or 2) program EPROMs.

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

i learned assembly years ago as my first language (not counting BASIC, Pascal and Fortran, which i never did anything meaningful with)...

That is a big contridictory sentence :) Assembly could not have been your first language if you had learned others prior to that.

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

Are you talking about writing a plug-in for Windows Explorer ? When you right-click a file in Explorer it will add to its popup menu item(s) that you write ?

I have seen something like that I think at www.codeproject.com, but don't recall how it is done.

[edit]Ok I think I found what I had in mind -- CodeProject: The Complete Idiot's Guide to Writing Shell Extensions - Part I

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

Is b.cpp the application program that is calling the exported function in a.cpp, which is in a dll ?

In b.cpp you need to declare the function as _dllimport.

// b.cpp is the application program
//
_delspec(_dllimport) void a();

void foo()
{
    a();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I'm not sure how to play this game but here are my answers

Choose one
1. Make a move
2. Save the game
3. Quit
1
Player 1 Would you like to move piece A, B, C, or D?
a
Please choose a move
 1 2
3   4
  X
5   6
 7 8
1 <<<<<<<<<<<<<< This is what I entered

In the function appropriatemove(), the first loop is looking for the the value of piece in the board. The first time through the board is empty, so it will not be found. Therefore the value of row is 0.

Next, when row == 0, the function does a switch on move which is 1. Following the math if (row - 2 >= 0 && row - 2 <= 9 Since row is 0, the above check fails.

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

Here is an explaination of how I use it. I always use it with __declspec and don't have a problem. It does not affect the .DEF file -- actually the DEF file is not even needed when dllexport is used.

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

>> i didn't know you could do that with structures i thought only classes!
In c++ structures are almost identical to classes.

>>temp->pdata->bookTitle = title;
You forgot to allocate pdata too. temp->pdata = new data;

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

Here is a working example of one way to sort a linked list. It adds 5 nodes to the linked list then sorts the list based on book title.

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

struct data
{
   string bookTitle;
   string *authors;
   int nAuthors;

   data()
   {
       authors = 0;
       nAuthors = 0;
   }
   ~data()
   {
       if(authors)
           delete[] authors;
   }
};


struct node
{
    struct data* pData;
    node *next;

    node()
    {
        next = 0;
        pData = 0;
    }
    ~node()
    {
        if(pData)
            delete[] pData;
    }
};
          
node *head = NULL;

node* AddHead(data* pData)
{
    node* newNode = new node;
    newNode->pData = pData;
    newNode->next = head;
    head = newNode;
    return newNode;
}

void SortList()
// This just uses the standard bubble sort.
{
    node* i;
    node* j;
    for(i = head; i->next != NULL; i = i->next)
    {
        for(j = i->next; j != NULL; j = j->next )
        {
            if( i->pData->bookTitle > j->pData->bookTitle)
            {
                data* tmp = i->pData;
                i->pData = j->pData;
                j->pData = tmp;
            }
         }
    }
}


int main()  
{
    string names[5] = { "James", "King", "Arnold","Bellairs","Johnson" };
    struct data* pData;
    // add 5 nodes to the linked list
    for(int i = 0; i < 5; i++)
    {
        pData = new data;
        pData->bookTitle = names[i];
        AddHead(pData);
    }
    // sort them by book title
    SortList();
    // print them to the console screen
    node* curr = head;
    while(curr)
    {
        cout << curr->pData->bookTitle << "\n";
        curr = curr->next;
    }
    // delete the linked list
    curr = head;
    while(curr)
    {
        node* tmp = curr;
        curr = curr->next;
        delete tmp;
    }
    head = NULL;    
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok, so now what's the problem ? I forgot. Oh yea, I remember now -- how to sort a linked list. If you know how to sort a normal array then its not all that hard to sort the linked list. With the new node class I posted all you have to swap is pData pointers.

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

>>we cant use anything from STL
Ok, in that case you can't use std::string objects either.

struct data
{
   char* bookTitle;
   char **authors;
  int nAuthors;
 };
struct node
{
    struct data* pData;
    node *next;
};
          
node *start_ptr
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The nodes might be easier to sort if you put the data in another structure

struct data
{
   string bookTitle;
   vector<string> authors;
};
struct node
{
    struct data* pData;
    node *next;
};
          
node *start_ptr

Now when a swap needs to take place all you have to do is swap the data pointers and leave all the node pointers alone.

Also, don't use a pointer to a std::string object. It doesn't save you a thing, and actually causes more trouble than its worth. If you want an array of authors then use a vector. And if you use vectors you don't need int nAuthors; because the vector class keeps track of that value.

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

Look at the format specifiers here and you will find the one you want.

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

See the Read Me Starting "C" at the top of this board. You posted right over it.

Its good that you are studying assembly language but I would not have picked it as my first programming language because assembly is not considered a good way to program. That is, the language is chucked full of jumps and gotos. You will have to unlearn all that in order to code effectively in any of the higher-level languages. In other words, assembly language teaches you a lot of bad habbits.

The second reason not to learn assembly as a first language is that there are very few people who actually program in that language any more. Most programmers code for years without even seeing any assembly language code.

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

I think it opens the file, gets the file length, allocates a buffer if the required size, then checks if any character elements is > 126, which is the upper limit of the text characters in the standard ascii chart.

So I would think this would work. Note: the following is untested and not compiled.

bool IsTextFile(string FileName)
{
    bool isText = true;
   // open the file in binary mode
    ifstream in( FileName , ios::binary);
  if( in.is_open() )
  {
      size_t len;
      // locate end of file
      in.seekg(0, ios::end);
      // get file length
     len = in.tellg();
     // back to beginning of the file
     in.seekg(0,ios::begin);
    // allocate buffer space
     unsigned char* buf = new [size+1];
    // read the file into memory
    in.read(static_cast<char*>(buf), size);
    in.close();

    // now check if its a text file
    for(size_t i = 0; i < size && isTest == true; i++)
   {
        if( buf[i] < 13 ||  buf[i] > 126)
        {
            isText = false;
        }
    }
    delete[] buf;
    return isText;
}

There is another way to accomplish the same thing without allocating any memory. Just read the file one character at a time and test to see if it is a text character or not.

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

The School of Hard Knocks is within each of us. Its experience, and millions of hours testing various algorithms to find the most efficient.

Traicey commented: Oh Boy, Im speechless +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I Isnt that insertion sort?

No. This is insertion sort

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

Yes, the School Of Hard Knocks.

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

Correct me if I'm wrong but shouldn't this: for(int j = 0; i < n; j++) be: for(int j = 0; j< n; j++) Or else you will get out of array-boundary => crash?

Yes, and that's what I get for typing without testing.

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

Yaeh!!!! I have seen all that maybe I just dont want to understand it but honestly I would be lieying I I say I understand all o that, I just wish to know why for (i=0; i<n-1; i++) {for (j=0; j<n-1-i; j++) are we substracting 1 and - 1 - i what does that mean, if Dragon U can be kind enough to explain that to me, I would go home and rest

The reason the first loop only goes to n-1 is because it isn't necessary to compare a[n-1] with a[n-1], where i and j both equal n-1. Bubble sort is the easiest algorithm to code, but also the slowest (normally). So anything that will reduce the number of comparisons will improve performance.

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

sorry, the second loop was incorrect. for(int j = i+1; j < n; j++) The difference between my first two algorithms is the differences between counting up or counting down. I prefer to count up because it seems more natural.

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

The bubble sort is probably the very easiest sort algorithm there is. What is so difficult to understand about this:

for (i=0; i<n-1; i++) {
  for (j=0; j<n-1-i; j++)
    if (a[j+1] < a[j]) {  /* compare the two neighbors */
      tmp = a[j];         /* swap a[j] and a[j+1]      */
      a[j] = a[j+1];
      a[j+1] = tmp;
  }
}

I don't really use the above algorithm, but this one

for(int i = 0; i < n-1; i++)
{
    for(int j = 0; i < n; j++)
    {
          if( a[i] < a[j] )
          {
               int temp = a[i];
               a[i] = a[j];
               a[j] = temp;
           }
       }
}

And in C++ you can do it in just one line: But I doubt this is a satisfactory solution to your assignment.

vector<int> array;
// fill array is not shown
//
// now sort it
std::sort(array.begin(),array.end());
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Took about 10 seconds to find the answer here Here

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

Passing by reference only means to pass a pointer, and all arrays are always passed by reference, never by value.

It is not necessary to pass a pointer to a pointer. Passing by reference means to pass a pointer, and String is already a pointer. All that function needs is a pointer to the allocated memory where to do the concantination. Its not necessary to change the value of the original pointer that was allocated in main().

void string_concat( char *string )  {
    
    //write in string something, need a realloc?
    strcat(string, something);    

}