Thread: Having trouble reading input from a file in a way that satisfies my assignment

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    3

    Having trouble reading input from a file in a way that satisfies my assignment

    I'll post the instructions first then my issue. I know I'm close since my code can be rearranged and pops out the correct answers, but then it doesn't satisfy the assignment.

    "Main function opens a file and calls get_input to read each entry and save to global variables. Then main function calls calculate_area and calculate_area decides whether it is triangle, square, or trapezoid and calls the corresponding "shape"_area function accordingly. "shape"_area returns to calculate_area and calculate_area returns to main."

    I am using a counter controlled input file and my problem is that I cannot make main call get_input, go back to main to call calculations and then go back to get_input and retrieve the next line of data. If I arrange things not according to the instructions I pop up a row of correct answers no problem. I don't think a matrix is the correct answer as one of our input files is several thousand lines long.

    I guess I'm looking for a way to make get_input read the next line of data if it is called again and not start over at the beginning. 2.5 hours of trying to solve this is frustrating me.

    sample of input file

    6
    1
    23.384350 37.419813
    1
    11.857509 35.222480
    1
    27.589801 42.399823
    2
    38.950621
    1
    15.829493 44.876095
    3
    17.818537 13.336894 40.175634

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    below is the code that gives me correct answers. I don't know how to move the calculate_area function out of get_input and into main.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "input.txt"
    
    
    void get_input(FILE *fp);
    float calculate_area();
    float triangle_area();
    float square_area();
    float trapezoid_area();
    void output_area(FILE *fp);
    
    
    int num_data_pts=0, current_shape,j;
    float a, b, c;
    FILE *fp;
    
    
    
    
    
    
    int main (void)
    {
    float area;
    fp=fopen(FILENAME, "r");
    get_input(fp);
    
    
    system("pause");
    return 0;
    }
    
    
    void get_input(FILE *fp)
    {
       float area;
        
        if(fp==NULL)
            printf("Error opening input file. \n");
        else
        {
               fscanf(fp, "%d", &num_data_pts);
           for(j=1;j<=num_data_pts; j++)
            {    fscanf(fp, "%d",&current_shape);
                switch(current_shape)
                {
                    case 3:
                       fscanf(fp, "%f", &c);
                    case 1:
                       fscanf(fp, "%f", &b);
                    case 2:
                       fscanf(fp, "%f", &a);
                       break; 
                }       
                area=calculate_area();
                printf("%d,%f \n", current_shape, area );
            }    
        }  
      
    return;
    }        
    
    
      
    float triangle_area()
    {
    float area;
    area=0.5*a*b;
    return area;
    }
    
    
    float square_area()
    {
    float area;
    area=a*a;
    return area;
    }
    
    
    float trapezoid_area()
    {
    float area;
    area=0.5*c*(a+b);
    return area;
    }
    
    
    float calculate_area()
    {
    int z;
    switch(current_shape)
    {
    case 1:
        z=triangle_area();
        break;
    case 2:
        z=square_area(); 
        break;
    case 3:
        z=trapezoid_area();
        break;
    }    
    return z;
    }  
    
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, it's despicable that your teacher has forced you to use global variables. But apparently such is the case. C'est la vie.

    Secondly, if you mean for your cases to fall through, you should label them so:
    Code:
                switch(current_shape)
                {
                    case 3:  // triangle
                       fscanf(fp, "%f", &c);
                       // fall through
                    case 1:  // trapezoid
                       fscanf(fp, "%f", &b);
                       // fall through
                    case 2:  // square
                       fscanf(fp, "%f", &a);
                       break; 
                }
    And to fix your problem, what about something like this:
    Code:
    int main (void)
    {
        // de-globalizing a few variables...
        FILE *fp;
        int j;
        float area;
        int num_data_pts;
    
        fp = fopen(FILENAME, "r");
        if (fp == NULL)
        {
            printf("Error opening input file. \n");
            exit(1);
        }
    
        fscanf(fp, "%d", &num_data_pts);
        for(j = 0; j < num_data_pts; j++)
        {
            get_input(fp);
            area=calculate_area();
            printf("%d,%f \n", current_shape, area);
        }
    
        fclose(fp);  // It's a good habit to explicitly close the file.
    
        return 0;
    }
    Obviously get_input needs to be correspondingly modified.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The description doesn't actually ask you to call get_input multiple times.
    You can read the values from file into an array, and satisfy everything it asks you to do.
    I highly suspect that is what they intend.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    I apologize for not commenting my cases, I'm 5 weeks into this class and still new. Yes it was on purpose.

    I appreciate the help. I tried using a for loop in the main function previously and it spit the first numbers in the file back at me over and over. You have solved my problem. I might get some sleep tonight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble reading from txt file
    By ColtsBR in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2010, 11:23 PM
  2. Trouble reading from file?
    By patricio2626 in forum C++ Programming
    Replies: 9
    Last Post: 11-02-2006, 11:39 AM
  3. Trouble with file input
    By w274v in forum C Programming
    Replies: 6
    Last Post: 12-18-2005, 04:40 AM
  4. trouble reading input
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 06:18 PM
  5. Having trouble with file input...
    By Xaviar in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 12:55 PM