0

I am studying the c language in our class I create some code to find a some number inside other array but when I try it with char value my code is not working in same as working with int value.

This is my code:

#include<stdio.h>

int main () {
    int  num, i, j;
    char a[99], ele;

    printf("Enter the Character element:");
    // get the length of array by the num value
    scanf("%d", &num);

    printf("Enter the values: \n");
    // a loop for getting values for our a[array] line by line
    for ( i = 0 ; i < num ; i++ ) {
        // get value by index i for array a index by index
        //printf("%d\t", (i+1));
        //if ( i + 1  == num ) {
        //    scanf("%c", &a[i]);
        //} else {
            scanf("%c", &a[i]);
        //}

    }

    printf("Enter the Character elements to be searched:");
    // get the value for ele, to use ele for searching inside our a[array]
    scanf("%c", &ele);

    // we need to set i to 0 for our while loop
    j = 0;
    // use the while loop to
    while ( j < num && ele != a[j]) {
        j++;
    }
    if ( j < num ) {
        printf("Character found at the location = %d\n\n\n", j + 1);
    } else {
        printf("Character Not Found!\n\n\n");
    }
    return 0;

}

I try to fixed many time but each time I get error, so the above one is working but it scape the some input value during input.

bruceg
  • 2,433
  • 1
  • 22
  • 29
Nazari
  • 438
  • 1
  • 9
  • 20
  • 1
    Please add an example input where you're code doesn't do what you expect – Ingo Leonhardt Jul 06 '18 at 15:38
  • if I add \n after %c in this line (scanf("%c", &a[i]);) the input will fixed but after inputing the last value the second scanf this one (scanf("%c", &ele);) dos not work and the program going to be end. – Nazari Jul 06 '18 at 15:41
  • 2
    Think carefully about *every* key pressed on your keyboard when running this program. There's much more than just alpha-numeric characters going on here, starting with pressing Enter upon input of your number `num`. [Read this answer](https://stackoverflow.com/a/36504282/1322972). – WhozCraig Jul 06 '18 at 15:51
  • 2
    please take some time to understand the `scanf()` behaviour –  Jul 06 '18 at 16:14
  • The takeaway here is that , for good reason, the `%c` directive differs from most other `scanf` directives in more than just the type expected for the corresponding variable. – John Bollinger Jul 06 '18 at 16:24
  • "but each time I get error". Compile error? Runtime error? Explain this error please. – Burstful Jul 06 '18 at 16:28

2 Answers2

2

Thank you WhozCraig, user3121023 for your advices, the whitespace in scanf(" %c", &a[i]); and scanf(" %c", &ele); make some unwanted inputs so this is a my code and its working like charm. :))

I just add space before the %c and every thing is okay.

#include<stdio.h>

int main () {
    int  num, i, j;
    char a[99], ele;

    printf("Enter the Character element:");
    // get the length of array by the num value
    scanf("%d", &num);

    printf("Enter the values: \n");
    // a loop for getting values for our a[array] line by line
    for ( i = 0 ; i < num ; i++ ) {
        // get value by index i for array a index by index
        printf("%d\t", (i+1));
        scanf(" %c", &a[i]);


    }

    printf("Enter the Character elements to be searched:");
    // get the value for ele, to use ele for searching inside our a[array]
    scanf(" %c", &ele);

    // we need to set i to 0 for our while loop
    j = 0;
    // use the while loop to
    while ( j < num && ele != a[j]) {
        j++;
    }
    if ( j < num ) {
        printf("Character found at the location = %d\n\n\n", j + 1);
    } else {
        printf("Character Not Found!\n\n\n");
    }
    return 0;

}
Nazari
  • 438
  • 1
  • 9
  • 20
0

There is a problem in your program that causes surprising behavior:

  • scanf("%d", &num) reads a number from standard input but leaves the newline typed by the user pending as the next character to be read.
  • Further calls to scanf("%c", &a[i]); read this pending newline and num-1 more characters from the user, leaving the rest of his input pending...
  • The final scanf("%c", &ele); reads whatever byte is pending in stdin, either a character from the user or the pending newline.

Hence the program behaves as if it does not execute the last scanf().

You should read input from the user one line at a time with fgets() or flush the pending newline with a loop:

#include <stdio.h>

int main() {
    int num, i, c;
    char a[99], ele;

    printf("Enter the number of character:");
    // get the length of array by the num value
    if (scanf("%d", &num) != 1 || num < 0 || num > 99)
        return 1;

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    printf("Enter the characters on a single line: \n");
    // a loop for getting values for our a[array]
    for (i = 0; i < num; i++) {
        if (scanf("%c", &a[i]) != 1)
            return 1;
    }

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    printf("Enter the character to be searched: ");
    // get the value for ele, to use ele for searching inside our a[array]
    if (scanf("%c", &ele) != 1)
        return 1;

    // consume the rest of the line typed by the user
    while ((c = getchar()) != EOF && c!= '\n')
        continue;

    // use a for loop
    for (i = 0; i < num && ele != a[i]; i++) {
        i++;
    }
    if (i < num) {
        printf("Character %c found at the offset %d\n\n\n", ele, i);
    } else {
        printf("Character %c Not Found!\n\n\n", ele);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189