[Solved] I want to create a program in C to check for @ and . if typed in by user [closed]


The major problem your code is facing is not about C but about the logical structure of your function. If your emailadd contains any character different from @ and/or different from . you ask for a new email address.
That’s not what you want.

Let’s structure your problem. Here is what you want to do:

  1. Get user input
  2. Check if the input contains an @
  3. If not, ask for new input
  4. Check if the input contains a .
  5. If not, aks for new input

You don’t want to do this once, but as long the user input is correct. To be even more precise you want to get a . after the @ (otherwise an email address like stack.overflow@so would be valid.

Consider this approach which implements exactly the behaviour defined above:

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

int main()
{
    char emailadd[50];
    size_t emailaddL;   /* Use size_t instead of int */

    /* Get the email address for the first time */
    printf("Please enter your email address\n");
    scanf("%s", emailadd);
    emailaddL=strlen(emailadd);

    /* Loop while the address has a wrong format */
    while(1){
        /* Create flags: 0 character missing, 1 character found */
        int at_flag=0;
        int dot_flag=0;
        emailaddL=strlen(emailadd);

        /* Loop through the char array. You have to loop from 0 to the array lenght -1*/
        for(int i=0; i<emailaddL; i++){    /* You can declare variables in for loops */

            /* If an @ has been found, set the flag */
            if(emailadd[i] == '@'){
                at_flag=1;
            }
            /* If a . has been found and @ has been found earlier, set the flag */
            if(at_flag && emailadd[i] == '.'){
                dot_flag=1;
            }
            /* If both flags have been set, you can stop searching */
            if(at_flag && dot_flag){
                break;
            }
        }

        /* If no @ has been found, ask for it */
        if(!at_flag){
            printf("The entered email address is incorrect. Please re-enter your email address and it must contain the '@' specifier\n");
            scanf("%s", emailadd);
        /* If an @ has been found, but no ., ask for it */
        } else if(!dot_flag){
            printf("The entered email address is incorrect. Please re-enter your email address and it must contain the '.' specifier, after the '@' specifier\n");
            scanf("%s", emailadd);
        /* Otherwise leave the loop */
        } else{
            printf("Everything is fine!\n");
            break;
        }
    }
}

Let’s try some scenarios:

$ Please enter your email address
$ stack.overflow 
$ The entered email address is incorrect. Please re-enter your email address and it must contain the '@' specifier
$ stack.overflow@so
$ The entered email address is incorrect. Please re-enter your email address and it must contain the '.' specifier, after the '@' specifier
$ [email protected]
$ Everything is fine!

1

solved I want to create a program in C to check for @ and . if typed in by user [closed]