Help with a code using File I/O and Loops

This is a discussion on Help with a code using File I/O and Loops within the C Programming forums, part of the General Programming Boards category; It seems as though I've written the code perfectly, although its not executing properly. I have to read a bunch ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    14

    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:
    10
    50
    25
    75
    2000
    30
    500
    800
    10
    25
    485

    where the output should be similar to this:
    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.
    Last edited by bns1201; 10-17-2008 at 07:20 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you know it isn't executing properly because why?

    Anyway, this
    Code:
    while (!feof(fin))
    is bad in that it will read the last data entry twice. It's an FAQ; you should look it up.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    If it reads the last data entry twice, how would I fix that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bns1201 View Post
    If it reads the last data entry twice, how would I fix that?
    Quote Originally Posted by tabstop View Post
    It's an FAQ; you should look it up.
    You've got to learn to read all the way to the end of the line.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Im using netbeans6.1 on a mac, and it when i run and compile it, it begins fine, but when I put in the file name, it gives me this

    /Applications/NetBeans/NetBeans 6.1.app/Contents/Resources/NetBeans/cnd2/bin/dorun.sh: line 103: 6823 Segmentation fault "$pgm" "$@"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. If you're on a mac, good luck with system("PAUSE").
    2. Your compiler complained very loudly about the line
    Code:
        int number[size];
    which makes a zero-length array. Ignoring the complaints does not actually fix the problem.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    with the initialization of size as 0, is that what the complaining is about? And yea i know about the system("PAUSE") ordeal lol, I have to have it run in a windows native environment, but I have a mac :/

    But if I don't initialize size as 0 would it get me closer to running correctly?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. You can't create the array until you know what size is supposed to be.[1] Your code seems to assume that the first number is the size, but your sample text file doesn't contain it.

    Unless your instructor gave you the directive to include system("PAUSE") in your program, I wouldn't include it -- it's not necessary (and usually not desired) on that OS either.

    [1]If you happen to know an upper limit on the size, you can use that instead to create the array (the excess memory will be wasted, but it probably won't be that much).

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Alright, I initialized the integer number[] after i scanned the document, is that what you meant by creating it after you know what it is supposed to be?

    Code:
     //Prompts user for the name of their data file to read.
        printf("Please enter the name of the data file\n");
        scanf("&#37;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);
        
        int number[size];

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes -- on the assumption that the first number in the file actually is the number of data values in the rest of the file. (Again, the sample .txt file you gave did not start with 10.)

    I feel obligated to mention that this is "new C" (C99, as opposed to C89), and different compilers have different amounts of adherence to the new standard.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Ok, its starting to make sense. The original test.txt file that my instructor had given me included a 10 at the begining of the file, i have made the edit to show how it originally appeared b4 I had deleted the 10 because it had made more sense to me.

    Now after that, is there any problem with the loops I have for questions 4, 5, and 6, because im pretty confident that questions 1, 2, 3, and 7 are correct.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    -1 is not a valid array index. Your loops shouldn't go from -1 to 0, but from 0 to size.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    so would i have to change:
    int lowest = -1;
    int highest = 0;

    to:

    int lowest = 0;
    int highest = size;

    ?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should change
    Code:
    int lowest = -1;
    int highest = 0;
    into
    Code:
     
    The variables serve no purpose in your program, so why have them?

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Code:
    if(option==2)
                    {
                         for(x=low_bound; x<=high_bound; x++)
                         {
                            if(number[x]<lowest)
                            {
                                 lowest=number[x];
                            }
                         }
                            printf("The smallest donation from person &#37;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);
                    }
    Could you steer me in a direction so that I can make these 2 if loops work if I do in fact get rid of those 2 variables. Sorry if i'm asking a lot, Im a beginner.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file i/o dilemma
    By adramalech in forum C Programming
    Replies: 27
    Last Post: 11-11-2008, 12:30 AM
  2. help on assignment (file i/o)
    By Longhorn9024 in forum C Programming
    Replies: 4
    Last Post: 03-07-2008, 02:08 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 10:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21