Program scipping input

So I am working on this console game and so far I am still at the beginning of making everything. I ran in to a problem with printing a 2d char array on the screen. I print it on the screen, then get input from the user. Everything is in side a do..while loop that can't stop (for now) and the array gets printed out a number of times before asking for input instead of asking for input instantly. Why is this happening

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>

void generateworld(char (*map)[30][40]) {
  int chance;
  for (int y = 0; y < 30; ++y) {
    for (int x = 0; x < 40; ++x) {
      (*map)[y][x] = 178;
    }
  }

  for (int y = 0; y < 30; ++y) {
    for (int x = 0; x < 40; ++x) {
      chance = rand() %100 +1;
      if (chance < 11) {
	(*map)[y][x] = 176;
      }
    }
  }
}

void printworld(char map[30][40]) {
  for (int y = 0; y < 30; ++y) {
    for (int x = 0; x < 40; ++x) {
      printf("%c", map[y][x]);
    }
    printf("\n");
  }
}

int main(void) {
  int exit;
  int game_over;
  unsigned int seed;
  char direction;
  char name[10];
  char world[30][40];

  printf("Welcome to CRPG\n");
  printf("\nEnter your character name.(max 10 characters)\n");
  printf(">");
  scanf("%10s", &name[0]);

  do {
    exit = 0;
    game_over = 0;

    printf("\nEnter a series of numbers and letters that effect how your world is generated.\n");
    printf(">");
    scanf("%d", &seed);
    srand(seed);
    generateworld(&world);

    do {
      printf("\n\n\n\n");
      printworld(world);
      printf(">");
      scanf("%c", &direction);
    } while(game_over == 0);
  } while(exit == 0);

  return 0;
}
Last edited on
Do you have to use C? This will be much easier using C++.
I don't have you no, I just perfer C. Always thought because C++ is backwards compatible with C it would never matter here.

EDIT: is using C causing my problem?
Last edited on
Uh oh. Everything you write in C you can run in C++, but it doesn't apply vice versa. Writing a game - especially an RPG game - would be much easier and shorter using classes.

By the way, just three things:
1) It is a good custom not to declare variables in a for loop header.
2) You write scanf ("%10s", &name[0]); as scanf ("%10s", name);, but that's just a cosmetic thing.
3) If your character name is 10 symbols long, it will actually take 11 characters (10 characters for the name and 1 for terminating character), so you should declare char name[11];
Last edited on
Oh I know you can't use C++ code in C. I meant people here would still be able to read the code with C being built in to C++

I only want the variables to be accessible in the for loops so I can keep using the same names is why I declare them in the for loop.

when I use scanf ("%10s", name); I am pretty sure I get warning and read to use scanf ("%10s", &name[0]);

EDIT: Never mind, I didn't notice you were not pointing to the variable you passed the actual variable in.

Thanks about the name length part

Now still. Why does the char array get printed out a number of times before asking for input even though it shouldn't?
Last edited on
It doesn't when I build it.

Welcome to CRPG

Enter your character name.(max 10 characters)
>eggs

Enter a series of numbers and letters that effect how your world is generated.
>   
No not that spot, I'm talking about just after printing the 2d char array on the screen. This is inside the do..while loop.
Sorry for the delay, I went to sleep. :) Anyways, your problem is (I presume) that you enter a string instead of numbers when it asks you for a seed. That way scanf won't accept it and the whole string (I mean the part of the string which begins with a non-number) stays in buffer, which is then passed to the next scanf. So when it asks you for a seed and you enter for example 123hello, 123 is processed and "hello\n" stays in buffer. Then the game asks you for a direction and then 'h' is passed from buffer (why only 'h'? Because you are asking for a single character with %d). Then it checks if the game is over and since it is not, it loops again. Then it passes 'e' and so on.

So, in short, enter only numbers. But even so, the game will print the map twice. Why? :) Because if you enter 123, it actually stores "123\n" and '\n' is not a number again. So after the scanf which gets seed, you should have getc (stdin); to get rid of the '\n' from buffer. :) stdin stands for standard input.

EDIT: About scanf and passing a char array to it - let's have char array [10]. Now when you do array[0] = 'a', it's the exactly same thing as *array = 'a'. Similarily array[5] = 'b' is the same thing as *(array + 5) = 'b'. array[i] = 'c' is thus the same as *(array + i) = 'c'. This is why scanf ("%s", name) does the same thing as scanf ("%s", &name[0]). I did not "pass the actual variable," as you said, I did the same thing as you did - pointed to the beginning of the array. I think you can see now why it is better to write it the first way - it is shorter and cleaner. :)

About the for loops and declaring variables. There is no problem having for example
1
2
3
4
5
6
7
8
9
10
11
int i;

for (i = 0; i < 10; i ++) {
   ...
}

...

for (i = 0; i < 20; i ++) {
   ...
}


As you can see, you can still use the single variable i, because you always set its value again in a for loop header. Some compilers won't even let you compile your program if you declare a variable inside a for loop header, some will give you a warning.
Last edited on
Change line 50 to

scanf("%d%*[^\n]", &seed);

That removes everything in the input buffer up to and including the next newline, and stop asking for "a series of numbers and letters" when you mean "a number."

http://wpollock.com/CPlus/PrintfRef.htm is a decent reference for scanf.


Berzerger wrote:
Everything you write in C you can run in C++


Untrue.
Could you please write an example of a code which you can compile with gcc and not with g++?
1
2
3
4
5
void main()
{
  void* ptr;
  int *i = ptr; 
}
1
2
3
4
5
void func();
//...
func(2, 3, 4);
//...
void func(int a, int b, int c){}
Without nonstandard language extensions in g++?

1
2
3
4
5
6
7
8
9
void f(int i)
{
      char faduddle[i] ;
}

int main()
{
    f(4);
}


legal C, illegal C++.
Last edited on
I stand corrected. :)
Topic archived. No new replies allowed.