0

Program executed successfully but it doesn't continue. Please help. It works when I take y or n as character.

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[20], n, i, sum = 0;
    char c[10];
    clrscr();
    do
    {
        printf("How many numbers you want to sum: ");
        scanf("%d", &n);
        for (i = 0; i < n; i++)
        {
            printf("\nwrite the number: ");
            scanf("%d", &a[i]);
            sum = sum + a[i];
        }
        printf("\nSum of numbers are %d", sum);

        printf("\nDo you want to continue. yes or no:  ");
        scanf("%s", c);
    } while (c == "yes");
    if (c == "no")
        printf("\nThanking You");
    else
        printf("\nWrong choice selected. Please select correct one");

    return 0;
    getch();
}

The error is shown below. How many numbers you want to sum: 3

write the number: 1

write the number: 2

write the number: 3

Sum of numbers are 6 Do you want to continue. yes or no: yes

Wrong choice selected. Please select correct one [Program finished]

Suraj
  • 91
  • 1
  • 7
  • 1
    Use [`strcmp()`](https://man7.org/linux/man-pages/man3/strcmp.3.html) to compare C strings. – MikeCAT Apr 27 '21 at 10:36
  • It means we can't compare two string with == in condition. We have to use `strcmp()` function of string to compare. – Suraj Apr 27 '21 at 10:48
  • @Suraj Exactly. Since `string`s are just a pointer to a char array in c, the `==` operator is working on the pointer values themselves (the addresses of both strings), one would be the address of `n` and the other would be the address of the string constant `yes` for example, stored globally outside of the code section. `char` in c is stored as an integer value 1 byte in size, which can be compared using the `char`'s `==` operator. This is why in order to compare strings you want to use a function like `strcmp()` which traverses over both strings and checks the equity of their values. – Irad Ohayon Apr 27 '21 at 10:54
  • Got it. Thanks a lot sir. – Suraj Apr 27 '21 at 11:05

0 Answers0