Adak 419 Nearly a Posting Virtuoso

In your TC directory is a file named readme or readme.txt (forget which), and it explains how to install the software.

What's happening with this error, is that the compiler can't find these header files. Read the readme file, and set your directory option for the compiler, under the "Options" >> compiler (iirc), headings of the IDE.

Use <stdio.h> with the brackets, if you are using the header directory you have in the compiler listing. Use "stdio.h" with double quotes instead, if you have them in a different sub directory.

IIRC, the directories should go:

C 
-- TC 
---   -- BIN, 
         headers
         examples
         etc.

If that doesn't work, please start up a new thread - this one is old, and marked as solved already. It won't get a lot of attention.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Wurdig! ;)

Remove the switch case. It just makes things more difficult.

You'll need two nested for loops. The outer one will range from 0 to < MaxRows. The inner for loop is for the columns in one row, and will range from 0 to < MaxCols.

One or two if statements inside the inner for loop, will give the logic needed to either print a char (putchar('*'), or print a blank char (putchar(' ').

It's very helpful to see the logical steps needed, if you do it several times by hand, with tree's of different sizes, with paper and pencil first. Keep your code as simple as possible. Simple is good!

Adak 419 Nearly a Posting Virtuoso

Thanx Adak. You are right about my sofware engineering ambition. I am running DOS under windows and I am totally cognisant of the fact that DOS is cadevorous rotting operating system, but don't you think that learning DOS before going for windows could give me a better understanding of how things work at the elmentary level, and how things really began manfesting.

Also let me know if you can provide me with some useful resources which you think might be of help, especially if you have things regarding Intel Processors and their architecture and instruction set.

I would skip DOS, then. Google up info on the 8088 or 8086 cpu (first PC cpu), and then work your way up through the 80186, 80286 and especially the 80386. The AMD (not Intel) design for keeping compatibility with 8088 software, while moving to a flat memory model, etc., was a HUGE breakthrough. Intel couldn't match it, and paid big bucks to use the AMD design (which is still the basis for the cpu, although it's been enhanced many times over by both Intel and AMD).

For hands on learning though, you can't beat the simple(r) micro boards like Arduino, and etc. Those little kits are a blast and a half, and (if you don't burn them up), then can be used for several different projects. There, the simplicity of the board makes so many things jell in how digital micro electronics work.

You can't run TSR's on top of Windows …

Adak 419 Nearly a Posting Virtuoso

Try "cd..", to get back to the next higher level directory (parent). After the cd, if you don't use the two dots, you should specify the directory name you want to change to.

Try "dir", instead of "ls".

What OS are you using?

Adak 419 Nearly a Posting Virtuoso

I used to have a book with some examples of this - haven't seen the book in years, but I'll look around and see if I can find it.

The problem with TSR's was, they would work on PC's with a certain design, and peripherals, and OS, but anything else was a maybe, and the further you got away from the author's design of PC, the less of a chance there was of his TSR programs (or any assembly program), working.

TSR's worked if they were made for a particular PC and OS, etc., but they were never officially supported by Microsoft, unless they were made by MS.

From your other forum's posts, I believe you'd really like software engineering, and a small microprocessor board based project, would be right up your alley. Have you looked into any of the Arduino board or other micro board forums/projects?

The more you look into TSR's, the more you'll find they're sort of the smelly rotting corpse, end of the PC engineering world.

But I will look for the book. ;)

What version of DOS are you running? You're not running this inside a Virtual Box/Virtual Machine type environment, right?

Adak 419 Nearly a Posting Virtuoso

Windows puts constraints on what you can do in this regard, depending on the version of Windows you are running. You are very likely to crash either the TSR program, another program running at that time, or even Windows itself.

Modern way to do this, would be to use a service, perhaps. Check with your operating system.

Adak 419 Nearly a Posting Virtuoso

On line 43, replace the [ char with (, and the ] char with ).

On line 59, change

while((fgets( buff, BUFSIZE, fp)) != NULL)

to

while((fgets( buff, sizeof(buff), fp)) != NULL)

Since you're not using BUFSIZE anymore.

