I'm writing a program to read in user intered values for a country (read into a 2D character array) and 2 different types of values (into a 2D float array) and calculate a third value. I wrote out my code and it compiles fine and whatnot, but when I run it, it crashes after the first entered float value.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define NCHARS 15
#define NCOLS 3
#define MAXCOUNTRY 10
int collectdata(char country[][NCHARS], float stats[][NCOLS]);
void printdata(char country[][NCHARS], float stats[][NCOLS], int count);
void printheader();
int main()
{
int count;
float stats[MAXCOUNTRY][NCOLS];
char country[MAXCOUNTRY][NCHARS];
count = collectdata(country, stats);
printdata(country, stats, count);
return 0;
}
int collectdata(char country[MAXCOUNTRY][NCHARS], float stats[MAXCOUNTRY][NCOLS])
{
char ans;
int count=0;
ans = 'Y';
while (ans != 'N' && toupper(ans) != 'N')
{
printf("Enter the name of the country:");
gets(country[count]);
printf("Enter the number of units:");
scanf("%f", stats[count][0]);
printf("Enter the MWe:");
scanf("%f", stats[count][1]);
stats[count][2] = (stats[count][1])/(stats[count][0]);
printf("\nDo you want to enter another country? (Y or N)");
scanf(" %c", ans);
count++;
}
return count;
}
void printdata(char country[MAXCOUNTRY][NCHARS], float stats[MAXCOUNTRY][NCOLS], int count)
{
int i;
printheader();
for (i=0; i<count; i++)
{
printf("%s %4f %4f %4f", country[i], stats[i][0], stats[i][1], stats[i][2]);
}
}
void printheader()
{
printf("World Nuclear Power Generation \n");
printf(" As of January 2007 \n");
printf("Country Units Total MWe \n");
}
so I'm assuming its something wrong with the collectdata function, but I can't figure it out - anyone wanna give me any tips/point it out for me?