I am trying to save weather data in a structure. In the below, when I use scanf, it works fine for the 1st loop, but from the second loop, scanf is skipped and just the printf statement gets executed. How can I get scanf to get input throughout the loops. Here is my code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
struct weather
{
char *date;
int month;
int day;
int year;
unsigned int h_temp;
unsigned int l_temp;
int max_wind_speed;
int preciption;
char notes [80];
};
void collect_data (struct weather *pinfo)
{
int loop;
char yes_no[2];
time_t curtime; //declaring time variable
//storing current system time in the time variable
time(&curtime);
//storing current time to time structure
struct tm * wdate = localtime (&curtime);
for (loop=0;loop<4;loop++)
{
if (loop!=0)
{
(pinfo+loop)->date = ctime(&curtime);
(pinfo+loop)->day = wdate->tm_mday;
(pinfo+loop)->month = wdate->tm_mon;
(pinfo+loop)->year = wdate->tm_year;
}
/*else
{
recent_date(loop,wdate);
}*/
printf("\nEnter the high temperature of the day:");
scanf("\n%d",&(pinfo+loop)->h_temp);
printf("\nEnter the low temperature of the day:");
scanf("\n%d",&(pinfo+loop)->l_temp);
printf("\nEnter the maximum wind speed of the day:");
scanf("\n%d",&(pinfo+loop)->max_wind_speed);
printf("\nEnter the perciption of the day:");
scanf("\n%d",&(pinfo+loop)->preciption);
printf("\nDo you have any notes about the weather of the day (y/n):");
scanf("\n%s",yes_no);
if (strcmp(yes_no,"y")==0)
{
printf("\nNotes:\n");
scanf("\n%[\n]s",(pinfo+loop)->notes);
}
}
}
int main ()
{
struct weather info [4];
collect_data(info);
return 0;
}