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

mods: how did this thread get trashed? it has two different topics???

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

I've tested and my result was the opposite, thanks for the hand ;)

See what I mean -- there are lots of 'if's. You can't just make a blanket statement that printf() is faster than cout.

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

are you using VC++ 6.0 compiler? Yes, then the problem is because that compiler pre-dates current c++ standards. Update to VC++ 2005 express and the code you posted will compile correctly.

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

my quick test using VC++ 2005 Pro shows that cout is faster than printf

#include <cstdio>
#include <ctime>
#include <fstream>
#include <iostream>
using namespace std;

int foo1()
{
    clock_t t1,t2;
    t1 = clock();
    for(int i = 0; i < 100000; i++)
        printf("Hello World\n");
    t2 = clock();
    return t2 - t1;
}
int foo2()
{
    clock_t t1,t2;
    t1 = clock();
    for(int i = 0; i < 100000; i++)
        cout << "Hello World\n";
    t2 = clock();
    return t2 - t1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int d1,d2;
    d1 = foo1();
    d2 = foo2();
    cout << "\n\n\n" << d2 - d1 << endl;
	return 0;
}

final time output was 33873 on my computer. So whether one is faster than the other depends on the compiler, operating system, bus speed, and several other factors.

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

Like I said, the address is meaningless in a file. But you can save the object to which it points.

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

int main()
{
   string str = "Hello World";
   char *ptr = "another World";
   ofstream out("myfile.txt");
   out << str << endl;
   out << ptr << endl;
   out.close();
}

The above code does NOT save the addresses of the pointers, but the string to which they point.

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

Do you mean the address contained in the pointer or the object to which it points. What kind of pointer do you have in mind? Writing the address to a file is useless because it cannot be simply read back in later and expect it to point to the same place it was originally. The operating system has probably shuffled memory around in the meantime.

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

>>Also why integer is limited to -32767 TO +32767?

The standards do not specifiy the size of an integer, and it depends on the compiler you are using. You should look at limits.h to find out what you compiler supports.

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

convert the strings to a have fixed-length fields

7379->65535->65535
7315->    5->65535

The the above, just to a simple comparison

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

>> when just about every other employer is looking for c# developers with .net experience.
>> Btw I don't work, still at school :sad: this is just a hobby.

If you are still in school how do you know what employers want? Have you even held a real programming job? Or studied the current job market? :eek:

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

Sorry, but you wore out your code. The only way to fix the problem is to print the source out and then type it all back in fresh. This also happens when you let code sit too long and it starts to rot..

:cheesy: :cheesy: :cheesy: you really should have put a smily after that so that some gullible sole (like me) will see the satire. Funniest thing I've read all day.:cheesy:

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

>>"MESSAGE ""%s""\r"
The reason this doesn't work the way you want it is because that is a standard way of concantinating string literals and the compiler will just make one string out of it.

char str[] = "Hello " "World"
is the same as this
char str[] = "Hello World"

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

look at www.microsoft.com for CreateThread() win32 api function. It looks like a lot of parameters but you can leave most of them 0. I don't know if you can use the same stream object between threads or not (not sure if they are thread-safe). Even if they are, the two threads will have to coordinate their access to the stream object -- they cannot both access the same object at the same time. Normally use mutexs for this purpose.

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

