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

I tired the solution posted by ANCIENT DRAGON. It is not working too....:

Post your code. fgets() has been working perfectly on all platforms for over 20 years. Its your code that is wrong, not that function.

Take a look at this function:

int *getline( char *string, size_t size )
{
<snip>
    return string;
}

Why does that function say its return int * when it is in fact returning char * ?

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

there is a lot easier way to get a line from the keyboard than what you coded in the program.c file. fgets() will do all that nasty work for you, like this -- notice there are no loops and only 1 line of code other than the buffer declaration.

char line[100];
fgets(line,sizeof(line),stdin);

BTW: your question is ok, its the poll that is a dumb one.

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

Given that the code posted by me/Vijayan works both on windows and unix #ifdefs won't be needed in this particular case.

agreed that kind of messy stuff isn't needed when using portable functions like clock(). I think I was referring to using non-portable code such as getrusage(). And there are, of course, hundreds of other non-portable examples.

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

>>Which I guess means I'm sort of working on unix doesn't it?

Yes and No. You are working on both environments and will probably have to program for both by using preprocessor directives for each compiler

#if defined _WINDOWS
// do MS-Wndows specific code here
#else
// do *nix specific code here
#endif

I know the above looks like a pain, but that's the way portable programs are written when os-specific api calls and even compiler-specific calls are made.

In your case, I think I would install cygwin on your ms-windows computer and use gnu compiler so that you don't have to do that sort of messy preprocessor stuff as shown above.

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

Thanks everyone for all your posts. Will try different solutions and see what works. Thing is, does the fact that noone has mentioned getrusage() mean that noone here uses it?

never heard of it until yesterday. Its a linux/unix-specific function and most posters here use MS-Windows os.

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

Here is more information. I think I left out something in my instructions. Scroll down to the Examples section.

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

nonetheless, i found out that clock() works different depending on which compiler you are working... 4 example... in dev c++, i measured clock measured 1/1000... but turbo c++ measured 1/20...

the thing is... i don't know why...

In the original MS-DOS 6.X and older operating systems the clock speed default was 18 ticks per second, thus clock() return 1/18. Programs that wanted to use milliseconds had to deliberately reprogram the clock speed to 1000 ticks per second and reset it back to 18 before the program exited. If the program failed to reset the clock speed then all sorts of funny, and sometimes disasterous, things would happen.

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

1. Don't know the first question.

2. Functions must often be run hundreds or even thousands of times in order to get measureable time. The clock() function is in milliseconds and usually sufficient. So, to get a measurable amount of time

start timer
run the function 1,000 times
stop timer
delta time (stop - start) / 1000 is the average time per iteration of the function.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Step 1: Tell the eVC++ 3.0 IDE to create a makefile, which is a text file that contains all the commands the compiler needs to compile the project from the command line. Select menu Project --> Generate Makefile (near the bottom of the program menu). This will generate a file with *.vcn extension.

Step 2. Open the *.vcn file with Notepad. Scroll down a few lines and you will see a line that looks something like this: !MESSAGE NMAKE /f "example.vcn" CFG="example - Win32 (WCE ARM) Debug" Copy that line into a batch file. Then look at the *.vcn file and you will see down a few more lines other lines that begin with !MESSAGE. Choose the configuration you want and replace the text in the batch file you just made with it.

Step 3: you will have to run another batch file that sets up the compiler's environment variables for the target processor. When you installed your compiler it also installed these batch files in the <install directory>/bin directory. On my compiler they are here: C:\Program Files\Microsoft eMbedded Tools\EVC\WCE300\BIN Now in the batch file you created in Step 2 above you will have to call one of the batch files in the above bin directory. For example: if you are compiling the ARM target processor then call the file WCEARM.BAT

Step 4: compile the program from the command line. Just execute the batch file you made above.

Here is the complete batch file that I …

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

I think there are only a handful of users who post in those silly games -- and some of the mods have posted in them too. I've even seen Ms. Dani a couple times, which is very very rare occasion. Rather than stop it probably having their own board in Coffee House would be better solution.

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

I take it that the console app is program that runs on a PC and compiled with a different compiler -- you can't compile PC programs with eVC++ 4.0. Or do both programs run on the same target device?

I know that VC++ 2005 is capable of doing what you want -- that is setting dependencies among projects. Not sure about eVC++ 4.0, I doubt it. What you might have to do (and how I did it in the past) was to write a small batch file to compile the programs in the desired order from the command-line.

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

It compiled and linked ok for me with that compiler. Must be the way you set up the project. First, create a new empty console project. Copy the *.cpp and *.h files into the project folder, then use menu Project --> Add To Project --> Files, and select the files you added above.

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

>>Where did that isvalidInt come from?

You wrote the program, we didn't. You will probably have to write that function yourself because it is not standard C or C++ function.

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

You will probably need to create a function that checks for keyboard hit (such as kbhit() from conio.h) and time() to get current time, something like this. This won't work if your compiler does not support conio.h, which is a non-standard header with non-standard functions.

It also assumes MS-Windows os because of the Sleep function. If you are using *inux os then replace it with sleep() (lower-case 's')

bool chkinput()
{
   time_t t1, t2;
   t1 = time(0);
   while( !kbhit())
   {
         Sleep(100);
         t2 = kbhit();
         if( (t2-t1) >= 30)
            return true;
   }
   return false;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

_getcwd(). Its not a win32 api function. or GetCurrentDirectory() which is a win32 api function.

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

how to draw a line depends on the operating system. If you are using MS-Windows you can use the win32 api graphics functions. If you are in MS-DOS you could probably just use a line drawn with dashes, in a loop of some sort.

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

works ok for me. your problem must be something else. what compiler? what are the errors?

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

rewind moves the file pointer back to the beginning of the file so that it can be read all over again.

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

The problem is not the data files, but that you are using feof() to test for end-of-line. That doesn't always work the way we would like it to. Here's a better solution

else {
           
           printf("%-20s%-13s%-20s%10s\n", "EmployeeNumber", "Name","EngineeringDicipline", "YearOfBirth");
           printf("________________________________________________________________\n");
           fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);

// changes start here
           while (fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth))
           {
                if(employeeNumber!=0){      
                    printf("%-20d%10s  %-20s%7.2d\n",employeeNumber,name,engineeringDicipline,yearOfBirth);
                }
           }
           fclose(cfPtr);
           system("PAUSE");          
      }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have to zip them with WinZip or PkZip which will given them a .zip extension.

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

What operating system and compile are you using? Where did you get those two files? If you are using MS-Windows and you got the files from someone running linux/unix, then your program may not know how to interpret the end-of-line characters correctly.

Also, if the files are small you can zip them up and attach them to your post so that we can have a look at them.

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

first, use std::string's find() method to find the first space, then use its substr() method to extract the first word

std::string line = "name    =  John";
std::string tag;

std::string::size_type pos = line.find(' ');
if(pos != std::string::npos)
{
   // first space found.  So extract the first word
   tag = line.substr(0,pos);
}

Now, use code similar to above to find the '=' symbol, and extract everything after that. You will also want to skip all the spaces between '=' and the first non-white-space character.

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

Line 19 -- change || (or) to && (and) logical operator.

[edit]Sorry Aia -- didn't see your post[/edit]

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

The test in line 29 can never ever be true because the line just before it set k equal to j. Move line 27 to after line 34 and see if that helps.

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

it won't matter how many spaces are between the marker and the '=' symbol. After reading the whole line into memory take the first word (which starts with the first space on the line). If its the marker you want search for the '=' symbol and the text you want follows that (ignoring the spaces between '=' and the text).

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

I dunno about that... do windoze systems come with compilers on them? If your school uses macs then you might be in business, but I don't think windoze comes with any c++ compilers.

And when I think about the "editing cout" thing, it makes me think about overloaded operators. ^_^

you mean to tell us that a college that offers c and c++ courses, requires lab assignments, do not have computers with compilers already installed for students to use :-/

And to answer you question -- no MS-Windows does not come with compiler(s) already installed.

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

the computer at school should already have a compiler installed.

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

try it and find out :) you can download Dev-C++ free from here, install it on the flash drive. I don't know if it will work or not, not sure if it installs any registry settings. If it does, then you probably can't use it.

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

if your flash drive is big enough (1 gig or larger). But I wouldn't even consider running such a compiler from there -- it would probably be too slow. you might try Dev-C++, its 176 Meg on my hard drive.

Back in the old days (20 years ago) compilers fit on one 256K floppy disk. But those days are long gone.

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

No -- you need one (or more) for each operating system. GNU compilers gcc and g++ are available on many operating systems, such it is just command-line driven.

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

I was thinking that too -- you should never try to install a program that was compiled for debug on another computer, for the very reason you just discovered. Glad you found the problem. :)

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

why ? There are probably other more simpler and more portable ways to accomplish whatever it is you want to do.

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

what kind of prorogram did you write? .NET ? does the other computer have .NET installed? does it have the correct version of the windows DLLs ? The compiler bin directory has a depends.exe program that will show you all the DLLs that your program needs. Run it against your program and see if the other computer has them all.

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

Its not bits but bytes. The smallest thing you can send from client to server is a byte, which is normally 8 bits. The letter 'Q' is one byte.

>>Also when reading at the other end, i will have 3 bits of information in the string

No, what you probably mean is that the server will get 3 bytes of information. Just treat it like you would any other character array.

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

What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?

Please re-read my post -- I realized void was not right and made appropriate correction. Sorry if I confused you. :$

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

looks ok to me except the function should return char* not char. See lines 13 and 14 below.

The rest is simple