You may not have a parallel printer port, but C automatically opens stdprn as a file, when it starts your program. I don't have a code sample of it, and haven't used that function, since DOS days. (I'm sure Google has something newer than that!). Check it out, but it's easy (if it's what you want), to start your program from the console window "command line", with:

myprogramName > nameOfFile

and then just print the nameOfFile file, like you would any other file.

Adak 419 Nearly a Posting Virtuoso

Your logic should answer the kid's annoying questions on long trips:

"Are we there yet?"

while(1) {
  if distance == goal) {
    stopWalking();
    printf("Goal reached\n");
    break;
  }else {
    takeAstep();
    //assumes each step is 1 meter
    printf("I have %d more meters to cover\n", --distance);
  }
}

In humans, walking is a forward lean accompanied by taking steps. Same thing with robots, except they don't do well with the energy saving forward lean aspect of it. It still saves energy, but if you don't limit it, they'll fall over. They have no sense of balance, as we do, unless you build it into them.

Adak 419 Nearly a Posting Virtuoso
#include<stdio.h>
#include<conio.h>
#include<string.h>
int x;
char name[20];

main()
{
      printf("Exercise no. 10 ESN 211-9A MWF 7:30-9;30\n\n");
      printf("Enter your age:\n");
      scanf("%d",&x);
      if (x<18)
         printf("UNDERAGE!\n\n");
      else if (x==18)
         printf("STUDENT LICENSE POSSIBLE\n\n");
      else 
         printf("PROFESSIONAL LICENSE AVAILABLE\n\n");
      printf("Enter the result of your drug test: \n");
      scanf("%s",&name);

      /* C doesn't support strings as basic data types, so you can't compare them
         directly. Use strcmp() (from string.h), to compare strings. Strcmp returns
         either a value < 0 (less than), 0 (equal strings), or > 0 (greater than). 

         Practice with it, and see which results you get with different comparisons.

      */

      if((strcmp( name, "positive" )) == 0) //note 6 parenthesis
         printf("HOLD LICENSE");
      else 
           printf("RELEASE LICENSE");
      getch();
      }

The return from strcmp(string1, string2) may indicate the number of letters difference, between the two strings, but that is not guaranteed by the C standard. Only < 0, 8, and > 0 are guaranteed. Another way to do the above if() statement would be this:

int result;
result = strcmp(string1, string2);
if(result == 0)
  printf("Strings are equal\n");
Adak 419 Nearly a Posting Virtuoso

Remove line 37 and 38, and copy these two lines into it's place:

37. if (( buff[strlen(buff)-1 ]) == '\n')

38.   buff[strlen(buff)-1 ] = '\0'; //remove newline from buff

It's never correct to have main(), or void main(). There are only two forms of main declaration, that are correct:

int main(void) {

   return 0;
}

//or

int main(int argc, char *argv[]) {


  return 0;
}

The argc is the integer count of the number of arguments being passed into the program (typically, by a redirection of standard input), and char *argv[] is an array of pointers to char, which is storing the actual string inputs.

The name of your program itself, will be found in char *argv[0] = "My_Program's_Name".

Adak 419 Nearly a Posting Virtuoso

Why you have use getchar() ?

The getchar() removes one char from the input stream - in this case, the newline char, left whenever you use scanf().

When things are going right, it may not be a problem, but when they don't, you may need to remove unwanted content from the input stream.

Here's a very small example of how bad input stream can goof up a program badly:

#include <stdio.h>

int main(void) {
   int num=1;

   while(num) {
      scanf("%d", &num);
      printf("Num = %d  Enter 0 to quit. \n", num);
   }

   return 0;
}

Looks innocent enough, doesn't it? Run it through once and enter integers to see that it works, as it should.

Now, run it again, but enter a letter, instead of a number, and watch the chaos!

Lastly, put this line of code, anywhere in the loop:
getchar();

recompile the program, and run it again. ;)

The difference is like night and day, don't you agree?

This is just one of the reasons why scanf() is for input of formatted code, only (generally), and by "trusted user's". General user's should have their input taken as a string ( fgets() ), and be examined carefully before accepting it as legit input to your program.

Adak 419 Nearly a Posting Virtuoso

I would change this code:

printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);

to this:

