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

>separated by '\n' or '\r\n', depending on the os
Don't you know that C automatically translate '\n' into the native newline character when files are opened in text mode?
Read http://en.wikipedia.org/wiki/Newline#Newline_in_programming_languages 2nd point.

Hence you never need to bother what is the newline character of underlying implementation, just use '\n' all the way.

Yes of course I know that -- I was talking about what's on the disk, not what's in memory.

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

Are those four different integers? Looks like a normal text file to me, not a binary file. If it were binary file then you wouldn't be able to read those integers. Try this:

int x;
infile.read( (char*)&x, sizeof(int));

or this

char* buffer = new char[4];
infile.read(buffer, 4);
int x = *(int *)buffer;

also this after removing ios::binary flag and reading as normal text file.

int x;
infile >> x;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to compile all programs for release mode before attempting to run them on other PCs. The DLLs you mentioned are for debugging on your PC only with your compilers.

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

Reading that file is not difficult as long as the records are separated by '\n' or '\r\n', depending on the os. The code below will read each line then split it up with the semicolons as deliminators.

FILE* fp = fopen("file.txt","r");
char buf[255];
while( fgets(buf,sizeof(buf), fp) != NULL)
{
    char *ptr = strtok(buf,";");
    while( ptr != NULL)
    {
            // do something with the text
          ptr = strtok(NULL, ";");
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Start here.

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

I am sorry but I do not know how to use code tags

Then you are not paying attention. You had to type right over the instructions when you typed that post. The instructions are also provided when you signed up, and again at the top of every DaniWeb page. Finally, here is yet another set of instructions.

[code=cplusplus] // put your code here

[/code]

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

Yes, the functions could be in a DLL and loaded at runtime. But the program will still have to know at compile time which set of functions it is going to call. The name of the function could come from the data file, the call GetProcAddress() to get the function pointer from the DLL.

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

Inlining is optional for compilers -- they may choose to honor it or not. Most compilers will honor one or two line inline functions, but the ones you posted are probably just too large to make inlining practical. IMO its better to put the implementation code for large functions in the *.cpp file instead of attempting to inline them.

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

>> static int code,a,c,p,m,d,e,h,f,z;

You forgot to declare code globally like you did the others

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

You need to use function pointers. Here is a simple example where you type the name of the function you want to execute at the keyboard. Since C and C++ and not interpreted languages you can not execute code that has not already been compiled in the program.

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


int foo1(int a)
{
    return a+5;
}

int foo2(int a)
{
    return a+6;
}

int foo3(int a)
{
    return a+7;
}

int main()
{
    int (*fn)(int a) = 0;  // This is a function pointer
    std::string line;
    cout << "Enter a function name -- foo1, foo2, or foo3\n";
    getline(cin, line);
    if( line == "foo1")
        fn = foo1;
    else if( line == "foo2")
        fn = foo2;
    else if( line == "foo3" )
        fn = foo3;
    else
        fn = NULL;

    if( fn != NULL)
    {
        int x = fn(1); // call the function
        cout << "x = " << x << "\n";
    }
    else
        cout << "fn is NULL\n";



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

"%i" Hmm! I'll have to look that one up. May be compiler specific!

Nope. Its standard ansi C

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

[edit]%i is not valid in scanf()[/edit]

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

%i ???
do you mean %d or %u ?

%i is the same as %d, they are interchangeable but I believe %d is more common.

[edit]printf() they are the same, but %i can not be used in scanf()[/edit]

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

move line 9 down to between lines 12 and 13. Programs are executed in sequence, so the calculation can not be performed until after the value of the variables are known.

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

I'm using 64-bit Vista, but wish I was using XP so that I could play my favorite game Diablo LOD. It won't play on Vista. I have a copy of 64-bit XP but had to uninstall it because of lack of device drivers at the time. I also have a copy of 32-bit XP but using it on a different computer. Vista Home was preinstalled on the HP computer I'm using now.

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

>> Carlist.Cost[i1]=
Should be Carlist[i1].Cost= ...

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

Thanks ancient dragon,
the trick was passing a pointer pointer then.

No -- read the code I posted carefully because there are other changes.

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

change "%u" to "%p" and rerun your program. Also, the way to insure there are no holes in the structure is to use the #pragma pack(1)

#pragma pack(1)
struct emp
{   
    int age;
    char name[6];

} e;
#pragma pack()

int main()
{
    printf("%p\n",&(e.age));
    printf("%p\n",(&(e.age)+0));
    printf("%p\n",(&(e.age)+1));
    printf("%p\n",(&(e.age)+2));
    printf("%p\n\n\n",(&(e.age)+3));

    printf("%p\n",(&(e.name)+0));
    printf("%p\n",(&(e.name)+1));
    printf("%p\n",(&(e.name)+2));
    printf("%p\n",(&(e.name)+3));
    printf("%p\n",(&(e.name)+4));
    printf("%p\n",(&(e.name)+5));
    printf("%p\n\n\n",(&(e.name)+6));

    printf("%p\n",((e.name)+0));
    printf("%p\n",((e.name)+1));
    printf("%p\n",((e.name)+2));
    printf("%p\n",((e.name)+3));
    printf("%p\n",((e.name)+4));
    printf("%p\n",((e.name)+5));
    printf("%p\n\n\n",((e.name)+6));
   
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

since ptr2 is a reference to ptr1, when you change ptr1 the change is also reflected in ptr2. What you can't do with ptr2 is assign a new value to it like *ptr2 = 2; is an illegal statement because ptr2 is a const.

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

There are several ways to accomplish that -- functions in stdio.h or iostream. For example: cin.get()

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

>>The most natural place for structures without tags is typedef declaration

I forgot about those :)

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

0 And by the way average is sum/50.0 and not 5.0.

Its actually sum/(number of elements). If the array is full then it will be 50, otherwise it will be however many numbers are read from the file. So sum/5 might be correct if there are only 5 numbers.

And that average function is wrong too -- you need to pass it the number of items read from the file which may be less than 50.

jephthah commented: more correct +10
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need to actually dynamically allocate the orginal memory

#include<iostream>
#include<cstring>

void fix_name(char **name){
  printf("pointer in function arg: %p\n",name);
  int len = strlen(*name);
  if((*name)[len-1]!='/'){
    char *tmp = new char[len+2];
    strcpy(tmp,*name);
    strcat(tmp,"/");
    printf("content of new cstring: %s\n",tmp);
    printf("pointer of new cstring %p\n",tmp);
    delete[] *name;
    *name=tmp;
    printf("pointer of function arg: %p\n",*name);
  }
}

int main(int argc, char** argv){
  char* sdsd = new char[11];
  strcpy(sdsd,"asf/asdf");

  printf("%s\n",sdsd);
  fix_name(&sdsd);
  printf("%s\n",sdsd);
  delete[] sdsd;
  return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

After calling GetLastError() I always call FormatMessage() to get the error's text

char buf[255];
DWORD dwError = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0,
      dwError, 0, buf, sizeof(buf), 0);
MessageBox(NULL,buf, "",MB_OK);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I know what that function does, but why use wsprintf() instead of sprintf()? The first argument to wspringf() is wchar_t*, not char*.

>>WS_EX_LEFT || WS_EX_LTRREADING || WS_EX_RIGHTSCROLLBAR,

That should be using the bitwise | operator, not the boolean || operator WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,

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

Buffer overruns might cause that behavior because the compiler (at least Microsoft compilers) adds extra bytes to memory allocations when compiled for debug.

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

compile both for debug and use your compiler's debugger to find out the problem. Does your program use LoadLibrary() to load the dll into memory? Then GetProcAddress() to get pointer to a function? Make sure that function pointer is not NULL.

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

>>wsprintf

Are you compiling this program for UNICODE? If not, then why are you calling the unicode version of sprintf()? I know this doesn't answer your question, I don't know the answer.

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

hi everybody. i need to to know where to start developing my hard ware knowledge.
mimi

In school.

And Welcome to DaniWeb.

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

With me your code compiles (I'm using MinGW), but when running, your program will crash :)

That's because it contains illegal code for *nix and MS-Windows operating systems. Its not even valid code for MS-DOS 6.X and older.

tux4life commented: Oh, that's why :P +9
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

All versions, including the Express edition, has Windows Forms. To start a new project click File --> New --> Project --> CLR, then in the Templates window click Windows Forms Application

>>BTW, I am actually going to buy one of these versions but the pro edition is a bit pricy.

Don't bother -- just use the free Express version because it has the same compiler as all the other versions. The only difference between versions is all the other extra stuff you get, such as support for MFC.

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

post an example of the input string because I don't have a clue what that code (lines 23-39) is attempting to do.

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

>>Any advice or tip will help out. Thank you

Your program contains hard-coded data, but the requiremenets state it must read the data from a data file. That's the first thing you need to do -- get that working first then continue on with the next item in the requirements statement: sort the data.

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

you could do this: Also, its not necessary to surround function calls with parentheses.

int main(int argc, char** argv){
  char sdsd[] = "asf/asdf";
  printf("%s\n",sdsd);
  fix_name(sdsd);
  printf("%s\n",sdsd);
  return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>reinterpret_cast is the recommended way over anonymous unions.


reinterpret_cast is not allowed in C. So I don't know where you got your information but its wrong.


The only place I've used anonymous structs is inside a union

union something
{
    struct
    {
          int x;
          int y;
    };
    unsigned char buf[2 * sizeof(int)];
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post actual code

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

Do it by having intimate knowledge of both languages. Normally instead of converting C program line-by-line you would convert function concepts. If you have a C function named foo(int a, int b) that returns the product of two numbers then just write the same function in matlib.

Don't ask me how to do it because I have no idea. If you don't either then you need to spend some time learning matlib and learn how to do it yourself.

jephthah commented: yes, but they never listen to reason. +10
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The secret is using a great compiler's IDE, such as VC++ 2008 Express, and learning how to debug programs.

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

It would be helpful if you told us which line the error occurred on so that we don't have to read the whole program to find out.

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

>> if (argv[2] = "SC")
You can't compare character arrays using the = operator like you do VB. Use strcmp() function prototyped in string.h header file -- or <cstring> if you prefer. if( strcmp(argv[2], "SC") == 0)

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

There is an error in the airport1.txt file on line 90 SYD BNE 07:30 1:00 getline() reads it in as SYD BNE07:30 1:00 -- missing the space between BNE and 07:30. I re-typed the line and the program continued to work from there until the next error, which was at the destructor

AllAirports::~AllAirports() {
    airports.~map();
}

You can't explicitly call a c++ class's destructor like that because it will crash your program.

ippomarley commented: shows amazing talent and experience +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you step through the code and look at the values of y you will see that it never equals exactly 0 because y is a double and can contain small rounding errors such as 0.000000099. Never ever try to text a double against 0 because it will almost always fail. try something like while( y > 0.0000009)

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

Its an infinite loop because the value of y never changes.

>> x=static_cast<int> (y)%10;

The cast is unnecessary because y is already an int, and y%10 is also an int.

And why is d_a a double? Why not just enter the binary digits as an integer? (unless of course that is part of the assignment)

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

Or just use XP zip compression - right click on the folder, and select Send To/Compressed Folder. XP wont unzip this thing, and neither will Python's zipfile module.

-- Paul

[sarcastic] Damn, how many gigabytes is this code. Trim it down to one million lines of code so you don't have to zip it. [/sarcastic]

Don't you two know how to read? This thread is 2 1/2 years old for God's sake. I really doubt anyone cares what either of you think about this topic.

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

I was wrong only once in my life -- I thought I was wrong but I was wrong. -- Unknown

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

I just got back from work and looked at the zip file you posted.

The program fails when function dummy() calls FileToString() because the file name (first parameter) contains the '\n' at the end of the filename, so the os can't find that file.

To correct that you will have to modify function Tokenize() to strip the character from the string.

vector<string> Tokenize(string data, char delimiter)
{
	vector<string> pieces;
	unsigned int a = 0;
	unsigned int b;
	while (true)
	{
		b = (unsigned int)(data.find(delimiter, a));
		if (b == string::npos)
			break;
                pieces.push_back( data.substr(a, b - a-1)); 
		a = b + 1;
	}
	pieces.push_back(data.substr(a));
	return pieces;
}
CppFTW commented: Thanks! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Can you zip up that file and post it here. I ran your program 2 million times without an error on a ReadMe.txt file.

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

why don't you just get one of the free open source FTP client programs?

Nick Evan commented: Good advice +18
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is the file more than 2 gig in size? Can you replicate the problem using the same file with a program that just calls those two functions?