can anybody help me with this code

This is a discussion on can anybody help me with this code within the C Programming forums, part of the General Programming Boards category; can someone what's wrong with this code ? I have written a code which promts the user to enter some ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    can anybody help me with this code

    can someone what's wrong with this code ?
    I have written a code which promts the user to enter some information about an animal then stock them within a structured data. but the function read_animal doesnt seem to work well. it skeps this scanf:
    Code:
    scanf("%c",&an.food)
    here is the full code
    Code:
    #include<stdio.h>
    
    //our animals structure.
    typedef struct{
    	char name[10],cont[10],food,habit;
    	int leg;
    } animal_t;
    
    //decalartion of functions.
    animal_t read_animal(void);
    void print_animal(animal_t);
    
    //main function of the program
    int main()
    {
    	animal_t an;
    	int c,c2;
    	printf("Do you want to enter informations about an animal: 1-Yes / 2-No\n");
    	scanf("%d",&c);
    	while( c == 1)
    	{
    		an = read_animal();
    		printf("Do you want to see the informations about the animal: 1-Yes / 2-No\n");
    		scanf("%d",&c2);
    		if( c2 == 1)
    		{
                print_animal(an);
    		}
            printf("Do you want to enter informations about an animal:1-Yes/2-No\n");
    	    scanf("%d",&c);
    	}
    	
        printf("You have choosen to quit! Bey\n");
    }
    
    //functions codes
    animal_t read_animal()
    {
    	animal_t an;
    	printf("Enter the name of the animal:");
    	scanf("%s",an.name);
    	printf("\n");
    	printf("Enter The continent where does the animal live:");
    	scanf("%s",an.cont);
    	printf("\n");
    	printf("Enter how many legs does he have:");
    	scanf("%d",&an.leg);
    	printf("\n");
    	printf("Enter What it eats c = carnivore, h = herbivore, o = omnivore:");
    	scanf("%c",&an.food);
    	printf("\n");
    	printf("Enter its primary habitat:");
    	scanf("%c",&an.habit);
    	printf("\n");
    	return an;
    }
    
    void print_animal(animal_t an)
    {
    	printf("The informations about the animal are:\n\n");
    	printf("_______________________________________________________\n");
    	printf("name:%s\ncontinent:%s\nlegs:%d\nfood:%c\nhabitat:%c\n",an.name,an.cont,an.leg,an.food,an.habit);
    	printf("_______________________________________________________\n");
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    animal_t read_animal()
    {
    	animal_t an;
            
            an.food = 'X';    // Try adding this, then print the value out in main(), think about the result.
    
    	printf("Enter the name of the animal:");
    	scanf("%s",an.name);
    	printf("\n");
    	printf("Enter The continent where does the animal live:");
    	scanf("%s",an.cont);
    	printf("\n");
    	printf("Enter how many legs does he have:");
    	scanf("%d",&an.leg);
    	printf("\n");
    	printf("Enter What it eats c = carnivore, h = herbivore, o = omnivore:");
    	scanf("%c",&an.food);
    	printf("\n");
    	printf("Enter its primary habitat:");
    	scanf("%c",&an.habit);
    	printf("\n");
    	return an;
    }
    Last edited by msh; 09-08-2010 at 12:41 PM. Reason: Added highlight.

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by shirou View Post
    can someone what's wrong with this code ?
    I have written a code which promts the user to enter some information about an animal then stock them within a structured data. but the function read_animal doesnt seem to work well. it skeps this scanf:
    Code:
    scanf("%c",&an.food)
    here is the full code
    Code:
    #include<stdio.h>
    
    //our animals structure.
    typedef struct{
    	char name[10],cont[10],food,habit;
    	int leg;
    } animal_t;
    
    //decalartion of functions.
    animal_t read_animal(void);
    void print_animal(animal_t);
    
    //main function of the program
    int main()
    {
    	animal_t an;
    	int c,c2;
    	printf("Do you want to enter informations about an animal: 1-Yes / 2-No\n");
    	scanf("%d",&c);
    	while( c == 1)
    	{
    		an = read_animal();
    		printf("Do you want to see the informations about the animal: 1-Yes / 2-No\n");
    		scanf("%d",&c2);
    		if( c2 == 1)
    		{
                print_animal(an);
    		}
            printf("Do you want to enter informations about an animal:1-Yes/2-No\n");
    	    scanf("%d",&c);
    	}
    	
        printf("You have choosen to quit! Bey\n");
    }
    
    //functions codes
    animal_t read_animal()
    {
    	animal_t an;
    	printf("Enter the name of the animal:");
    	scanf("%s",an.name);
    	printf("\n");
    	printf("Enter The continent where does the animal live:");
    	scanf("%s",an.cont);
    	printf("\n");
    	printf("Enter how many legs does he have:");
    	scanf("%d",&an.leg);
    	printf("\n");
    	printf("Enter What it eats c = carnivore, h = herbivore, o = omnivore:");
    	scanf("%c",&an.food);
    	printf("\n");
    	printf("Enter its primary habitat:");
    	scanf("%c",&an.habit);
    	printf("\n");
    	return an;
    }
    
    void print_animal(animal_t an)
    {
    	printf("The informations about the animal are:\n\n");
    	printf("_______________________________________________________\n");
    	printf("name:%s\ncontinent:%s\nlegs:%d\nfood:%c\nhabitat:%c\n",an.name,an.cont,an.leg,an.food,an.habit);
    	printf("_______________________________________________________\n");
    }
    You have to be careful with scanf(); I added 2 getchars() I think that's legal on this BB) to clear the stdin buffer. Program seems to work after that.

    Code:
    printf("\n");
    	getchar();
    	printf("Enter What it eats c = carnivore, h = herbivore, o = omnivore:");
    	scanf("%c",&an.food);
    	printf("\n");
    	getchar();
    	printf("Enter its primary habitat:");
    	scanf("%c",&an.habit);
    	printf("\n");
    	return an;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    SourceForge.net: Scanf woes - cpwiki <--- READ THIS and avoid scanf, especially for strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by Elysia View Post
    SourceForge.net: Scanf woes - cpwiki <--- READ THIS and avoid scanf, especially for strings.
    WOW! I've checked out that link. Between you, and many other people here that are
    kind enough to post a link, I've been saving them all in one location.

    What's hard for me, is when I get corrected I'm told to avoid using this or that function, without an explanation. What I love to see, is a one sentence comment with a link.. short and to the point. Then I'll bookmark that.

    I've seen in red "How to ask questions the smart way" that seemed to be just a signature, but it wasn't until Bayint (sp.?) posted that in one of his replies, and I clicked on it, I found out that it was actually a link.

    "How to ask questions the smart way" is probably the most important link that I have ever bookmarked here. It gives me all the answers to, such as, why some people get irritated with the questions that I used to ask, etc. etc.

    I'd like to add that link to my signature file, but I'll learn how to do later.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    The problem with scanf is many-fold, as the article really is supposed to point out (as well as alternatives). If you don't think so, then may you elaborate on its weaknesses from your standpoint that it may be improved?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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