Thread: Hopelessly Lost (calling file, write to value, calculate)

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Hopelessly Lost (calling file, write to value, calculate)

    this is sorta my last ditch effort here.. I have been bashing my head on the wall several days going through this.. (yes I'm rather new to C++ coding and this is what I have to do)

    - have 4 files w/ information on cars (each file has 399 lines worth, x and y values)
    ex. 0.967369 -19643.556725
    - prompt user to pick vehicle and call the corresponding file, writing info to kw400 variable
    - make sure no error opening file
    - Divide Kw400 by 80000 and store the result back in Kw400
    - Some of the guidance of formulas are provided..

    WOW.. this doesn't feel like an intro course any longer. Just a little direction of guidance would be nice. This is supposed to be extremely basic. making user choose which model, pretty much calling a file, verify, store values in a string (don't think an array). and run a computation on results, display.


    Code:
        int carNo;  // The number of the car selected
    
        cout << "Kw400 Crush-Work Stiffness Metric\n\n"
             << "1.  2007 Honda Civic\n"
             << "2.  2005 Chevy Cobalt\n"
             << "3.  2007 Toyota Camry\n"
             << "4.  2006 Ford Fusion\n"
             << "Choose a vehicle (1, 2, 3, 4): ";
        cin >> carNo;
    
        // Call the calcKw400 with one of the following filenames:
        //    honda_civic_2007.txt
        //    chevy_cobalt_2005.txt
        //    toyota_camry_2007.txt
        //    ford_fusion_2006.txt
        
    // Store the result in the kw400 variable
        // Display the output with two numbers after the decimal point
    
        system("pause");
    
    }
    
    /*
     *  Function:   calcKw400
     *  Purpose:    This is function calculates the Kw400 Crush-Work
     *              Stiffness based on the values stored in the file 
     *              filename[] 
     *  Parameters: filename:  a string with the name of the file to open
     *  Returns:    the calculated Kw400 value
     */
    double calcKw400(char filename[])
    {
        double Kw400 = 0;
        double X = 0, Y = 0;
        double lastX = 0;
        ifstream inFile;
    
        // Open the file
        // Check to make sure that there was no error opening the file
        // While there is data to read in the file...
        // Divide Kw400 by 80000 and store the result back in Kw400
        // Close the file
    
        return Kw400;

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, you can do the selection of the file name using a switch on the carNo entered by the user and simply call the function accordingly. Or, a series of if/else-if statements. Or you can assign the various filenames to a char* variable based on what carNo is and then make a single call to the function passing this pointer variable in as an argument.

    The file can be opened directly when the ifstream object is created by passing the filename as an argument to its constructor, or later in the function's code via the open member function. Testing for errors in opening the file can be done with the is_open member function. Reading the data is a simple member of using operator>> (like how you used cin to get carNo but with the ifstream object instead) in a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    that's very close to the basic structure I anticipated on using.. (putting it into words on this forum actually sorta got the plan more laid out in my mind)

    The difficulty I'm coming into is how to pass my txt file values to the particular CalcKW400 retaining all 399 lines of information..

    it is in a 0.967369 -19643.556725 line by line format.

    then it must be called upon later to run a calculation of data.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Store the information from the file in a vector, if it's important for the two values on a single line to be "together" then your vector should contain a struct that has those two values as members:
    Code:
    #include <vector>
    ...
    struct cardata
    {
        double x, y;
        cardata(double x_temp = 0.0, double y_temp = 0.0) : x(x_temp), y(y_temp) {}
    };
    ...
    double calcKw400(char filename[])
    {
        vector<cardata> foo;
        double tempx, tempy;
    
        // Open file, test for errors.
        ...
    
        // Load all data points from the file into the vector.
        while( inFile >> tempx >> tempy )
            foo.push_back(cardata(tempx,tempy));
    
        // Do something with the data in the vector, i.e. make your calculation here.
        ...
    Alternately, you might just allocate a two-dimensional array of an appropriate size (2*399 elements) and stuff that with data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by hk_mp5kpdw View Post
    Store the information from the file in a vector, if it's important for the two values on a single line to be "together" then your vector should contain a struct that has those two values as members:
    Code:
    #include <vector>
    ...
    struct cardata
    {
        double x, y;
        cardata(double x_temp = 0.0, double y_temp = 0.0) : x(x_temp), y(y_temp) {}
    };
    ...
    double calcKw400(char filename[])
    {
        vector<cardata> foo;
        double tempx, tempy;
    
        // Open file, test for errors.
        ...
    
        // Load all data points from the file into the vector.
        while( inFile >> tempx >> tempy )
            foo.push_back(cardata(tempx,tempy));
    
        // Do something with the data in the vector, i.e. make your calculation here.
        ...
    Alternately, you might just allocate a two-dimensional array of an appropriate size (2*399 elements) and stuff that with data.
    I just took my exam.. so I ended up learning alot more about how it probably should be done..

    the file should most likely be stored as a 2 dimensional array.
    the file comparison to validate is simply a loop stating !file = etc..

    everything seems to make much more sense after about 3 days of studying and reviewing all material. The basic structure of the program is still a little .. eh.. difficult to organize, at least for a beginner.

    I'm trying to develop the logical flow before I devote all my time to finishing. Currently we often are voiding functions to call later. I understand the concept but it can become very confusing when there are several.

    My Plan at least for flow pattern.

    -Cout the user to enter the file info
    -Cin their choice
    -Run a switch statement to call their info
    -Open the file while looping to verify correct file
    -Store file in 2 dimensional array.
    -Compare the values of the 2 dimensional array to determine value of Kw400
    -Cout the results to user

    (I'm also assuming the kw400 value should be static since per instructions state - "Divide Kw400 by 80000 and store the result back in Kw400"




    thanks for the all help hk_mp5kpdw .. between full time work, MCSA classes, and a Comp Sci class.. I need all the help I can get.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better use std::string rather than char*. This is C++, after all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Lost in C
    By David670 in forum C Programming
    Replies: 8
    Last Post: 10-31-2005, 11:19 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM