Writing a C program that calculates average quarterly rainfall for a city? -


i'm struggling program need write class. need write program reads city's name user , asks user enter month followed comma , average rainfall month. here's task @ hand:

write program reads data user quarterly rainfall in city , displays on screen. program must prompt user following information:

  • the name of city. may assume @ 50 characters.

  • the months , amount of rainfall received each month. may assume, each month, user enter both month , rainfall received values in 1 line separated comma month having length of 3 characters.

  • the format of output in terms of field width , precision.

your program must calculate average rainfall , produce output complying following specifications:

  • display name of city followed blank line.

  • for each monthly rainfall value, display name of month left justified in user specified field width followed rainfall month displayed right justified formatted using fixed-point notation, user specified precision, in field of 15 spaces wide.

  • finally follow last month's rainfall blank line , on separate line display sentence "average rainfall: " left justified in user specified field width followed average rainfall right justified in fixed-point notation user specified precision , in field of 15 spaces wide.

now here's code far, i'm struggling code right let user enter own comma, code compiles , runs, won't print next month's line, after hitting enter after entering first month's details, leaves blank line:

int main ()  {      char cityname[50];     char monthname[3];     char comma;     float averain = 0;     int monthcount = 1;     float averesult = 0;     int fieldwidth;     int outputprecision;      puts("this program calculates average quarterly rainfall given city.");     printf("enter name of city: ");      scanf("%s" , &cityname[50]);      while( monthcount <= 3 )     {         printf( "enter month , average rainfall of month %d: " , monthcount);         scanf( "%s %s %f" , &monthname[3] , &comma , &averain);          monthcount = monthcount + 1;     } } 

your scanf bad :

  • you declare char monthname[3] (only 2 chars , terminating null) , read @ &monthname[3] just after end of array : undefined behaviour
  • you try read %s in char : %s reads @ least 1 character , writes terminating null, here again writing anywhere ub

you should instead :

  • declare char monthname[4] leave room terminating null
  • write cr = scanf("%3s,%f", monthname, &averain); in order :
    • limit input @ 3 characters , null in monthname
    • process comma
    • be able control correctly scanned 2 fields or abort error message case of problem on input

btw preceding scanf bad : must read in char array , not after :

scanf("%49s" , cityname);  

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -