Actually, it does print to the file, only with very low probability (or 0 probability, in the case where the pseudo random generator was not seeded)...
The reason is obvious when the code is properly indented:
Code:
void simulator(void) {
FILE *project;
project=fopen("winds.txt","w");
if(project==NULL){
printf("Error!");
return 0;
}
double average,mingust,maxgust,windspeed,totalWindspeed,minwind,maxwind,range,div;
maxgust=10;
int i;
int Maxseconds=3600;
printf("What's the average wind speed?\n");
scanf("%lf",&average);
printf("What's the expected gust?\n");
scanf("%lf",&mingust);
//check parameters
if (average<0)
printf("not possible\n");
else if (average>30)
printf("not possible\n");
else if (mingust<0)
printf("not possible\n");
else if (mingust>10)
printf("not possible\n");
else if (mingust>average)
printf("not possible\n");
else {
//check if storm is occurring or not
int stormoccurence;
//Storm occurrence can take a random value between 0 and 999
stormoccurence = rand()%1000;
//Ensure value is <5
if (stormoccurence>5) {
printf("No storm occured\n");
}
else {
printf("Storm started\n");
srand(time(NULL));
Maxseconds=3*60+(rand()%121);
for(i=0; i<=Maxseconds; i+=10) {
minwind=average-mingust;
maxwind=average+mingust;
range=maxwind-minwind;
div=RAND_MAX/range;
windspeed=minwind+(rand()/div);
windspeed=windspeed+20+rand()%11;
totalWindspeed+=windspeed;
printf("simulation was successful\n"); /***/
fprintf(project,"%lf %f",average, (float)totalWindspeed/Maxseconds); /***/
fclose(project); /***/
}
}
}
return 0;
}
Control will only reach the lines marked in /***/ when rand() will generate a number that is less than or equal to 5, which allegedly should happen with probability 6/1000, though in your case with probability 0, since you called srand() only after calling rand().
I assume you want to print to the file regardless of whether or not the weather is bad (pun intended ), and so your intention was to take those three lines out of that 'else' clause:
Code:
/* This program simulates wind speeds for a flight simulator and it also analyzes the data produced*/#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void simulator(void);
void analysis(void);
int main()
{
int choice;
do
{
printf("Menu\n");
printf("1. Simulator\n");
printf("2. Analysis\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1: simulator();
break;
case 2: //analysis();
break;
case 3: printf("Quitting program!\n");
break;
default: printf("Invalid choice!\n");
break;
}
} while (choice !=3);
}
void simulator(void) {
FILE *project;
project=fopen("winds.txt","w");
if(project==NULL){
printf("Error!");
return 0;
}
double average,mingust,maxgust,windspeed,totalWindspeed,minwind,maxwind,range,div;
maxgust=10;
int i;
int Maxseconds=3600;
printf("What's the average wind speed?\n");
scanf("%lf",&average);
printf("What's the expected gust?\n");
scanf("%lf",&mingust);
//check parameters
if (average<0)
printf("not possible\n");
else if (average>30)
printf("not possible\n");
else if (mingust<0)
printf("not possible\n");
else if (mingust>10)
printf("not possible\n");
else if (mingust>average)
printf("not possible\n");
else {
//check if storm is occurring or not
int stormoccurence;
//Storm occurrence can take a random value between 0 and 999
stormoccurence = rand()%1000;
//Ensure value is <5
if (stormoccurence>5) {
printf("No storm occured\n");
}
else {
printf("Storm started\n");
srand(time(NULL));
Maxseconds=3*60+(rand()%121);
for(i=0; i<=Maxseconds; i+=10) {
minwind=average-mingust;
maxwind=average+mingust;
range=maxwind-minwind;
div=RAND_MAX/range;
windspeed=minwind+(rand()/div);
windspeed=windspeed+20+rand()%11;
totalWindspeed+=windspeed;
}
}
printf("simulation was successful\n");
fprintf(project,"%lf %f",average, (float)totalWindspeed/Maxseconds);
fclose(project);
}
return 0;
}
This does print to file.
Furthermore, you should compile with -Wall flag to enable warnings, your code has some warnings you would not want to ignore.