-3

I have a little problem with a C-programm, I wrote. It should be like "you go in a room.. is there a wall? no? then move on.. is there a wall? yes? then turn around" and so on. I am stucked, I go in the room and turn around but do not know how to go further.

#include <stdio.h>
void main()
{
char answer[2];
answer[0] = "Y";
answer[1] = "N";


do
{
printf("Move!\n");
printf("Is there a wall?\n");
scanf("%s",answer);
}
while (answer[0] != 'Y' );
printf("Turn around!");

}

I read about loops and ifs, but my head do not make klick. Thanks for reading, hjerteblod

  • 2
    Any decent compiler should have complained when you tried to build that code. A `char` is a *single character*, `"Y"` is a *string* containing *two* characters (the character `'Y'` and the null-terminator character `'\0'`). And since you don't use the array `answer` before you read into it with `scanf`, you don't really need to initialize it. – Some programmer dude Jun 24 '20 at 07:43
  • On another note, the "turn around" output is printed *after* the loop, not inside. It will not be printed until the user input something other than `'Y'`. And [the `main` function](https://en.cppreference.com/w/c/language/main_function) *must* be declared to return an `int`. – Some programmer dude Jun 24 '20 at 07:46
  • Also, compile your program with `-fsanitize=address`, it will catch out-of-bounds memory accesses. – Paul Hankin Jun 24 '20 at 07:51
  • thank you for your quick answer, what do you mean with "must return an int"? I just started with C and feel like a total newbie. – hjerteblod Jun 24 '20 at 07:52
  • Read the [*Modern C*](https://modernc.gforge.inria.fr/) book, the documentation of your C compiler (e.g. [GCC](http://gcc.gnu.org/) ...) and of your debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)) and [*How to debug small programs*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)). See http://norvig.com/21-days.html – Basile Starynkevitch Jun 24 '20 at 08:20
  • How do you actually learn C? It seems that your source of knowledge needs to get swapped. – RobertS supports Monica Cellio Jun 24 '20 at 09:12

3 Answers3

1

Assuming you want to never exit the game. If so, try this code -

#include <stdio.h>
int main()
{
    char answer;

    while(1){
        printf("Move!\n");
        printf("Is there a wall?\n");
        scanf(" %c", &answer);
        if (answer == 'Y'){
            printf("Turn around!\n");
        }
    }

    return 0;
}

ZBay
  • 352
  • 1
  • 6
  • 17
  • hey ZBay, yeah thank you! But my acutal problem is, that it should not end it I find a wall. Instead it should turn around and find the next wall. Never ending loop I mean – hjerteblod Jun 24 '20 at 08:21
  • It was unclear from your code. Hope you have managed to solve it. – ZBay Jun 24 '20 at 08:25
  • I am sorry, yeah my problem is the loop. It should not stop after one wall, because the room has 4 walls. unfortunately I did not solved it yet – hjerteblod Jun 24 '20 at 08:30
  • big thanks, I tried switch cases now but your solution is much more ... handy? thank u! – hjerteblod Jun 24 '20 at 08:43
0

"Y" is a string with two characters, answer[0] is a character, you cannot assign a string to a character. So you can change this code like this answer[0] = 'Y'.

By the way, you are better to return an int value in the function main.

include <stdio.h>

int main(){
    // your code here

    return 0;
}
guapi
  • 123
  • 7
0
  1. "N" is a string literal, which evaluates to a pointer to the first element of the string literal "Y" in memory. If you try to assign it to answer[0] you invoke undefined behavior, since you assign a address value to char objects.

    The same goes for answer[1] = "N";.

    Both assignments, answer[0] = "Y"; and answer[1] = "N"; should have give you a warning, like this from GCC:

    warning: assignment to 'char' from 'char *' makes integer from pointer without a cast

    Never ignore compiler warnings.

  2. You don't need an array at all, use a single char for answer.

  3. You need an infinite loop for always going further or turning back from a wall, here implemented by while (1).

Note that this is an unfinished algorithm. In a real production program you need a condition to break out of the loop. But just for the sake of your issue, here is want I think you need:

#include <stdio.h>

int main (void)
{
   char answer;

   while (1)
   {
      printf("Move!\n");
      printf("Is there a wall?\n");

      if ( scanf(" %c", &answer ) != 1 )
      {
          fputs("Error at input!", stderr);
          return 1;
      }

      if ( answer == 'Y' )
      {
          printf("Turn around!\n");
      }
   }  
}

Side Notes: