Ive been working on a Game of Life simulator in C for my programming class. Essentially, it needs to take a single line of input and print out a nice grid of x's and o's (representing live cells and dead cells, respectively).

I need to input the grid size, the number of generations to run, if it prints every generation, whether it pauses after each generation, and then the initial configuration on the grid.
To start, here's what I have for the first 5 requirements:

int maxx;
int maxy;
int gens;
char prints;
char pauses;
scanf( "%d %d %d %c %c", &maxx, &maxy, &gens, &prints, &pauses);

Thats simple enough. But this doesnt account for the last requirement. My question is how to input a string (not just a single character, like the n's) and save it in a character array. Here's what I tried to start:

char input[81];
scanf( "%c\n", &input[81] );

But it only copies one character, and I dont know how to get the whole line of input. Should I be using fget instead of scanf? I think I have the rest of the program working fine, Im just caught up on the input, arguably the most important (And possibly simplest) part.

Ive only had this class for about two weeks, so Im not very proficient in the language. Any prods in the right direction are appreciated! Im asking for just enough to help me understand how to incorporate arrays into my input stream.

Recommended Answers

All 6 Replies

>Should I be using fget instead of scanf?

if ( fgets ( input, sizeof input, stdin ) == NULL ) {
  /* Handle input error */

>scanf( "%c\n", &input[81] );
For the record, input[81] is an invalid index because the size of your array is 81. Valid indices range from 0 to n-1. Thus, 80 is the last valid index for your array.

Another option could be to use command line arguments

int main(int argc, char* argv[])
      // The values can be retrieved from argv. These values can be int, float, string. 

try using

scanf( "%s\n", &input );
scanf( "%s\n", input);

instead of

scanf( "%c\n", &input[81] );

>try using
Oh, this should be good.

>scanf( "%s\n", &input );
No, sir. There are four distinct problems with this line:

  1. &input is incorrect because it's a type mismatch. scanf expects a pointer to char, not a pointer to an array of char. Just because it happens to work for you doesn't mean it isn't undefined behavior.
  2. Any whitespace in the format string will cause scanf to expect, well, any whitespace. That is, any type and any number of whitespace, and it reads whitespace until there's no more. End result, scanf will wait until you type a non-whitespace character.
  3. Never use the unadorned %s specifier. In fact, forget it exists, because it's gets in disguise. Instead, make sure to specify the number of characters you want to read (minus the terminating null character) as a field width.
  4. It's especially important that you check for errors when gathering data from an unsafe source.

Here's the correct call:

if ( scanf ( "%80s", input ) != 1 ) {
  /* Handle an input error */

Thanks all, I managed to get command line inputs working. I had a few complications along the way, but it now works as intended. I appreciate all the help!

char a[20];

commented: Epic fail. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.