Reputation: 1
I am a beginner but I have been working on this code for a week now and I cannot get my scanf
functions to work. All of my printf
functions output just fine but if I go to input my information to test my code I do not get anything. Am I missing something simple here?
#include <stdio.h>
float average(float a[]);
int main ()
{
/* variable definition: */
char personsName[100];
char personsAge[3];
char personsState[20];
float Age, Texas;
int people,age,i,count,Avg,None;
// Loop through 50 people
for (people=0; people <2 ; people++)
{
Age =0.0;
printf("Enter Persons Name \n");
scanf("%s", personsName);
printf("Enter None if this is last family member. \n");
scanf("%s", personsName);
{
if (personsName == None)
break;
}
}
{
printf("Enter Persons Age \n");
scanf("%s", personsAge);
float average(float a[])
{
printf("Average of all family members is %f \n");
int i;
float avg, sum=0.0;
for(i=0;i<50;++i)
sum+=a[i];
avg =(sum/i);
return avg;
}
}
{
printf("Enter Persons State \n");
scanf("%s", personsState);
while (Texas > 0)
{
printf("Enter Texas\n");
scanf("%f",&Texas);
//Only assign if positive
if (Texas > 0)
{
personsState[count]=Texas;
count = count + 1;
break;
}
}
}
// Print data
for (i=0; i<count;i++)
{
printf ("Texas %f is %d\n", i,personsState[i]);
if (count == 50)
break;
}
return 0;
}
Upvotes: 0
Views: 300
Reputation: 134346
The issue, as I see it is with
if (personsName == None)
This is not the way to compare two string. You need to use strcmp()
to compare the contents.
Also, the two successive scanf()
s has same argument to store the input. The second scanf()
will overwrite the previously taken input.
FWIW, None
is not a string, however. You need to use "None"
to denote a string.
That said, uncontrolled input through scanf()
can lead to buffer overflows. Better to limit your scanning with the length specifier, like
scanf("%99s", personsName);
and so on.
Upvotes: 3