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

Here is a good tutorial about structures in assembly programs

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

did you try something like this?

data db 1024 * size node

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

>>i dnno how to insert in without using an array

Please explain more exactly. Insert a value into the array?

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

>>I am not intended to use "pass the vector by reference" because i am comparing the value of each element to find out the highest frequency count.

That's ok -- it still should be passed by reference to avoid having to duplicate the entire vector. If you had a vector with a million integers in it duplicating that vector would be very very time consuming as well as a lot of unnecessary extra memory. IMO c++ objects should almost never be passed by value.

Use the const keyword to prevent the functtion from changing it.

int highest_range(const vector<int>& match, int& index)
{
    int highest = match[0];
    index = 0;
    int sz = match.size();   
    for (int j=1;j<sz;j++)
    {
         if (match[j]>highest)
         {
            highest=match[j];
            index = j;
         }   
    }
    return highest;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 10: int big=match[0];
That won't work because its declared as a global and vector match does not contain anything.

>>void highest_range(vector<int>match, string range [])

You should pass that vector by reference, not by value, so that the entire vector doesn't have to be duplicated. void highest_range(vector<int>& match, string range []) You must make that function return an int value instead of void (read your instructions) then move the declaration from line 10 (mentioned earlier) into this function. Also, there is no reason for it to have that second parameter

int highest_range(vector<int>& match)
{
    int big=match[0];

// rest of that function goes here

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

>>can you please tell me how to use it in a dialog box.......
Not here -- that topic will fill a book.

When the call button is clicked the program should get the currnt system date/time and save it in a variable. When the end call is clicked the program gets the current date/time and save it in another variable. Then just get the difference and your problem is solved. I would just use the time functions in time.h

>>also if the user's balance is low or about to end, the call[ie the dialog box] should be closed, ie the "end call" button should be automatically activated

You might want to consider using an OnTimer() event handler for that

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

use an OnTimer() event handler. See SetTime() and KillTimer()

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

Probably something like this (untested) code

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


int main()
{
    std::vector<std::string>  elementVector;
    std::ifstream in("filename.dat");
    std::string line;
    while( std::getline(in, line) )
    {
        if( line == "<et>" )
        {
            std::getline(in,line);
            stringstream st;
            st << line;
            std::string word;
            while( std::getline(st,word,'\'' ))
            {
                size_t pos = word.find('\'');
                if( pos != string::npos )
                    word.erase(pos,1);
                elementVector.push_back(word);
            }

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

bool, true and false are c++ keywords. Just comment out that enum and it should work ok.

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

>>for (i = 0; i < n;++i){

What will happen if you only input 3 numbers? Answer: the final value of sum will be in the crapper because it will add in uninitialized array elements 4-20.

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

Another way to do it is to use Visual Basic Applications

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

Why not just use MS-Word or Open Office to modify the doc file?

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

to post the code just copy into the clipboard and paste it here in the editor. put [code] /* your code goes here [/code] code tags around the code, as in this example.

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

>>CC = /usr/bin/gcc

try removing the white space on that line

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

>> any ideas on why this would happen?

Yes, but its not worth posting until I see your code.

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

Post code because we can't see your monitor.

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

no, EOF is not a char, and you don't need it anyway, just while( fgets(...) ) will do the trick

>>is there a way to tell strtok to tokenize ignoring all non-letter characters?
Probably not. It might be easier to use a pointer and test each character with isalnumeric() which includes 'a'-'z', 'A'-'Z' and '0'-'9'. Just use isalpha() if you don't want numeric digits

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

>>but how can I keep reading it, line by line?

That's the purpose of the while statement that you posted. Every time fgets() is called it will read the next line in the file. The "!= NULL" says to keep reading until end-of-file is reaches or some other error occurs.

Yes, strtok() is one way (and probably the easiest) to extract the words.

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

Here is one solution, the same as used by many win32 api functions because it avoids the problems of allocating memory in DLLs.

void __stdcall date(char* dateStr, size_t size)
// dateStr is allocated by calling function
// size is the number of bytes allocted to dateStr
	
{
		 
    _strdate_s( dateStr, size); // fills char array with date
}

Or if you want the function to return something

char* __stdcall date(char* dateStr, size_t size)
// dateStr is allocated by calling function
// size is the number of bytes allocted to dateStr
	
{
		 
    _strdate_s( dateStr, size); // fills char array with date
    return dateStr;
}

Example calling function

int main()
{
   char dateStr[50];
   date(dateStr, sizeof(dateStr));
   cout << dateStr << '\n';
}
Suzie999 commented: Very helpful, exactly what this noob needed. +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I have fought with this for 10 hours and once again I'm so losing
the battle vs C++ char arrays and strings.

Because you must be too pigheaded and stubborn to listen to the advice I gave you in my previous post. You just wasted 10 hours for what? Nothing. Had you bothered to read my post 15 hours ago you would have discovered that what you are attempting to do can not be done.

I already told you that character arrays can not be returned from functions. Why? because the array is destroyed as soon as the function returns.

>>ss << dateStr[0];
That doesn't work becase all it does is put the first chararacter of dateStr into ss. If you want to put the entire string then do this: ss << dateStr; . But why are you using stringstream here? The code you posted does nothing with it so you might as well just delete it.

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

I agree. I don't recall ever seeing a drunk Ninga or a boozed-out rugby team play :)

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

character arrays like that can not be returned by any function, whether its in a dll or not makes no difference. You have several options

  1. Since this is c++ return std::string -- this may have the same problem as described below for #3
  2. add a char array parameter to that function void __stdcall date(LPSTR dateStr, size_t size)
  3. declare dateStr as a pointer and allocate memory for it. This is least desireable because the calling function has no way to delete the memory. Memory allocated in a dll must be deleted (or free'ed) in the same dll.

>>LPSTR string = "";

That declares a pointer which points to a character array of only 1 byte -- the NULL terminator.

>>string += dateStr;
This will not work with character arrays. All that it is doing is incrementing the pointer by dateStr number of bytes. If dateStr == 'A' then pointer is incremented by 65 bytes because 'A' has a decimal value of 65. Its the same as this: string += 65; That is clearly not what you intended.

Lines 7-11 do not seem to have a purpose anyway, so just delete them.

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

I don't follow golf -- afterall its no more a sport than computer programming or brain surgery. Anyone can play golf, whether sober or intoxicated; as long as your not passed out you can play the game.

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

1) Yes that is correct

2) Its not really hidden if you look around Eclipse options -- I don't use Eclipse so I do know. Eclipse probably generates a makefile that g++ uses.

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

or like this, where there is only one return statement

bool function(int a)
{
   bool rvalue = false;
   if(a == 2)
   {
      // do something
      rvalue = true;
   }
   else
   {
      // do something else
      rvalue = false;
   }
   return rvalue;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

When main.cpp is compiled the compiler generated main.obj (or main.o depending on the os). When custom.cpp is compiled the compiler generated custom.obj. The linker uses both main.obj and custom.obj, as well as other libraries, to create the executable program.

How does the linker know how to do that? It depends on the compiler and IDE you are using. With both Code::Blocks and VC++ 2010 you have to start out by creating a project then add both main.cpp and custom.cpp to that project. With olther command-line driven compilers you might have to create makefiles, which is a text file that tells the compiler and linker what to do.

No point in being any more specific until you tell us what compiler and operating system you are using.

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

You mean you want to sort the array without actually swapping array values? I have done that before but used a second array of intgers that represent the array indices.

float data[5] = {3,1,4,9,2};
int indices[5] = {0,1,2,3,4};

// sort the data
for(int i = 0; i < 4; i++)
{
  for(int j = i+1; j < 5; j++)
  {
     if( data[indices[i]] < data[indices[j]] )
     {
        int temp = indices[i];
        indices[i] = indices[j];
        indices[j] = temp;
     }
  }
}

// now display the values
for(int i = 0; i < 5; i++)
   printf("%f\n", data[indices[i]]);

This technique is a good way to sort large 2d arrays also because it greatly reduces the number of items to be swapped during sorting. The contents of the original array are unchanged.

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

should have been plrs.id = j;

>>As a quick fix I've changed it to plrs.id=i+1;
Well you changed it wrong. Your original code snippet in your first post was correct for that line.

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

A few problems:

1. There are more than 400 items in players.txt, so you need to increase the size of those two arrays

2. Add the ios::biary flag to the two binary files

3. When reading/writing binary arrays the arrays can be read/written all in one shot -- it is not necessary to write or read them one array element at a time. But sometimes it is more useful to read them one at a time if you don't know how many were written.

4. Move the class plr up above main() so that you can write other functins that use the same class. Almost no programmer will put the class definition within a function because it limits its scop to that one function. Not a very useful thing to do.

5. Instead of that array it would actually be better to use either std::vector or std::list so that you don't have to know how many items are in the files. Buth vector and list grow as you add items to them.

#include <fstream>

#include <iostream>

using namespace std;

  class plr
    {
    public:
      int id;
      int shirt_no;
      int age;
      int goals;
      int pos;//0-GK,1-DEF,2-MID,3-ATT
      char LName[51];
      char FName[51];
      int team;
      int skill;
      int goaltime;
      int cards;
      void transfer(int t_id)
      {
        team = t_id;
      }
      plr() 
      {
          id = shirt_no = age = goals = pos = 
              team = skill = goaltime = cards = 0;
          LName[0] = 0;
          FName[0] = 0;
      }
  };

int main() …
anantk commented: thanks +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

arrays are always, always indexed beginning from 0, never 1. So line 38 should be for (int i=0;i<401;i++) .

The index values on lines 41 and 42 should be using the loop counter i, not the value of j that was read from the file. What would your program do with a value when j == 500? (Hint: crash!) The value of j should the used as the player's ID, not the index value into the array. You should assume the player's ID has no relationship to the index value used in the array.

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

Post the changes you made.

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

Delete that do loop on line 152. Why would you want to continuously read the same file over, and over, and over forever???


change the loop starting on line 64 to this:

while( MySimpletron >> instructionRegister )
{
    memory[size] = instructionRegister;
    cout << setfill('0') << setw(4) << memory[size] << "    ";
    ++size;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That was a common problem with that compiler and the only fix is to reboot the computer. Unfortunately there is another common problem -- when you try to save a file the IDE thinks the file is already open by anothe program and can't save it. That can resut in complete loss of the program unless you pay attention to the errors and save it as a different file name before closing the IDE.

After some experiementation and talking to other programmers it seemed that I needed to turn off running antivirus programs, such as Norton Antivirus. That, at least solved the prblem on my computer which was running Windows XP. I have not experienced any of those problems with later versions of the compiler/IDE.

This is a good reason why you should upgrade your compiler to VC++ 2010. Version 6.0 is full of bugs.

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

download and use this dependency walker to find out what dlls and libraries your program needs

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

I don't know the answer to your question. But I am putting myself in into this for learning purpose.
I have not seen this function used in any c program that I have come across in my short experience with C. Can you brief about what exactly is the purpose of wide characters. I referred Google and now know what is wide characters but if you can give some examples where all this is used in real life, I would be grateful.

Wide characters wchar_t is used in UNICODE programs, which may or may not be English. Some character, such as Chinese, will not fit in one-byte char, so they use wchar_t. The sizeof(wchar_t) is not consistent among operating systems -- MS-Windows sizeof(wchar_t) == 2 while *nix its 4. So you have to be very careful about transferring unicode-witten files from one os to another.

sree_ec commented: Thanks... +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you are seeing the difference between %s and %S (not the capitalization os s and S).
with swprintf():
%s assumes the parameter is a wchar_t*
%S assumes the prameter is char*

The rule is just the opposite with sprintf()

This illustrates the difference

int main()
{
    wchar_t buf[255] = {0};

    swprintf(buf, sizeof(buf)/sizeof(buf[0]), L"G%sY", L"AMEDA");
    printf("%S\n", buf);
    swprintf(buf, sizeof(buf)/sizeof(buf[0]), L"G%SY", "AMEDA");
    printf("%S\n", buf);

}
N1GHTS commented: This answer saved me long hours of trial and error +1
sree_ec commented: I dint know that %s and %S existed :D +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. move line 20 so that it increments i after line 22.

2. line 31: There is no need for that j loop. Just make the tests for minimum and maximum within the i loop that starts on line 19. Every time a new number of entered check of the new number > max and check if its < min.

3. When i == 0, both minvalue and maxvalue should be set to input. Since inputs can be negative, the values initialized on lines 11 and 12 may or may not work. So just initilize those two variables for the first keyboard input value.

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

Your avatar looks ok to me.

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

I already told you with the code I posted above. It does exactly what you are asking. I just put the numbers in an array of integers rather than in individual variables. Just put the code I gave you in a loop that uses fgets() to read the file one line at a time, so that you know the begenning of each line.

Or if you know there will always be 4 numbers on each line and that it will never ever be anything else

int userID;
int time;
int from;
int to;
FILE*fp = fopen("filename.txt","r");
while( fscanf(fp,"%d%d%d%d", &userID,&time,&from,&to) > 0)
{
   // do something with the data
}
fclose(fp);
SamSmiles commented: Was very nice and helpful +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Standard i/o stuff -- read the file from beginning to end until you find the line you want. This is a first year programming question, are you sure you are ready to write MFC programs?

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

use fgets() to read a line, then for each line you can use strtok() to tokenize it, then atol() to convert from char* to int.

Another way is to use strtol() in a loop. Example

int main()
{
    char line[] = "12 23 34 45";
    char* ptr;
    int n1[4];
    int i;
    i = 0;
    ptr = line;
    while(*ptr != '\0' && (n1[i] = strtol(ptr,&ptr,10)) < LONG_MAX )
    {
        printf("%d\n", n1[i]);
        i++;
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

did you declare variable CStr? CString Cstr; Instead of that loop, just try this: CSt = str.c_str(); assuming CStr is of type CString.

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

>>I have tried to tokenize the whole file
Post what you have tried.


>>getline cant have delimiters in it.

std::string word;
ifstream in("filename.txt");
while( getline(in,word,',') )
{
  do something with this token
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>do I have to redraw everything?
Yes, well maybe.

>>if so, which controls should I include (at WndProc?)
catch the WM_PAINT event.

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

>> How do I edit my profile?

Click the CONTROL PANEL link at the very top right corner of the page. It's just above the SEARCH edit box.

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

CComboBox does not have an items property. (CComboBox*)(GetDlgItem(IDC_COMBO1))->AddString("Text");

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

>>Will this create a suttle memory overrun

Absolutely. sizeof(A) is 1. sizeof(float) is 4. Now you guess the result.

>>B[0] /= 3l

Wrong. It's *B /= 3; because B is NOT a pointer to an array but a pointer to a float. But since A is not a float that code snippet will have dedefined behavior, possibly crashing the program.

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

you have to check for error, and if it occurs clear the input buffer

See this thread about how to clear the input buffer.

#include <iostream>
#include <limits>
using std::cin;
using std::cout;

int main()
{
    int c;
    while(true)
    {
        cout << "Enter a number\n";
        cin >> c;
        if( cin.fail() )
        {
            cout << "Oops!\n";
            cin.clear();
            cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
        }
        else
            break;
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Are you sure unions will work well with types of different sizes?
Absolutely yes. That's the purpose of unions. The sizeof(AnyType) is the size of its largest member.

>>Is val.val /= 2 legal
No, but val.val.iVal/2 is legal

int result = 0;
switch(val.type)
{
   case TYPE_INT: result = val.val.iVal/2; break;
   case TYPE_CHAR: result = (int)val.val.cVal/2; break;
   case TYPE_FLOAT: result = (int)val.val.fVal/2; break;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It might be safer to use a union, such as VARIANT.Your structure does not have to be that complicated

typedef struct
{
   int type;
   union {
     char cVal;
     int iVal;
     float fVal;
   } val;
     
}AnyVal;

int main()
{
    AnyVal val;
    val.type = TYPE_FLOAT
    val.val.fVal = 0.0F;
}