int main()
{
  int player = 0;
  int computer = 0;
  int tie = 0;
  do {
    char userChoice;
    char compChoice;
    const char* msg;
    compChoice = getCompChoice();
    userChoice = getUserChoice();

    msg = result(compChoice, userChoice, player, computer, tie);
    cout << msg << "\n";

    cout << "User picked: " << userChoice << endl;
    cout << "Computer picked: " << compChoice << endl;

    
    cout << "User: " << player << endl
         << "Computer: " << computer << endl
         << "Ties: " << tie << endl;
  } while (doItAgain());
  return (0);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If I put your thumbnails side-by-side and look really really hard I can see one is slightly smaller than the other.

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

Just make the menu option the first character of the string that the client sends to the server. But I think that is what you already said. I have no idea what the remainder of that string will be.

>>and how it can be done using tab spacing ?
Huh? No idea what that means.

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

create the function and pass the three variables (tie, player and computer) by reference to it.

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

1. remove the else in that function. Each score must be tested and the else is preventing that from happening.

2. At the top of the function set LO to be initially the value of score1.

3. Why is LO a parameter to the function? Make it a local variable and return it at the end of the function.

int findLowest(int score1, int score2, int score3, int score4, int score5)
{
    int lo = score1;
    // other code here

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

so use realloc() to expand the 2d array to the desired size. Something like this untested code

// allocate first row
int **array = malloc( sizeof(int*) );
// allocate 6 columns
array[0] = malloc( 6 * sizeof(int));
int numRows = 0;
int n;
int row = 0;
int col = 0;
while( fread(fp,"%d", &n) > 0)
{
    if( col < 6)
    {
       array[row][col] = n;
       ++col;
    }
    else
    {
       ++row;
       array = realloc(array,row * sizeof(int*));
       array[row] = malloc( 6 * sizeof(int));
       col = 0;
       array[row][col] = n;
       ++col;
     }
 }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

first, create the arrays of appropriate size. If you know the number of rows and columns in the file it will make it a lot easier. But if you don't (and normally you will not) then you will just have to dynamically allocate them with malloc().

Easiest way is to put it into one large array

// array to be allocated
int *array = 0;
int x = 0;
int y = 0;

// open the file for reading
FILE* fp = fopen("file","r");
// read x and y
fscanf(fp,"%d", &x);
fscanf(fp,"%d", &y);

// allocate the array
array = (int *)malloc(y * x * sizeof(int));

// read the data into the arra
int i = 0;
int maxi = x * y;
for(i = 0; i < maxi && fscanf(fp,"%d", array[i]) > 0; i++)
         ;
fclose(fp);

// now you can calculate the location of any x,y cell by this simple formula

int row = 5; // desired row
int col = 3;  // desired column
int cell = (<desired row number>-1) * <columns per row> ) + (<desired column number>)
or
cell = ((row-1) * x) + col;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

not clear what a1 b1 c1 ... represents.

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

NodeType (line 10) has not been defined so your compiler has no clue what it is.

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

MIN and MAX won't really be of much value in your program anyway.

First use pencil & paper to find the minimum value of the 5 scores. That will give you a clue how to write the program. You would set the initial minimum value to be the value of the first number. Then look at the second number -- if it is less then the current minimum change to current minimum to be the second number. Do the same with each of the other 3 values. In your program you will have a series of 4 if statements, one if statement for each of the numbers 2 through 5.

Now do the same thing to find the maximum value.

[edit] I agree with Joe about using arrays, but only if you have learned them yet. If you can use arrays it will make the coding a lot easier[/edit]

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

I removed the smily and added the line numbers to make it a little easier to read the code.

Without actually compiling your program my guess is that the compiler doesn't like the open/close parentheses in line 16. And the keyword void in line 18 should be removed if it is trying to call that function.

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

The problem with your program has nothing to do with your compiler -- it was you who write the bug not the compiler. The program you posted will not work correctly with any compiler.

For a beginning class in c++ there is nothing wrong with your compiler. You will however need a better compiler when you get to more advanced stuff.

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

>>Ah, yes, about the "=="
that is a boolian operator, not an assignment operator.

you have a couple options
initialize the array with the string

char a[] = "Hello.txt";

or use strcpy

char a[15];
 strcpy(a,"Hello.txt");
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. don't use the version of fstream and iostream that have .h extension -- they are depreciated (obsolete) and were replaced by versions without extension. But if you are using an ancient compiler such as the original Turbo C++ you may have no other choice.

2. Don't use global variables for the stream class. Make it local to the function in which it is used.

3. Never erver use void main(). Always use int main instead. See this thread for more explaination about that.

4 The program didn't work because your compiler probably produced an error or warning that you ignored. Never ignore warnings because most of the time they are really errros. Example: a == "Hello.txt"; is an error that you should have corrected.

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

If you want to read the file like that you will have to write an overloaded extraction operator for your structure because the standard stream classes don't know how to read structures. Or if you don't want to do that, then extract each element individually, something like this: I don't know what the structure looks like so I'll just make up something.

infile >> students[i].name >> students[i].address;