Thread: what is wrong in the following program?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    what is wrong in the following program?

    I'm trying to split the ip addresses using c and check if each fields are below 255(validity)It shows several errors help me out..

    insert
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct
    {
    int vty;
    char a;
    char b;
    char c;
    char d;
    } ip[10]
    
    main()
    {
            int i,x;
            char st[15];
                    printf("Enter the number of ip address: ") ;
                    scanf("%d",&x);
            for(i=0;i<x;i++)
    {
                    printf("Enter the ip address: ");
                    scanf("%s",&st[i]);
    
                    sscanf(st[i],"%s.%s.%s.%s",&ip[i].a,&ip[i].b,&ip[i].c,&ip[i].d);
                    printf("%s.%s.%s.%s",ip[i].a,ip[i].b,ip[i].c,ip[i].d);
    
    
            if ((a!<256)&&(b!<256)&&(c!<256)&&(d!<256))
    {
                    printf("Enter a valid ip !");
                    i--;
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %s scans strings
    %c scans one character
    %d scans an integer (why aren't you just using this?)
    Your struct has single characters.
    You don't actually have any variable called a (or b or c or d).
    You don't apparently pay attention to your compiler at all.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    why don i have a variable called a?
    I accept al other mistakes.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if ((a!<256)&&(b!<256)&&(c!<256)&&(d!<256))
    You do not have anything called just a. You have a ip[ x ].a but not just a.

    Also those comparisons are wrong. You can't do !< as a comparison.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this program, and how to fix it?
    By pantera in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2009, 11:06 PM
  2. What is wrong with my program
    By bijan311 in forum C++ Programming
    Replies: 18
    Last Post: 12-13-2009, 05:04 PM
  3. What is wrong with the following program?
    By roaan in forum C Programming
    Replies: 14
    Last Post: 08-27-2009, 12:52 PM
  4. What was wrong with my program?
    By cjmdjm in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 03:39 AM
  5. What's wrong with my program?
    By Infuriate in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 04:43 PM