looping scanf

This is a discussion on looping scanf within the C Programming forums, part of the General Programming Boards category; There is a program, that needs input char email(variabel), must contain @ and .(fullstop),, and @ cann't directly beside fullstop.. ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68

    looping scanf

    There is a program, that needs input char email(variabel), must contain @ and .(fullstop),,
    and @ cann't directly beside fullstop.. how is the way to make something like that?
    if we input something(which don't contain @ and . it will ask to input again, until it has both ofi t)..
    Last edited by Kinshara; 10-29-2009 at 06:07 AM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    i just wrote a simple code snippet take a help from this

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int verify(char* email) {
      int result = 1;
      if (!strstr(email, "."))
        result = 0;
      else if (!strstr(email, "@"))
        result = 0;
      else if (strstr(email, "@.") != 0 ||
    	       strstr(email, ".@") != 0)
        result = 0;
      else
        result = 1;
      return result;
    }
    
    int main() {
      while (1) {
        char email[50];
        memset(email, 0, 50);
        printf("Please enter an email\n");
        gets(email);
        if (!verify(email))
    	    printf("email is invalid\n");
        else
    	    printf("email is valid\n");
      }
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    WAW!! GREAT... can you explain me? hehehe...
    I have just beginner for programming(at least 2 month, or 3 I forgot)...

  4. #4
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    Why so many ifs in verify? Why not clump them together with ||s:

    Code:
    int verify(char* email)
    {
        if fail conditions
            return 0;
        else
            return 1;
    }

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Epy View Post
    Why so many ifs in verify? Why not clump them together with ||s:

    Code:
    int verify(char* email)
    {
        if fail conditions
            return 0;
        else
            return 1;
    }
    You Can do that

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Quote Originally Posted by Epy View Post
    Why so many ifs in verify? Why not clump them together with ||s:
    owhhh.. you mean, all condition combine into one by using ||?? so if one false, the result will false too?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    
    #define MAKS 11
    
    void daftar();
    
    struct data
    {
    char nim[11];
    char name[11];
    char gender[11];
    int grade;
    int phone;
    char email[21];
    };
    struct regis_1
    {
    char password[MAKS+1];
    struct data profile;
    };
    
    struct regis_1 regis[MAKS];
    
    
    
    void daftar()
    {
    	int jum_data=0;
    system("cls");
    printf("Register");
    printf("\nInput NIM[10]\t: ");
    gets(regis[jum_data].profile.nim);
    fflush(stdin);
    printf("Input Pass[5-10]\t: ");
    gets(regis[jum_data].password);
    fflush(stdin);
    printf("Input Name[3-10]\t: ");
    gets(regis[jum_data].profile.name);fflush(stdin);
    printf("Input Grade[1..7]\t: ");
    scanf("%d",&regis[jum_data].profile.grade);fflush(stdin);
    printf("Input Phone No\t: ");
    scanf("%d",&regis[jum_data].profile.phone);fflush(stdin);
    printf("Input Email(xxx@xx.xxx)[MAKS 20]: ");
    gets(regis[jum_data].profile.email);fflush(stdin);
    jum_data++;
    printf("\n\nRegister succes!!!");
    }
    
    
    
    
    
    
    int main()
    {	
    
    daftar();
    
    
    
    
    
    
    getchar();
    return 0;
    }
    someone can help me? is these codes error at the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21