for(i=0;i<sizeof(buff);i++) {
                   buff[i] = '\0';  
                } 
                reply = 1;	
		while( reply == 1 ) {
	           printf( "Enter 1 to select file to display 0 to exit " );
		   scanf("%d", &reply);
                   getchar();
                   if(reply == 0)
                      continue; //loop again, effectively breaking out if reply is 0
 
		   puts( "Enter name of text file to display: " );
		   fgets(filename, sizeof(filename), stdin);
                   if(filename[strlen(filename)-1]== '\n'
                      filename[strlen(filename)-1] = '\0'; //remove newline from string
                     
		   /* change any backslash char's to forward slash, in buffer */
                   int len = strlen(filename); 
                   for(i=0;i<len;i++) {
                      if(filename[i] == '\') {
                         filename[i] = '/';
                      }
                   }   	           
                		
		   // Open the file for reading. 
		   if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		   // If end of file not reached, read a line and display it.
	
		   while((fgets( buf, BUFSIZE, fp)) != NULL) {
			printf( "%s", buf );
		   }
	
		   fclose(fp);
                } //end of while(reply == 1)

I haven't run this, so it may not …

Adak 419 Nearly a Posting Virtuoso

Make your output screen a tiled window, before you start the program. If you need to see more of it, use your mouse, only.

Obviously, you can't leave keystrokes in the input buffer, and not have it affect your program, which relies on the keyboard input buffer.

Adak 419 Nearly a Posting Virtuoso

Yes, that code is bogus < facepalm >

The first part is right

for(i=0;i<52;i++) { //eliminate repeated numbers
   array[i] = i;
}

The second part is goofed. All you should do there is pick a random two number from 0 to 51, and then swap them, using the two random numbers as the index into the array[].

for(i = 0; i < someNumber; i++) {
  //adjust for cards numbered 1 to 52, if you have that set up
  rand1 = rand() % 52;  //range is 0 to 51 
  rand2 = rand() % 52; 
  if(rand1 != rand2) {
     temp = array[rand1];
     array[rand1] = array[rand2];
     array[rand2] = temp;
  }
}

What should "someNumber" be? I'll leave that as an exercise for the student. ;)

Adak 419 Nearly a Posting Virtuoso

In this part of the code, the do while loop, belongs INSIDE the else statement, not after the else statements closing brace.

else
			{	// parial input line read
				filename[0] = 0;  // empty the filename buffer
			[B]}[/B]
                /* this do while loop, belongs INSIDE your else block, not outside
    		do
    		{	// loop until the new-line is read
        		fgets(dummy, 50, stdin);
        		strcat(filename, dummy);  // Save input but be sure
        					// sure to test your buffer size
		} while (dummy[strlen(dummy)-1] != '\n');

Also:

your reply query is still outside the largest while loop, and needs to be brought inside it. Unless that is done, you won't see that query again.

You are trying to read from the file before you open it. Open it first, then read.

If the line of text is longer than 60 chars, do you really want to remove it?

Adak 419 Nearly a Posting Virtuoso

What's your plan of attack here?

You can calculate this by adding all the exponents that are part of the numbers you want to multiply (10^9) * (10^9) = 10^18. That is one of the laws of exponents.

Carry out those additions, and that will tell you how many zeroes the final value would have.

As far as calculating it yourself:

Your array size is way too small, to me. Also, you'll probably need to change the data type of char, to save memory**. Char's can be printed out like "little numbers", if you use %d or %i format, and can be added, subtracted, multiplied, etc. Just remember their small range (127 signed char or 255 for unsigned char).

** static arrays are limited in size in C, because the stack that provides the memory, is a small subset of total memory. Use dynamic memory allocation to get memory from the much larger heap for huge arrays. (malloc or calloc). I suggest calloc() because it can set the value of all the array elements, to zero for you.

Adak 419 Nearly a Posting Virtuoso

Try it, it won't bite you. ;)

To learn to be a problem solver, you have to explore the world of code - roses and thorns, as well.

sergent commented: agree +6
Anuradha Mandal commented: I also agree. +0
Adak 419 Nearly a Posting Virtuoso

Because '\n' is not part of your *substring

I wouldn't use fseek(), unless it was the only way possible to make it work. Any time you can avoid making the disk head move around, I'd avoid it. On big files or lots of runs, it introduces relatively long delays (shorter delays with SSD's, but still a delay), and extra wear and tear on disk heads, if you have the data on HD's.

I'd go with a "flag" (a boolean type variable), that you set when the match == strlen(substring). Then, when it loops and a char has been stored in ch, add an if statement, if ch is a space, or a punctuation, then the match is confirmed, so handle it then, and of course, reset your variables, including the flag.

Adak 419 Nearly a Posting Virtuoso

Yes, I missed the factorial mention. Ooops!

I'm not a mathematician. If there is a trick to answering this, I don't know it. I would have to calculate it out.

You might want to look at this website:
http://www.edepot.com/win95.html

It's a shareware big number program. I don't know if it can possibly calculate your range of integers though. That's a HUGE number. This quote on Wikipedia, was interesting - perhaps the factorial number could be estimated?

The calculation of factorials can easily produce very large numbers. This is not a problem for their usage in many formulae (such as Taylor series) because they appear along with other terms, so that—given careful attention to the order of evaluation—intermediate calculation values are not troublesome. If approximate values of factorial numbers are desired, Stirling's approximation gives good results using floating-point arithmetic. The largest representable value for a fixed-size integer variable may be exceeded even for relatively small arguments (see the table below). Even floating-point approximations can exceed the maximum representable floating-point value.

It may help to recast the calculations in terms of the logarithm of the number. But if exact values for large factorials are desired, then special software is required, as in the pseudocode that follows, which implements the classic algorithm to calculate 1, 1×2, 1×2×3, 1×2×3×4, etc. the successive factorial numbers.

I can't help you with GMP, because I've never used it. Go to their website, and follow the instructions for your OS and compiler. Be …

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Bubbinos! ;)

Change your logic. Start with the array of numbers, being assigned to the array:

for(i=0;i<52;i++) {  //eliminate repeated numbers
  array[i] = i;
}
//now pick a random card
for(i=0;i<(NumPlayers * 5); i++) {
  j = rand() % 52 + 1; //range of 1 to 52
  hand[i] = array[j]; //a unique random number
}

I haven't run the above, but I believe it's OK. Notice how nice and easy it is to study it, when it's between tags.

You should ALWAYS use code tags - just click on the [code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] tags.

You should ALWAYS use code tags - just click on the icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.

Adak 419 Nearly a Posting Virtuoso

1) No, when I was working with big numbers, I had to "roll my own" handler for very large digits. GNU wasn't well known then (no www, and no internet for me).

2) Since 10 ^ 9 has 10 digits, and since 10 has only 1 one in it, 5 * (10^9) will also have 10 digits in it:

eg.
10 ^3 = 1,000 has 4 digits, and 5*(10^3) = 5,000
10 ^6 = 1,000,000 has 7 digits, and 5*(10^6) = 5,000,000

See the pattern?

A word about GNU:
They helped create Linux, which is more properly called GNU/Linux as a matter of fact. They have a religious conviction of the highest order, that anything worth saying, is worth saying in 10,000 words, while elucidating the complete specification of all 8.7 Billion species on planet Earth, and any other planets they're thinking of, atm.

I'd love to kick them one time in the arse, and ask them "Why can't you get to the effing point? But it would do no good - they'd just explain it in a 10,000 word speech, while elucidating the complete specifications of all 8.7 Billion species on ...

;)

Adak 419 Nearly a Posting Virtuoso

There are two ways:

1) Use a big number library. GNU's is well known.

2) Use an int or char array. Make each element hold just one digit of the number. Write a function for each operation you want done. Yes, you might want a matching array, just to hold the carry value.

You need to remember how arithmetic was done, before calculators! ;)

Adak 419 Nearly a Posting Virtuoso

He gives the example of a line of text, not having the entire word, like draw
ing.

Using fgets() and strstr(), for "drawing", you wouldn't find the word if it was split like this.

I agree that's a simple and sweet algorithm Walt, but it can't handle split words without "hurdles". ;)

Adak 419 Nearly a Posting Virtuoso

Welcome to the Forum, 1150! ;)

Scanf() leaves a newline in the keyboard buffer, and it's throwing a monkey wrench into gets().

The fix is to add this, right after the scanf() line of code:

getchar();  //pulls off one (newline in this case), char.
Adak 419 Nearly a Posting Virtuoso

I don't mean to complain, but after 41 posts manicaction, you should be putting your code between code tags on the forum, every time. Very few of us are going to spend time looking over code that looks like html.

Use fgets(), or don't, it's your choice. There's more than one way to do this, clearly.

Did you try Narue's code? Did you try Manicaction's code?

I just thought of a simple way that might appeal to you. Say the word being searched for is "draw".

open the file, and set int match = 0.

Until the end of the file is reached. In a loop, look for a 'd' char. When you find
one, match++, and check the next char. If it's an 'r', then match++, and continue looping. Break out of the loop when you reach a space, or a punctuation (use ispunct() ), or exceed the strlen() of the target word.

If you reach a newline, and the match variable equals the strlen() of the target word, then you've found an instance of the word, in the text. If the match variable is less than the strlen() of the target word, then go to the next line of text, and continue seeing if the char's match the next letter in the target word.

The first letter in the text that doesn't match the target word (except the above case), causes the program to break out of the loop.

Adak 419 Nearly a Posting Virtuoso

Very true, Narue.

The OP said he was searching in a "text file", which I took to mean standard lines and having complete words on each line.

If he has hyphenated words, or words that are arbitrarily split into a two lines of text, that would require more effort.

Adak 419 Nearly a Posting Virtuoso

I believe Narue has graciously answered your questions/concerns. Your point is well taken, that a literal searcher won't give you everything you need, without more coding work.

It seems that to get just the "ask" (whole word), you need logic to handle " ask", except in the case where the "a" in "ask", is the first letter in the line of text. Then it needs to match "ask ", "ask, ", or "ask. ".

Note that:
draw
ing

will certainly require extra code, because it simply isn't a word, until it's re-assembled by your code.

Imo, when using fgets(), make it 1/3rd larger than your biggest line of text, unless you know just what the correct size should be. Memory isn't that precious, and truncating data is usually the last thing you want to do.

Adak 419 Nearly a Posting Virtuoso

Ff the target word is found, you can do the rest of your code to handle the target word being found, and then simply break out of the searching loop.

If you want to find the target word 5 times, just break out when the counter == 5.

Seems quite straight forward.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Jamblaster! ;)

Always click on the [code ] icon and when it puts two code tags on your reply page, paste your code, between the code tags - easy to do since it puts your cursor right between them for you.

All comparison sorts except for 1 obscure one, require two loops - one nested inside the other. You are sorting one pass OK, but that is just one pass, not everything.

This is the pseudo code for a simple substitution sort (like a bubble sort, but a bit quicker/easier, and no bubbles. (not always quicker, but usually).

int temp

for(i...;; i++) {
  for(j=i+1;...; j++) {
     if(array[i] > array[j]) {
        do your swap of array[i] and array[j], and use a temp variable
     }
  }
}
Adak 419 Nearly a Posting Virtuoso

No, this something you can easily do yourself, and just involves a bit of problem solving. You NEED to develop your problem solving, or you can't program worth a darn.

Work through it by hand, and see what kind of loops you're using!

Put some sweat into it! ;) Don't look at the recursive code - don't look at any code, right now. Learn how YOU would solve for LCD, using a loop (which is very natural, btw)

Adak 419 Nearly a Posting Virtuoso

printf returns the number of chars printed, and you didn't print any objects, so it would return a negative number, as an error IF your compiler allowed it to compile.

Perhaps you were thinking of:

if((ch = printf(something here)) > 0)
  //semi-colon or other code goes here

That's the normal idiom for this kind of if statement, IF I understand what you want, correctly.

Adak 419 Nearly a Posting Virtuoso

Use fgets() to take in at least one line of text, at a time, into your buffer. Then use strstr() to look for the target word, in the buffer. You don't want to do this, char by char.

To make it more efficient still, search for string search in Wikipedia, and read up. The best way, depends on how long your target word is.

Adak 419 Nearly a Posting Virtuoso

If you ONLY want a C IDE and compiler for Windows only, I'd recommend Pelles C. It is ONLY for C, and ONLY for Windows (32 or 64 bit, so XP or Vista or Win7 are all fine).

I like it! ;)

Adak 419 Nearly a Posting Virtuoso

And don't be like me and keep trying to add an s into that format specifier! ;)

Adak 419 Nearly a Posting Virtuoso

This is your original code (in code tags, please use them):

char *dic[][40] = {
"atlas", "A volume of maps.",
"car", "A motorized vehicle.",
"telephone", "A communication device.",
"airplane", "A flying machine.",
"", "" /* null terminate the list */
};

The square brackets here: dic[][40], are not necessary.

You have an array of pointers to a 2D array of char's.

That's not quite what you need though.You just need:

an array of pointers to a row of char's (a row of char's is a 1D array of char's).

So, it should be:

char *dic[40] = {
"atlas", "A volume of maps.", "car", "A motorized vehicle.",
"telephone", "A communication device.", "airplane", "A flying machine.", ""
};
//the number of strings per line above, doesn't matter at all.

C will count the number of items in this array (the number of strings), and assign the correct number of pointers to handle it (if possible). Also, C will give each pointer, the proper address, so even though the strings are not the same size, the pointer for each string will point right to the first char in it's string.

It might be easier to think of this, like this:

for(i=0;array[i];i++)
  printf("%s", array[i]);

This is not a true 2D array, since the first dimension is all pointers, but pointers and arrays have many shared characteristics - to the point where you start thinking that arrays *are* pointers with "syntactic sugar", to make them easier to …

Adak 419 Nearly a Posting Virtuoso

Correct. As long as the malloc() calls are not returning NULL, you should be getting valid addresses - so that's not the problem.

Aren't you getting a warning about the variable p, l, and u, in lupSolve()? I don't see a declaration for them in that function. If they are globals (bad!), then it's very poor form to pass them around to the functions as parameters - those are copies of the global, and as such, their value will be lost when the function they're passed to, is done. (That is, they will return to their global instantiation, with their global values).

Adak 419 Nearly a Posting Virtuoso

You can't compare strings - you have to compare each char, one at a time. Include string.h and use strcmp(string1, string2).

strcmp(string1Name, string2Name) will return one of three values:

1) a positive number
2) zero
3) a negative number

That return tells you what the comparison of strings revealed:

1) string1 is greater than string2
2) the strings are equal
3) string2 is greater than string1

The comparison is made, based on your system's character set, not strictly according to the dictionary rules of alphabetizing. (not quite a lexicographic comparison)

If you don't have a good C reference book or help files, bookmark a C reference site in your browser. You will need it.

Adak 419 Nearly a Posting Virtuoso

OK, you're serious.

The for loop is the more complete loop. In fact the Go language from Google, doesn't have any loop other than the for loop. Anything you can do with a while loop, you can do with a for loop.

The while loop doesn't include an inital assignment to the format, so it's standard idiom to see one or more assignments before the while loop begins:

i=0;
while(i<10) {
  printf("%d\n", i);
  ++i;
}

The typical while loop includes just a test between it's parenthesis, but here's one with an assignment first:

while((ch=getchar()) != '\n') {
  countchar++;
}

The for loop syntax in C, requires more than just a single value in the while test:

i = 0;
while(1) {
  if(i++ == 9) break; //a break is required or it's an endless loop
}
for(1) { <<== illegal syntax

for(;;) { //legal, note the two semi-colons
  if(i++== 9) break;
}

So at a deep logic level, there is no difference between them. Anything you can do in a for loop, you can also do in a while loop, and vice versa.

In common usage, when you know the number of times the loop will need to iterate around, then a for loop is more intuitive. When you don't know how many times a loop will need to iterate, then a while loop is slightly clearer, and is usually used.

Adak 419 Nearly a Posting Virtuoso

Naturally, you can write data to a buffer you control - thought you understood that was no problem. It's getting the foo function to do it, that's the part I don't know.

I have always had full access and control of the functions, for the programs I wrote.

Adak 419 Nearly a Posting Virtuoso

Yes, exactly.

Adak 419 Nearly a Posting Virtuoso

Of course you do! ;)

