0

I'm just a noob/newbie in the C/programming world. I'll first post the code and then I'll describe my problem.

#include <stdio.h>
#include <string.h>

int
main ()
{
  char f[30], j[30] = "mount everest";
  int s,a=0;
  printf ("\n");
  printf ("2) Which is the tallest mountain in the world?\n");
  printf ("\n");
  printf ("Please type your answer below\n");
  scanf("%[a-z]s",f);
  s = strcmp (f, j);
  printf ("\n");
 
  if (s == 0)
    {
      printf ("Congratulations! You have recieved a point!\n");
      a++;
    }
  else
    {
      printf ("Sorry! Better luck next time!");
      a--;
    }
  
  return 0;
}

So when I try to type the answer as "mount everest", the output goes directly to my 'else'(Sorry! Better luck next time!) statement. Also, I want the program to only accept small letters(as I've defined it a strict rule to be followed throughout the quiz).

**OUTPUT** 

2) Which is the tallest mountain in the world?

Please type your answer below
mount everest

Sorry! Better luck next time!

But when I use (not)'!s' instead of 's' in my if condition, it fulfills the if condition and prints the message as 'Congratulations! You have recieved a point!'. I really don't know what is going on? I had the same approach for my other 2 questions and they all worked perfectly without this '!' issue.

#include <stdio.h>
#include <string.h>

int
main ()
{
  char f[30], j[30] = "mount everest";
  int s,a=0;
  printf ("\n");
  printf ("2) Which is the tallest mountain in the world?\n");
  printf ("\n");
  printf ("Please type your answer below\n");
  scanf("%[a-z]s",f);
  s = strcmp (f, j);
  printf ("\n");
 
  if (!s == 0)
    {
      printf ("Congratulations! You have recieved a point!\n");
      a++;
    }
  else
    {
      printf ("Sorry! Better luck next time!");
      a--;
    }
  
  return 0;
}
**OUTPUT**

2) Which is the tallest mountain in the world?

Please type your answer below
mount everest

Congratulations! You have recieved a point!

So someone can please explain this to me of what is happening(as I'm new so I kinda don't know how to understand debugging) and what am I doing wrong here? Also, if you cannot understand from this section of the code, I'll post the whole code here but for now I thought it would be too overwhelming so if u want then pleasedo let me know! Thank you for taking the time to go through my problem. Any help is very much appreciated.

EDIT

I programmed the code again with the help of such useful comment from you guys/contributers(thank u all for taking the time to help this newbie) now I'll show you the revised/new code first:

#include <stdio.h>
#include <string.h>

int
main ()
{
  char f[30], j[30] = "mount everest";
  int s,a=0;
  printf ("\n");
  printf ("2) Which is the tallest mountain in the world?\n");
  printf ("\n");
  printf ("Please type your answer below\n");
  fgets(f,15,stdin);
  printf("%s\n",f);
  printf("%s\n",j);
  printf ("\n");
  s=strcmp(f,j);
  if (s==0)
    {
      printf ("Congratulations! You have recieved a point!\n");
      a++;
    }
  else
    {
      printf ("Sorry! Better luck next time!");
      a--;
    }
    
  
  return 0;
}

But now I'm again being thrown back at the else condition. Like this:

Output

2) Which is the tallest mountain in the world?

Please type your answer below
mount everest
mount everest

mount everest

Sorry! Better luck next time!

I know there is an issue with the 'if' condition but can't seem to figure out what it is. So if anyone can detect my error then I would be really glad. Once again thank you all for helping me.

  • 1
    strcmp return 0 if string matches, you wrote your condition is `!s == 0` <- this is incorrect. condition should be `s==0` – Popeye Nov 04 '21 at 06:20
  • Debugging tip: use `printf("[%s]\n", f)` to see what was stored in `f`. – user3386109 Nov 04 '21 at 06:20
  • 1
    `%[a-z]` only reads letters; there's a space between `mount` and `everest`. One possible fix is to add a space to the character set. Note, too, the `%[…]` is a scan-set and is complete; it is not a modifier for `%s` so the `s` in `%[a-z]s` is not wanted. In general, it causes problems, though you won't notice them here. – Jonathan Leffler Nov 04 '21 at 06:24
  • 1
    I'll use the printf("[%s]\n", f) and will let you know. Thank you so much! – Black Apathy Nov 04 '21 at 06:24
  • @user3386109 I used the printf("[%s]\n",f); and it gave me the ouput as [mount]. Dunno why? – Black Apathy Nov 04 '21 at 06:30
  • 1
    The `"%[a-z]"` conversion will stop when it finds any character that isn't a lower case letter. Which means it stops when it finds the space character after `mount`. You could try adding the space character to the set of allowed characters, e.g. `"%[a-z ]"`. Personally, I would use `fgets` to read the whole line ([but you'll have to remove the newline that `fgets` puts in the buffer](https://stackoverflow.com/questions/2693776)). – user3386109 Nov 04 '21 at 06:38
  • Get rid of scanf and use fgets. – Lundin Nov 04 '21 at 07:55
  • @user3386109 I used the fgets function and now it is printing the and that I needed but...it is still telling me that the ans is wrong as it is going in the else statement. This the new modified code: https://onlinegdb.com/CUCY4jyKjJ ``But the output is this: 2) Which is the tallest mountain in the world? Please type your answer below mount everest mount everest mount everest Sorry! Better luck next time! `` What am I doing wrong here? – Black Apathy Nov 04 '21 at 07:56
  • @BlackApathy Keep in mind that fgets doesn't discard the \n so you have to do that manually. Here's the FAQ: https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – Lundin Nov 04 '21 at 07:57
  • I thnk Lundin is correct that the newline wasn't removed. That's the reason for the square brackets in `printf("[%s]\n", f);` The brackets let you see if there's a newline in the string. – user3386109 Nov 04 '21 at 16:44

2 Answers2

1

The reason why your program is not working is because your scanf is not taking the whole line as input. Instead, it is taking the first word. Try to use scanf("%[^\n]%*c",f);

In future when your code is not working, try to print and verify the input you're receiving and validate from it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0
scanf("%[a-z]s",f);

should be

scanf("%[a-z ]",f);

%[a-z] means look for a nonempty sequence of lower case characters. You forgot to tell it to look for space.

Also %[a-z]s means look for a nonempty sequence of lower case characters followed by an 's'.

Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
mxbadger
  • 1
  • 1