1

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.

3
  • have you tried using getch(); funtion before return 0; remember you include conio.h #include <conio.h> Commented Dec 23, 2013 at 6:48
  • 2
    You compare string using strcmp() in C, not == or !=. Commented Dec 23, 2013 at 6:53
  • Note: Recommend to always check the result of scanf() and family. Commented Dec 23, 2013 at 14:35

6 Answers 6

3
  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;   
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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"))

Comments

1

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;   
} 

8 Comments

Thank you. However my program still doesn't stop when I put in something like "10 10 stop" or "stop".
Dont include any space in scanf... that is, you have to use it like this scanf("%d%d%s",&a,&b,c);, there should not be space in "%d%d%s"
It says error C4996 when I try to use scanf instead of scanf_s.
@user3128376 : You can try using fgets separately for scanning the string.
@user3128376 : There is something called debugging the code, try that. 1) Print the value of c; 2) Check return value of scanf (or scanf_s). 3) Try assigning "stop" directly to c while initialization.
|
1

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.

1 Comment

+1 for fgets() and sscanf() and checking results. BTW: fgets(c, 32, stdin) is OK - no need for 32-1. fgets(c, sizeof c, stdin) is even better. Note: given that an int could be 64-bits these days, maybe a larger c[2*21+2].
1

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;   
} 

9 Comments

@0xF1 : Exactly. How I did not think about that?
I tried it this way however when I put N or n, it just asks me to enter the values again and won't quit.
@user3128376: Fixed. "Replaced || with &&"
I think it should be "if (c == 'N' || c == 'n')" And it won't quit when I put "n" or "N"
scanf_s expects the buffer size, so just replace with "scanf_s( "%c", &c, 1 ) ;"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.