That's a new one to me. I don't know how to do what you want. Any Google hits for "redirect stdout to a buffer, in C"?

Adak 419 Nearly a Posting Virtuoso

You're trying to print to the file or buffer, from within the IDE, aren't you?

That's not what you want. Run it from the command line.

Open a terminal (aka text window, command line interface, etc.) window. If you are not in the same directory as the exe of your program, change over to that directory. Now enter the name of your compiled file (your program), and add the redirect symbol:

myProgramsName > fileName

Your programs output to standard output, will be redirected to fileName file.

Redirection works for every operating system I am familiar with - Unix, Linux, DOS, Windows (all versions), BSD, etc.

If the program explicitly sends output someplace else, then this won't work.

Give that a try. ;)

Adak 419 Nearly a Posting Virtuoso

Try this:

//Before main() define a struct with 
struct countries {
  char name[50];
  int numNation;
}

//now make an array of these structs, in main() 
struct countries nations[500?];  
char myNation[50];

//and read in your data from a file, into the array of structs.

//Now a for loop can replace all the if else switch statements:

for(i=0;i<500?;i++) {
  if(nations[i].numNation = numBarcode) {
    strcopy(myNation, nations[i].name); //or print the country name here
    break;
  }
}

printf("%s\n", myNation);

;)

Adak 419 Nearly a Posting Virtuoso

