Thread: struct

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    struct

    I have the following program. Everything seems ok, the only problem is that the wight always prints 0.
    How can i fix that
    Code:
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h> 
    #define TRUE 1
    #define FALSE 0  
    
    void printCat(CAT);
    
    //void compare (CAT, cat27, max);
    
    typedef struct {	
    	double weight;	
    	char name[50];	
    	int neutered;
    }CAT;  
    	int main(){	
    		int size=0; 
    		int index=0;
    		int i;
    		CAT* heardOfCats;
    		char*name[6]={"Fluffy", "Tigger", "Max", "Betty", "Cat27","Jake"};	
    		CAT max;
    		srand(time(NULL));
    		
    		
    		
    		printf("Please enter the number of cats: ");	
    		scanf("%i", &size);
    		
    		
    		 	
    		heardOfCats=calloc(size, sizeof(CAT));
    		
    			
    		strcpy(max.name,"Max");	
    		max.weight=12;	
    		max.neutered=TRUE; 	
    	
    		printCat(max);	
    		
    		
    
    		for(i=0; i<size; i++){
    		index = rand() % 6;
    		strcpy(heardOfCats[i].name, name[index]);
    		heardOfCats[i].neutered=rand()%2;
    		printf("%s    ",heardOfCats[i].name);
    		printf(" %i   ", heardOfCats[i].weight);
    		printf("%i     ",heardOfCats[i].neutered);
    		printf("\n");
    		}
    		
    		system("Pause");}   
    
    	void printCat(CAT max){	
    		printf("%s\n", max.name);	
    		printf("%.1lf\n", max.weight);	
    		printf("%i\n", max.neutered);}

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Certainly the fact main() isn't actually returning anything doesn't exactly qualify as ok, but I am not here to pick on your code.

    Its good practice to pass structs by reference rather than by value.

    Example
    Code:
    void printCat(const CAT *max)
    {
      const char *boolStr[2] = {"false", "true",};  /* Corrected */
      printf("&#37;s\n%.1lf\n%s\n", max->name, max->weight, boolStr[max->neutered]);
    }
    Secondly, I don't see anywhere that you are actually setting anything other than name of the cats.
    Last edited by master5001; 04-21-2008 at 12:05 PM. Reason: The code corrected is properly noted.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Firstly when compiling ALWAYS USE -Wall

    Your string library is missing. Your main function should be returning something, 0.

    Code:
    printf(" &#37;f   ", heardOfCats[i].weight);
    And your formatting needs to be improved
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    Its good practice to pass structs by reference rather than by value.
    By pointer, please. By pointer. By reference is misleading.

    Quote Originally Posted by JFonseka View Post
    Firstly when compiling ALWAYS USE -Wall
    Who says goran00 is using GCC? It's bad to assume.

    Also, as someone mentions, you need to work on your indenting,
    http://cpwiki.sourceforge.net/Indentation
    http://cpwiki.sourceforge.net/User:Elysia/Indentation

    I spot two other subtle errors in your program too.
    Code:
    char*name[6]={"Fluffy", "Tigger", "Max", "Betty", "Cat27","Jake"};
    Firstly, this should be const char, not char. A string literal is const. Also note how you have 6 names.

    Code:
    index = rand() % 6;
    strcpy(heardOfCats[i].name, name[index]);
    Note that this could possibly result in 6 which is beyond the end of your name array.
    Last edited by Elysia; 04-21-2008 at 01:44 AM.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    index = rand() &#37; 6;
    will NEVER be 6 - it will be a number in the range 0..5, so it's correct in relation to the array posted.

    Compare:
    Code:
      const char *boolStr[2] = {"true", "false"};
    with:
    Code:
    #define TRUE 1
    #define FALSE 0
    If TRUE is 1, then I suggest that boolStr[] should have "true" on index 1, not index 0.

    And finally:
    Code:
    printf("%.1lf\n", max.weight);
    in C99 will print a "long double" number - if you are NOT using a C99 compatible(-ish) compiler, it may do "anything". Note that this printf and scanf are different in the treatment of the "%lf" format specifier.


    --
    Mats
    Last edited by matsp; 04-21-2008 at 02:17 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The boolStr[] was my bad. It was late...

    It would never hurt your code to actually do some error checking too. I suppose it depends on what the program is for, but scanf() has some perils to be aware of that can easily be avoided if one just makes sure scanf() read what it was supposed to read.

    Though I am sure while debugging your code you are not entering "a" or something when it prompts for the size, if someone else is using it, that could be a reasonable issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM