1

I have already read the C string function man pages. The best thing I found was "strcmp" and "strncmp". But that's not what I want because it compares two strings. I just want to compare one like this...

char input[3];
    do {
        //important code
        printf ("You want to continue??);
        fflush(stdin);
        gets(input);
       } while (input == "yes" || "Yes);

It doesn't work. I would need to not use string and just a char like: "y" for yes. Is there a way I can do what I want? How can I compare just this one string with these values?

Jeff Holt
  • 2,940
  • 3
  • 22
  • 29
Speyke
  • 13
  • 3
  • Are you getting an error? If so, what is it? – Eric May 24 '17 at 06:02
  • 1
    You want to compare two strings. It doesn't matter that one of them is const. == would only compare their addresses, so always evaluates false for different objects. –  May 24 '17 at 06:05
  • No errors, but the program simply don't do what is expected, it keeps on the do while and don't go further on the code. So the program gets stuck there you see... – Speyke May 24 '17 at 06:05
  • Don't [`fflush(stdin)`](https://stackoverflow.com/a/7898516/694733). – user694733 May 24 '17 at 06:19
  • If you want to compare a one-character response, you could do something like `while (input[0] == 'y' || input[0] == 'Y';`. – Steve Summit May 26 '22 at 14:27
  • Don't declare a tiny array like `char input[3];` to hold a line the user's going to type — the user might type more than you expect. And never use the `gets` function to read such lines, because you can't tell it how big your array is, so it can't be careful not to overflow it. – Steve Summit May 26 '22 at 14:29

3 Answers3

2

If you want to make string comparison, use strcmp. You will compare just one variable, as you want it, but with two different values. Change line :

while (input == "yes" || "Yes");

to :

while ( strcmp(input,"yes") == 0 || strcmp(input, "Yes") == 0);

Also, change :

char input[3];

to :

char input[4];

as you need to take into account the terminating \0 character.

Marievi
  • 4,951
  • 1
  • 16
  • 33
  • Thanks a lot! You sure the extra char is needed cuz of the null "\0" stuff about arrays? How I give you reputation?? I cant vote yet, sorry! – Speyke May 24 '17 at 06:10
  • @Speyke every string ends with the terminating character `\0`, yes :) Glad I helped! You gave me reputation by accepting my answer, if you want to upvote, you can press the upper triangle at the left of the answer :) – Marievi May 24 '17 at 06:21
  • You should not change `input[3]` to `input[4]`. You should change it to `input[100];`. Memory is cheap, and users sometimes type more than you expect. – Steve Summit May 26 '22 at 13:59
  • Also you should *never* use a function that, like `gets`, reads arbitrary-length input into an array which you're not able to tell it the size of. In such a case, there's no way to prevent buffer overflow. – Steve Summit May 26 '22 at 14:00
1

Here I wrote a simple solution for what you're trying to achieve.

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

int main()
{
    char input[4];

    do {
        printf ("You want to continue??");
        fflush(stdin);
        gets(input);
    } while (strcmp(input, "yes") == 0 || strcmp(input, "Yes") == 0);

    return 0;
}
Vic
  • 301
  • 1
  • 9
0

Most likely you are going run into newline character that gets carried over to next input. Here is a solution for that.

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
   char input[4], garbage[50];
   do 
   {
      //important code
      printf ("You want to continue??");
      scanf("%s", input);
      fgets(garbage, sizeof(garbage), stdin); //Newline char is now stored into garbage.
    } while (strcmp(input,"yes") == 0 || strcmp(input,"Yes") == 0 );

   return 0;
}
Nguai al
  • 958
  • 5
  • 15