Help with a code using File I/O and Loops
It seems as though I've written the code perfectly, although its not executing properly. I have to read a bunch of data from a file (which i named test.txt) and then execute a bunch of options to the user via a menu. heres the code.
Code:
#include <stdio.h>
int main(void)
{
//initialize variables
char fname[128];
int size = 0;
FILE* fin;
int count = 0;
int option;
int low_bound;
int high_bound;
int x;
double average = 0;
int lowest = -1;
int highest = 0;
int target_donation;
int target_count = 0;
int target_donation2;
int target_count2 = 0;
int target_donation3;
double percentage;
int sum = 0;
int total_under;
int number[size];
//Prompts user for the name of their data file to read.
printf("Please enter the name of the data file\n");
scanf("%s", fname);
//Notice the "r". If it were a "w" it would be writing, not reading to the file
fin = fopen(fname, "r");
//This line scans the file that it is reading, and is reading the numbers within the file
fscanf("%d ",&size);
//This while loop accumulates the number of numbers within the file.
while(!feof(fin))
{
fscanf(fin,"%d ", &number[count]);
count++;
}
//Close the file
fclose(fin);
//Tells the user how many numbers were read within the file
printf("The data for %d numbers of contributions has been read.\n", count);
while(1==1)
{
//Now starts the menu process, asking the user for his/her choice
printf("Please make a choice from the following options:\n");
//Full menu
printf("1. Calculate the average donation for a subset of the donations.\n");
printf("2. Calculate the minimum donation for a subset of the donations.\n");
printf("3. Calculate the maximum donation for a subset of the donations.\n");
printf("4. Calculate the number of donations exceeding a target value.\n");
printf("5. Calculate the number of donations of a particular dollar amount.\n");
printf("6. Calculate what percentage of the donations (Value Wise) are under a target value.\n");
printf("7. Quit\n");
//Scanning the users option for the menu list
scanf("%d", &option);
//If the user chooses to quit, they will type 7, which the computer will read and return the program to quit
if(option == 7)
{
system("PAUSE");
return 0;
}
//I grouped options 1 to 3 because they all use the lower and higher bound variables.
//I was going to individually write each option out, but grouping the 3 was easier.
if((option >= 1)&&(option<=3))
{
printf("What is the low bound for your group(0-%d)?\n", size);
scanf("%d", &low_bound);
printf("What is the high bound for your group(0-%d)?\n", size);
scanf("%d", &high_bound);
if(option==1)
{
for(x=low_bound; x<=high_bound; x++)
{
average+=number[x];
}
average=average/size;
printf("The average donation from person %d to person %d is $%.2lf.\n", low_bound, high_bound, average);
}
if(option==2)
{
for(x=low_bound; x<=high_bound; x++)
{
if(number[x]<lowest)
{
lowest=number[x];
}
}
printf("The smallest donation from person %d to person %d is $&d .\n", low_bound, high_bound, lowest);
}
if(option==3)
{
for(x=low_bound; x<=high_bound; x++)
{
if(number[x]>highest)
{
highest=number[x];
}
}
printf("The largest donation from person %d to person %d is $%d .\n", low_bound, high_bound, highest);
}
}
//Now to compute if the user selects option 4
//This time around, I chose to split up the 3 options that use the common variable of a target donation
if(option==4)
{
printf("What is the target donation?\n");
scanf("%d", &target_donation);
for(x=lowest; x<highest; x++)
{
if(target_donation>=number[x]) //consider revising
{
target_count++;
}
}
printf("%d people donated more than %d .", target_count, target_donation);
}
//Now to compute if the user selects option 5
if(option==5)
{
printf("What is the target donation?\n");
scanf("%d", &target_donation2);
for(x=lowest; x<=highest; x++)
{
if(number[x]==target_donation2) //consider revising
{
target_count2++;
}
}
printf("%d people donated exactly $%d .\n", target_count2, target_donation2);
}
//Now to compute if the user selects option 6
if(option==6)
{
printf("What is the target donation?\n");
scanf("%d", &target_donation3);
for(x = 0; x < count; x++)
{
sum += number[x];
}
for(x=lowest; x<=highest; x++)
{
if(total_under < target_donation3)
{
total_under+= number[x];
}
}
percentage = (sum/total_under) * 100;
printf("Donations under $%d accounted for %.2lf percent of the funds raised",target_donation3, percentage);
}
}
system("PAUSE");
return 0;
}
So test.txt is a file with this written in it:
Quote:
10
50
25
75
2000
30
500
800
10
25
485
where the output should be similar to this:
Quote:
Please enter the name of the data file.
test.txt
The data for 10 number of contributions has been read.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
1
What is the low bound for your group (0-9)?
3
What is the high bound for your group (0-9)?
6
The average donation from person 3 to person 6 is $832.50.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
2
What is the low bound for your group (0-9)?
3
What is the high bound for your group (0-9)?
6
The smallest donation from person 3 to person 6 is $30.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
3
What is the low bound for your group (0-9)?
3
What is the high bound for your group (0-9)?
6
The largest donation from person 3 to person 6 is $2000.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
4
What is the target donation?
75
4 people donated more than $75.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
5
What is the target donation?
25
2 people donated exactly $25.
Please make a choice from the following options:
1) Calculate the average donation for a subset of the donations.
2) Calculate the minimum donation for a subset of the donations.
3) Calculate the maximum donation for a subset of the donations.
4) Calculate the number of donations exceeding a target value.
5) Calculate the number of donations of a particular dollar amount.
6) Calculate what percentage of the donations (value wise) are under a target value.
7) Quit
6
What is the target donation?
500
Donations under $500 accounted for 17.50 percent of the total funds raised.
Can anyone help me as to why the file isn't executing properly? Thanks in advance.