TommySVK
TommySVK

Reputation: 13

How to stop reading input when a certain character is reached

Let's assume somebody is writing some text. My program has to scan that text and then print all the characters under each other. However, it should only read the input until * appears. So when the input is "Hello*darling", it should only read "Hello". I used the * as an argument in while loop, but my program scans "Hello*" instead of "Hello". How do I get rid of the *?

#include <stdio.h>

int main()
{
    char c1;
    while (c1!='*'){
        scanf("%c", &c1);
        printf("c1: %c \n", c1);
     }
    return 0;
}

Upvotes: 0

Views: 3385

Answers (5)

alk
alk

Reputation: 70911

Using scanf() and doing complete error checking and logging:

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

int main(void)
{
  int result = EXIT_SUCCESS; /* Be optimistic. */

  {
    int r;

    {
      char c1 = 0;
      while (EOF != (r = fscanf(stdin, " %c", &c1)) && c1 != '*')
      {
        if (1 != r)
        {
          fputs("Invalid input. Retrying ...", stderr);
        }
        else
        {
          printf("c1: %c \n", c1);
        }
      }
    }

    {
      int errno_save = errno;

      if ((EOF == r) && ferror(stdin))
      {
        errno = errno_save;
        perror("scanf() failed");
        result = EXIT_FAILURE;
      }
    }
  }

  return result;
}

Upvotes: 1

John Bayko
John Bayko

Reputation: 1091

Use while or for when you want to check the condition at the start of the loop, do ... until if you want to check at the end. If you want to check in the middle, I prefer an infinite loop ("while(TRUE)" or "for(;;)") and using if/break in the middle. Taking your loop, that would be:

while (TRUE){
    scanf("%c", &c1);
    if (c1=='*') {
        break;
    }
    printf("c1: %c \n", c1);
}

Some people don't like that, an alternative is to make that a function, using return instead of break:

boolean get_and_print_if_not_end() {
    scanf("%c", &c1);
    if (c1=='*') {
        return true;
    }
    printf("c1: %c \n", c1);
    return false;
}

You'd call that in a basic while loop:

while (!get_and_print_if_not_end()) {
    // Nothing to do here.
}

Upvotes: 0

DorkCodingsign
DorkCodingsign

Reputation: 78

    #include<stdio.h>
    #include<ctype.h>
    int main()
    {
        char string;
        do
        {
            printf("String is :%c\n",string);
            if(scanf("%c",&string)!=1)
            {
                return 0;
            }
        }while(string!='*');
        return 0
}

Here:


First, it will get the string character and compare it to * character if the character is no found it will print the character. else it finds it will return 0 and the program will be eliminated.


Upvotes: 0

Blaze
Blaze

Reputation: 16876

You could switch the scanf and printf statements and put an initial scanf before the loop:

int main()
{
    char c1 = '\0';
    do {
        printf("c1: %c \n", c1);
        if (scanf(" %c", &c1) != 1)
            return -1;
    } while (c1 != '*');
    return 0;
}

Also note that as your program currently is, there's not just the issue with printing the *, but it's undefined behavior because c1 is uninitialized in the first run of c1 != '*'.

Upvotes: 3

Phantom
Phantom

Reputation: 875

You should look at getchar()

#include <stdio.h>

int main()
{
    int c1;
    while ((c1=getchar())!=EOF && c1!='*'){
       printf("c1: %c \n", c1);
    }
    return 0;
}

EDIT: and this way, there is no undefined behavior, because c1 is always initialized (see @Blaze answer) :)

Upvotes: 4

Related Questions