>>Now my guess is that the first block of code uses more memory at any given moment than the second one
In the second example, a good optimizing compiler will notice that it only needs to allocate space for one std::string object and reuse this memory on each block entry. All memory is allocated on the stack during function entry, not block entry. That makes the second example more memory efficient than the first. It would be the same as declaring one std::string object at the beginning of the function and re-using it in each block, something like this: (in this example you don't need the blocks at all!)

int foo()
{
  std::string str;
  {
     str = "Hello";
    // blabla
  }
  {
     str = "By";
    // blabla
  }
}

>>And each mystring only has scope within the {} enclosing it right?
That is correct -- but it doesn't mean that the memory allocated for the objects are removed from the stack when the block terminates. It is only released when the function returns to whoever called it.

>>the second block of code uses more than or equal processing power than the first one
Depends on how you look at it. Overall, from the beginning to the end of the function they are both equal. But the second will consume less initial processing than the first because std::string class constructor will be called for all three strings at the same time in the first example, while …

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

>> But anyways, I'm on windows, and most likely they will be using windows also, maybe a different version, like win xp home vs win xp office or whatever

It will be portable across all versions of MS-Windows since Win95. You should verify by looking up the functions at www.microsoft.com -- the function descriptions tell you which versions of MS-Windows are required.

>>EDIT: but don't worry, if it gets bad I can always swallow my pride and ask my folks for money
You can always get a real job flipping hamburgers, bussing tables, washing cars, etc. :cheesy:

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

It won't be portable anyway -- changing colors is os-specific, ms-windows is NOT the same as *nix or MAC. If you want portability among operating systems you will have to embeed proprocessing symbols in your overloaded function, something like this:

ofstream& operator<<(ofstream& os,const string& color)
{
#if defined(_WIN32)
// blabla
#elif defined(_UNIX)
// blabla
#elif defined(_MAC
// blabla
#endif
}

The above overload probably won't link because there is already an overload function with those parameters in <string>, so you will have to think some kind of parameters.

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

No there is no shortcut because changing the color is an os-specific function and ansii standards try to stay clear of those issues. -- but you might be able to write your own ofstream overloaded function that will accomplish that.

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

I personally believe the level of control and customization offered by WIN32 CSDK programs are best over any C# of MFC. Although they are long but they are fun writing...

your employer does not pay you to play -- he pays you to get a job done in least amount of time. That means using tools that will allow you to create a program in the fastest time possible -- C# and MFC will do that, as well as several other win32 api wrapper libraries such as wxWindows and QT. The only reason today for writing a new program using pure win32 api functions is for speed-critical programs and, of course, for educational purposes.

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

In c and c++, char is sort of a misnomer -- it is meant to hold more than just characters, it is in fact a small one-byte integer and can be used just like integers of other sizes. unsigned char has a range of 0 to 255 (see limits.h for ranges on your operating system). If you look at an ascii chart such as this one you will see that standard ascii characters fall within the range of 32 to 126, all the other possible values are used for other miscellaneous things.

>>Also, can the char data type hold more than one character
In special circumstances, yes -- they are called Binary Coded Decimal (BCD) or Packed BCD. This is not very common on MS-Windows or *nix computers.

aeinstein commented: Sometimes it's the littlest detail that counts the most! +6
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

MFC is slower, try WIN32 SDK it is dificult to write but faster...

MFC is slower than what? win32 SDK programs? yes that is right. Java -- NEVER.:) MFC will whip Java in a blink of an eye.

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

>>Java is just as good

Only for internet stuff. It will work for desktop programs, but is very very slooooooow.

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

you still have several 16-bit c/c++ compilers available: Turbo C/C++, Watcom and Microsoft Visual C++ Version 1.52C. The M$ compiler is difficult to find, but the Borland compilers can be easily downloaded free from their web site. The Watcom compiler is also free, but I'm not sure where to get it -- you will have to google for it. You can NOT use any modern 32-bit compiler such as Dev-C++ to compile for MS-DOS 6.X and earlier operating systems. That means your programs will be limited to running in about 400-620K memory. If your program is small that may not be a problem.

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

All the programs I write are MFC programs. Why? Because they can be written in less than 1/4 the time as pure win32 api programs. And you are not limited to just MFC -- you can mix and match as needed with win32 functions. On the negative side, MFC makes the program larger and is somewhat slower. So if speed and size are really critical, then code in pure win32 api. I don't know much about VC++ 2005 compiler and using .NET Forms instead of MFC. And I've heard C# may be easier too.

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

Then, what would you use in replacement of <windows.h> so you could write code and use it on different OS?

depends on the operating system. There are libraries that are pretty much portable between *nix, MS-Windows, and (I think) MAC, for example QT.

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

just open the file for writing, then output all user input to that file. Yes, you have to add additional code to do that. No, you can't have cout and cin do two things at the same time (write/read to/from the screen and write to a file too).

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

also, windows.h doesn't define any c++ classes -- pure C, although you can use in in c++ programs. If the .h extension really bothers you then you could create your own file without the extension and have it call windows.h

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

See any of these google articles. Many of them are for VB, but you can just ignore that part and read about how to write the dll.

One important point: memory cannot be allocated in a dll and deallocate it in the calling application. Special non-ansi-standard functions such as GlobalAlloc() and GlobalFree() can be used, not neither new or malloc().

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

The error is telling you that you are attempting to pass a pointer to a function, and the 1st parameter is supposed to be a float. Maybe you meant to code like this so that it calls giveX() which returns the float.

glVertex3f(pA.giveX(), pA.giveY(), pA.giveZ());

[edit]Sorry Dave -- I didn't see your answer.[/edit]

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

simple -- just typecast the float to an int. All decimals will be dropped, nothing rounded with this method.

float f = 12.345;
int i = (int)f;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Good! I changed my IDE font to the font "terminal" and then inserted the é straight out and everything worked fine. Thx!

What? I thought you wanted to see 'é', not that other character. When I did that with Dev-C++ it only made the IDE look like the console, not the other way around.

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

>Use the limits in limits.h if you want portability.
Smartass. :rolleyes:

Ha! One for me, one million for you.:)

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

But for portability reasons, you can't assume more than -32,767 to +32,767.

Use the limits in limits.h if you want portability.

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

I think you're c++ program will have to change fonts before beginning to output the text. The font used by the c++ console program is not the same font that the IDE editor uses. The IDE does not use cout or other console output functions to write to the window -- it uses win32 api functions. Don't know how to do that in console program.

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

none of those functions require MFC. _beginthread() can also be used, but I find it more convenient to use CreateThread(). Here is an example.

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

offhand, I'd say you don't have a complete installation of the compiler. Attached is a bitmap of the bin directory on my computer. Yours should be similar.

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

Thanks dragon. I thought all the dragon were too proud to talk to humans. :-) You are the most friendly dragon I have ever met. :-)

:mrgreen: :mrgreen: :mrgreen: But don't forget -- ancient does not necessarily mean all-knowing.

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

depends on the operating system. For ms-windows, I use CreateThread() and CreateMutex(). see MSDN for details.

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

you are confusing the ascii value of '3' (numeric value is 53) with the binary value of 3 (decimal value of 3). Look at an ascii chart and it will show you the ascii values of all 255 possible values that can be contained in unsigned char.

If you want the decimal value of 3, then you have to convert '3' to int value -- simply subtract the ascii value of '0'
numVerts = buffer[0] - '0';

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

The best way to learn C is buy an Introduction to C book because none of the online tutorials are very good. all you will get online are bits and pieces -- a book will give you a thorough introduction. But say clear of the Dummies series -- not much better than the online tutorials.

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

In addition, if the file is NOT one the current working directory the program may have to change the current working directory to where the file is. There are a few operating systems where there is no concept of "current working directory", in which case the program has no choice but to specify the full path.

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

what you want is fgetc, not getc(). http://www.die.net/doc/linux/man/man3/fgetc.3.html

why not use the fgets() simpler function -- does everything you have in your loop

while( fgets(buffer,sizeof(buffer),stream) != NULL)
{
  // do something with the string
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

FILE (all caps) normally refers to C file i/o functions, such as fopen(), fgets(), fread(), fwrite() and several others. The word "file" (all lower case) generally describes what is actually stored on the disk -- each file has a filename and size.

>>Secondly, does the definition of "stdin" include this "FILE"?
stdin is a specific type of open FILE, normally refers to the keyboard. stdout and stderr normally refer to the monitor (what you see on the screen). I say normally because they can be changed to refer to standard disk files or to something else, like sockets, but that is somewhat rare.

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

Can't you just simply use std::vector?

vector<bool> array;

It will grow as needed.

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

use the depends.exe that is in the vc++ 6.0 install bin directory to find out what dlls are required, then install those dlls with your program. You must take care not to overwrite newer versions of the same dll that is on the target machine. If you use an install program such as InstallShield it will make those tests. To minimize the number of dlls that you need to install you can compile your program for Release Minimum Dependencies and the compiler will statically link most of them. It will make your program .exe bigger but reduce the number of dependent dlls

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

Nit:
http://cboard.cprogramming.com/showthread.php?p=367853#post367853

You're right -- the code I posted isn't quiite right.

>>The two arrays could be represented by two std::strings?
No they could not. But they could be easily represented by std::vector<string> and std::vector<int> or std::vector<some struct>

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

You don't need that list at all. you alreay have map which you can use to map strings with their counts. Add the words to the map when read and you will not need the list at all. Also, the loop to read the file can be coded better

// read the file one word at a time.  If the word contains any 
// punctuation, such as commas and periods you will probably want to 
//remove then before adding it to the map.
   while( in >> word )
   {
          // add the word to the map
   }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

VB is generally considered a toy. OK for your own personal enjoyment but not very well supported in commercial world. Managed .NET may change that perception in the future.

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

If you have learned about liked lists then using a linked list to hold the words and their counts would be more efficient. But if you haven't learned about them yet, you can just use a simple array of a very large number -- say an array big enough to hold 255 words. It doesn't matter if the array is too big, but cannot be too small, so if the file has more than that you will have to increase the array size -- maybe up to 512.

char *array[255] = {0};
int counts[255] = {0}

The above allocates an array of 255 pointers and initailizes them all to 0. Now, when the program reads a word from the file, search the array to see if the word is already in the array. If the word is not in the array, then add it in the next available slot. When you add the word, don't just simply copy the pointer but allocate memory with either malloc() (C programs) or new (C++ programs) and copy the string.

Yet another solution similar to above but make the size of the array dynamic so that it increased when a new word is added

char **array = 0;
int array_size = 0;
int *counts = 0;

when you add a word you have to also expand the arrays. array_size contains the current number of elements in the array.

char **array = 0;
int array_size = 0;
int *counts = 0; …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I know sizeof(struct baseball) = 16.

Not necessarily -- that is compiler implementation dependent. Just because one compiler says its 16 doesn't mean it will be the same for all compilers.

The sizeof(Yankeys) is the sizeof(struct baseball) * numer of elements in the Yankeys array. And the number of elements in Yankeys array is calculated like this:

sizeof(Yankeys) / sizeof(Yankeys[0])

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

>>The version I am using is 3.4.2 of gcc

Then upgrade to the newest version. Why use a buggy compiler (as if VC++ 6.0 is bug-free!:mrgreen: )

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

works ok with Dev-C++ (version 4.9.9.2 which uses gcc), just had to add getchar() after the scanf() to flush the '\n' out of the keyboard so that I could see the results before the program terminated.