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

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.

> 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)```
3. 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
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
159
Rep Power
22
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
5. 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.
6. 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.