#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Bit time problem in scanning a set of 1 character and a float


    Dear Experts,

    I am having trouble scanning the sex-code and the height of the students in my program:

    How can I make it easier to scan the input while maintaining the restriction of the program to enter the sex code and height pair at a time.

    Code:
    /* Program 13: Find the average male and female heights in the class (input in the form of sex code and height)
    
    /* sample output :
     m 170 m 161 m 179 f 160 f 161 f 168
     The average of male heights is 170.000000
     The average of female heights is 163.000000
    */
    
    /* Pre-processing Section */
    # include <stdio.h>   
    # include <conio.h>  
    
    /* main function */
    void main()   
    {   
    	/* Data Declaration Section */
    	char sex;
    	int i, n, countM, countF;
    	float sumM, sumF, AvgM, AvgF;
    	float * height;
    	
    //	clrscr();   
    	
    	/* Data Initialization Section */
    	countM=countF=0;
    	sumM=sumF=AvgM=AvgF=0.0;
    
    	/* Data Entry and processing Section */
    	printf("Enter the number of values you want to enter:  ");
    	scanf("%d", &n);
    
    	printf("Enter all %d values in the form of sex code and height:  ", n);
    	for (i = 0; i <n; i++)
    	{
    		sex=getch();
    		scanf("%f",&height[i]);
    		if(sex == 'f' || sex == 'F')
    		{
    			countF++;
    			sumF+=height[i];
    		}
    		else if(sex == 'm' || sex == 'M')
    		{
    			countM++;
    			sumM+=height[i];
    		}
    		else
    		{
    			printf("Re-enter the values with sex code as 'm' or 'f'");
    			i--;
    		}
    	}
    
    	AvgM=sumM/countM;
    	AvgF=sumF/countF;
    
    	printf("\nThe average of male heights is %f\nThe average of female heights is %f", AvgM, AvgF);
    
    	getch();
    }
    Thanks,
    Saurabh.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    (conio.h and void main as noted in your other thread)

    > float * height;
    Where is this pointing?
    You're writing over some random memory addresses, causing who know what damage.
    Besides, you're not using an array, so you may as well have
    float height;

    > sex=getch();
    > scanf("%f",&height[i]);
    Again, don't mix and match input methods (especially non-portable ones).
    Try say
    Code:
    scanf(" %c", &sex);  //!! note the leading space - important if you're after just actual characters
    scanf("%f",&height); //!! not an array (see above)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    I am using a pointer (and not a fixed array or a variable) as I don't know before run time, how many heights (of male and female students) will be entered... The input is to be like this:

    Code:
    how many heights are to be entered? 5
    OK, enter the 5 values:
    m 169 m 167 f 171 f 169 f 170 (but there could be more depending on user choice)
    the average male height is 168
    the average female height is 169
    thanks,
    saurabh
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Originally Posted by saurabhmangal
    I am using a pointer (and not a fixed array or a variable) as I don't know before run time, how many heights (of male and female students) will be entered...
    So where are you allocating any memory for this pointer? Plus you don't actually need an array to compute an average.

    Jim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Input restriction


    Originally Posted by jimblumberg
    So where are you allocating any memory for this pointer? Plus you don't actually need an array to compute an average.

    Jim
    I get it now... I was doing the pointer/array thing the wrong way. Thanks for the reply... What I need to know now is how to make a program accept alternating chars and ints as mentioned in my above post at 3 pm today.

    Thanks.
    Saurabh.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Originally Posted by saurabhmangal
    What I need to know now is how to make a program accept alternating chars and ints as mentioned in my above post at 3 pm today.
    salem already gave you that in the first reply, Message #2. And do pay close attention to that space in the character scanf; it is extremely important.

IMN logo majestic logo threadwatch logo seochat tools logo