简体   繁体   中英

How to stop a program based on user's input in C

I'm just starting to learn C and I'm having problem with stopping my program based on what the user inputted.

#include <stdio.h>
#include <stdbool.h>
int main()
{
int a;
int b;
char c[5];
printf("Enter the two values you like to compare, type stop to end.\n");
while (c != "stop")
{
    scanf_s(" %d %d %s", &a, &b, &c);
    if (!(a^b))
    {
        printf("both are equal\n");
        getchar();
    }
    else
    {
        printf("both are not equal\n");
        getchar();
    }
}
printf("Thanks for playing.");
getchar();
return 0;   
} 

The problem that I'm having is having to put in another variable, c, in my scanf_s. How would I do it so that the user does not have to put in another word after the 2 numbers? Also how can I check if the user only input "stop" so that it will stop the program? Btw the way I have it right now also does not stop the program when I do "10 10 stop". Thanks.

  1. remove & for c in scanf_s(" %d %d %s", &a, &b, &c);
  2. use strcmp to compare strings.
  3. if you need to ignore case while comparing use strcasecmp (for UNIX based systems) and stricmp (for windows).
  4. Use do-while instead of while if you need to run the loop at least once.

Full Working Code:

#include <stdio.h>
#include <string.h>
int main()
{
  int a;
  int b;
  char c[5] = {'\0'};

  do {
    printf("Enter the two values you like to compare, type stop to end.\n");
    scanf("%d%d%s", &a, &b, c);
    if (!(a^b))
      {
    printf("both are equal\n");
    getchar();
      }
    else
      {
    printf("both are not equal\n");
    getchar();
      }
  }
  while (strcmp(c,"stop"));
  printf("Thanks for playing.");
  getchar();
  return 0;   
}

while (c != "stop")

You cannot compare strings in C like that, use memcmp() or strncmp() library functions available in string.h . Read about them to know how they can be implemented as condition in while loop.

Also, to get string input, use
scanf_s(" %d %d %s", &a, &b, c); // Remove that litle '&' before 'c'.

NOTE: The function scanf_s returns the number of inputs scanned correctly, you should check that before proceeding with input values.

To get the user to stop without explicitly entering "stop", many ways are there:

1) Use do-while loop and keep asking user if he wants to play more.
2) Use negative numbers input (say -1 ) to quit.

Use line below to make sure your 'c' scan your string entered.

scanf(" %d %d %s", &a, &b, c);

Edit: Consider replacing your while with line below to make sure you stop works. Include "string.h"

 while (strcmp(c,"stop")) 

Here is the fixed version... I have add the comments in the code for understanding...

#include <stdio.h>
#include <string.h>
int main()
{
    int a;
    int b;
    char c[5] = {'\0'};
    printf("Enter the two values you like to compare, type stop to end.\n");
    while (strcmp(c,"stop"))
    {
        scanf("%d%d%s", &a, &b, c);
        if (!(a^b))
        {
            printf("both are equal\n");
            getchar();
        }
        else
        {
            printf("both are not equal\n");
            getchar();
        }
    }
    printf("Thanks for playing.");
    getchar();
    return 0;   
} 

Its because you are using &c instead of just c in scanf_s . Replace that and it should work. Also, c is a string , so, you have to use strcmp instead of != .

An easier way to write this code would be::

int main()
{
int a;
int b;
char c;
do
{
    printf("Would you like to play?\nPress 'Y' for 'Yes' or 'N' for 'No'\n");
    scanf( "%c", &c ) ;
    /*scanf_s( "%c", &c, 1 ) ; */
    if( c != 'Y' && c != 'y' )
        break ;

    printf("Enter the two values you like to compare\n" ) ;
    scanf(" %d %d", &a, &b);
    if (!(a^b))
    {
        printf("both are equal\n");
        getchar();
    }
    else
    {
        printf("both are not equal\n");
        getchar();
    }
}while(1) ;
printf("Thanks for playing.");
getchar();
return 0;   
} 

First, lets correct your program: &c (third argument to scanf_s) is incorrect. It should be c (because it is already a pointer). According to docs, scanf_s requires sizes to be specified for all %s format strings; therefore, the way you are doing things now, you should have written scanf_s(" %d %d %4s", &a, &b, c);

The program would be much easier to use if you changed your logic to "enter a blank line to exit". You can test this looking at the return value of scanf_s , which will be the number of format strings correctly read from the input.

If you need to ask for strings, then allow either, and look at whatever the user wrote to see whether it was number or string:

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

int main()  {
    int a;
    int b;
    char c[32];
    printf("Enter the two values to compare, or type stop to end.\n");
    while (fgets(c, 31, stdin) != NULL && strncmp("stop\n", c)) != 0) {

        // user did not request to exit and wrote something; maybe 2 numbers
        if (sscanf(c, "%d %d", &a, &b) != 2) {
            // but he did not write two numbers. Complain and repeat.
            printf("please write two numbers to compare, or type stop to end.\n");
            continue; 
        }

        if (a == b) {
            printf("both are equal\n");
        } else  {
            printf("both are not equal\n");
        }
    }
    printf("Thanks for playing.\n");
    return 0;   
} 

fgets reads whole lines, and you can then try to parse them using sscanf . This has the advantage over common scanf that you can try to parse the same line in different ways, depending on what you find in it. Generally, you do not want to fill your code with getchar() . If you are debugging, your debugger will stop for you. If you are not debugging, you will want to test or use your program with input redirection , and getchar() is simply not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM