0

I just recently starts learning C and found a problem. So here is my code:

#include <stdio.h>
#include <ctype.h>
int main()
{
char email[100];
int i;

printf("Input username (email)    :");
    scanf("%s", email);

    for (i = 0; email[i] != '\0'; ++i)
    {
        if(!ispunct(email[i])) 
        {
          printf("Please input a correct username (email)    :");
          scanf("%s", email);  
        }
    }


    return 0;
}

So, the program somehow only finished if I only input 1 character like @ or other punctuation, but if I add alphabet like jack@gmail.com it will loop forever until I input only 1 punctuation. so can somebody please tell me whats wrong? I am trying to make a program that will only loop if I only input alphabet and didn't input punctuation like @ or . in my email.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Smarkite
  • 109
  • 6
  • You should not immediately scan the new input after the first iteration of the loop, you need to let that loop finish – Sourav Ghosh Dec 02 '19 at 14:45
  • 3
    Checking whether an email address is syntactically valid is [much harder than you might realize](https://davidcel.is/posts/stop-validating-email-addresses-with-regex/). – zwol Dec 02 '19 at 14:54
  • 2
    Also, [never use `scanf` to read input interactively](https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf). In fact, just never use `scanf`. – zwol Dec 02 '19 at 14:55
  • You are checking whether each character is punctuation. – S.S. Anne Dec 02 '19 at 18:25

2 Answers2

3

There are couple of suggestion:

  • use strchr() to find a particular charcater.
  • Loop over the same input instruction until you find a satisfactory input.

A modified version of your code can be

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

int main(void)
{
    char email[100];
    char toBeFound = '@';  // instead of any punctuation, make your search precise

    while (1){              // go for a loop till the valid input is received.
        printf("Input username (email)    :\n");
        scanf("%99s", email);  //length limit the input, to avoid buffer overflow problem.


        if (strchr (email, toBeFound)){  // search whether the required character is there 
                                         //in the input or not
            printf ("%s is a valid email address\n", email);
            break;
        }

        printf ("The input %s is not valid\n", email); // for debug, can be removed.
    }

   return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

Your program start the loop with i = 0 and test the first character in email. If this first character is not a punctuation, your program asks for a new email. So your program keeps testing only the first character and it requires that it’s a punctuation.

You should accumulate the result of the ispunct test and test the result.

#include <stdio.h>
#include <ctype.h>
int main()
{
    char email[100];
    int i;
    printf("Input username (email)    :");
    scanf("%s", email);
    for () {
        int done = 0;
        for (i = 0; email[i] != '\0'; ++i)
            done |= ispunct(email[i]);
        if (done != 0)
            break;
        printf("Please input a correct username (email)    :");
        scanf("%s", email);  
    }
    return 0;
}
chmike
  • 20,922
  • 21
  • 83
  • 106