Focus on one thing at a time - in this case, parsing the string.

Looks like -dH is the field identifier for your struct, where "H" represents a unique handle {w, Win, h, etc.}.

So make a list of these field id's: -dh, -dw, -dWin, etc., and use strstr() to return a pointer to them, wherever they are, in the string. Use that pointer to then assign the value they point to, to your struct fields.

You'll need to include <string.h> for strstr(), and use sscanf() to store your value (since it is formatted data) from a string). Make your buffer plenty large enough to handle one string - maybe BUFSIZ (which varies from system to system, but 512 chars size is common these days). (BUFSIZ is a macro).

Heaven help us, shared memory. You couldn't just pass the string to a function?

Anyway, one thing at a time, let's get the parse going locally, and then rest, later.

Adak 419 Nearly a Posting Virtuoso

I don't even know what the blazes you're doing with that code. *Argv[] is an array of pointers to char, so it should work fine with a string.

Forget code for a second. Show several strings as examples of your input. Then show the output that you want. Then show the pseudo code logic that you plan on using in your program to make that happen.

I know as a beginner, the C syntax can be a major hurdle, but ignore it for right now. Assume that you can get the syntax to work, with a little help, and it's not a problem. Concentrate on the logic, and program flow of that logic.

"Concentrate on the force, Luke!" ;)

Adak 419 Nearly a Posting Virtuoso

I haven't read through all the above posts, but here's my ideas:

1) Use structs for each player, and struct members (fields) for each attribute you need.

2) KISS. Don't go Rube Goldberg here! You don't need a damage attribute, because when a character is damaged, his strength or life force, is simply dropped a bit.

If you don't keep the game organization simple, you program will be much harder to extend & de-bug, and will run slower.

A good game has a good story to present - not a zillion variables and do-da's.

Adak 419 Nearly a Posting Virtuoso

Are you using a C compiler, or a C++ compiler?

In C++, &n would be a reference to n. I'm unsure how a C++ compiler would handle that reference, in a declaration.

In C, a declaration of &n is an error. These are the errors I get from my Pelles C compiler:

C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2014: Empty declaration.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2001: Syntax error: expected ';' but found '&'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2048: Undeclared identifier 'n'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2168: Operands of '=' have incompatible types 'int *' and 'int'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2088: Lvalue required.
*** Error code: 1 ***
Adak 419 Nearly a Posting Virtuoso

Yes, you did, and my warnings were not intended for your example, but for Pinkannu. I don't want to see code from the OP, until after the problem has been thoroughly studied, and the logic to solve it, has been found.

Adak 419 Nearly a Posting Virtuoso

PLEASE - don't start this with code - FORGET THE CODE FOR NOW!

Start with carefully studying the problem, in this case, the strings. KNOW those strings - know what are the separators between the fields, know how many fields they might have AT THE MOST.

STUDY & KNOW the problem, until you know how to solve it. Then set up the logic you used, to solve it. Put that logic into a flowchart or pseudo code, so each step of an algorithm to solve the problem, can be identified.

THEN write your code (which should spring right out from the pseudo code or flowchart).

Put off all the details, and get the flow of the program, and the functions you want for it, set up. Check your work as you go for any compiler warnings or errors, and for the accuracy of the program, so far.

Then add your details that you put off, and make any final little adjustments (typically, this will be to the look and feel of the interface).

Welcome to Top Down design! ;)

When you say:

A structure that assumes no name or value will be larger than 16 bytes

Who says you want char's? Why 16 bytes? That looks so much like a "magical number" you just pulled out of the hat.

Start with the strings, Luke! Turn off your target acquisition system in your X-wing fighter! ;)

Adak 419 Nearly a Posting Virtuoso

What is your field (struct member is also a field), separator? Hyphen, space, maybe both?

Here's a trick:
Initialize all your fields in the struct to 0 or '\0'.

Print out some strings. Put a vertical line between each field, with a pencil |.

Now you can loop through the buffer holding the string, as many times as you need to, parsing out one field at a time.

When it's all working for even the most difficult strings, then integrate the code enough to make it more efficient, but keep it clear enough that you can readily understand it.

You may need to change this code, if the string format ever changes in the future.