!?!?help

This is a discussion on !?!?help within the C++ Programming forums, part of the General Programming Boards category; Can anyone get this code to work?! im missing something or screwing up somewhere, but i cant get rid of ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    !?!?help

    Can anyone get this code to work?! im missing something or screwing up somewhere, but i cant get rid of a few errors. It can open the file but i don't know if the 2nd and 3rd functions are right.

    Code:
    #include <iostream>
    
    #include <iomanip>
    
    #include <fstream>
    
    struct Package{
      float weight;
      float length;
      float width;
    };
    
    using namespace std;
    
    bool openFile(ifstream &ins);
    
    void inPack(ifstream &ins, Package &packList);
    
    void getPkgs(ifstream &ins, Package *&packList, int &numPkg);
    
    int main()
    {
      ifstream ins;
      ofstream outs;
    
      bool ok = openFile(ins);
      if(ok){
        Package *packList;
        int numPkg;
        getPkgs(ins, packList, numPkg);
      }
    
    }
    
    bool openFile(ifstream &ins)
    {
      string str;
      bool flag;
    
      cout << endl;
      cout << "Enter the name of the package data file: ";
      cin >> str;
    
      ins.open(str.c_str());
      if (ins.fail())
        {
          cout << "Error opening file: " << str << endl;
          exit (0);
        }
    
      ins.close();
    
      return (!ins.fail());
    }
    
    void inPack(ifstream &ins, Package &packList)
    {
      ins >> packList.weight >> packList.length >> packList.width >> packList.depth;
    
      return;
    }
    
    void getPkgs(ifstream &ins, Package *&packList, int &numPkg)
    {
      ins >> numPkg;
    
      packList = new Package[numPkg];
    
      for(int i = 0; i < numPkg; i++)
        {
           inPack(ins, &);
        }
    
      return;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    you should post the errors, use code tags when posting code, and narrow it down for us if you need some help. But before you do anything else, read the stickies on the top of this board.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    I've modified your post to make your code readable. Please use code tags in the future.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> inPack(ins, &);

    maybe:

    Code:
    inPack(ins, packList[i]);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Hello Pavement! I hope this helps you follow what's going on: (and helps me hijack this thread too hehehe)
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::cin;
    using std::ifstream;
    using std::ofstream;
    using std::string;
    
    
    // A package, holds package information
    struct Package
    {
       float weight;
       float length;
       float width;
    };
    
    
    // Open our database
    bool openFile( ifstream &ins );
    
    // Get/show packages from package database
    void getPkgs( ifstream &ins );
    
    
    
    // The filename of the database
    string theDatabase;
    
    
    
    int main( )
    {
       // in = Database input stream
       
       ifstream in;
    
       // Open the database stream
       
       openFile( in );
    
       // Get the package information from database stream
       
       getPkgs( in );
    
       // Return success
       
       return 0;
    }
    
    
    
    // Open the file from where we'll reading in package information
    bool openFile(
       ifstream &in )   // The stream we'll be using to fetch the data
    {
       // Ask the user nicely what database we're getting the
       // Package information from
       
       cout << endl;
       cout << "Enter the name of the package data file: ";
       cin >> theDatabase;
    
       // Open the database
       
       in.open( theDatabase.c_str( ) );
    
       // If opening the database failed, exit program with error
       
       if ( in.fail( ) )
       {
          cout << "Error opening file: " << theDatabase << endl;
          exit ( 1 );
       }
    
       // Close database stream and return success
       
       in.close( );
       return 1;
    }
    
    
    
    // Get package data from database stream
    void getPkgs(
       ifstream &ins )   // The stream we're fetching package information from
    {
       // np = The number of packages
       
       int np;
    
       // Open the database
       
       ins.open( theDatabase.c_str( ) );
    
       // Fetch the number of packages from the first line of the package database
       
       ins >> np;
    
       // Create enough package data for our inmemory database
       
       Package *packList = new Package[np];
    
       // Put the data from the database file to our inmemory database
       
       for( int i=0; i < np; i++ )
       {
          cout << i+1 << endl;
          ins >> packList[i].weight;
          ins >> packList[i].length;
          ins >> packList[i].width;
       }
    
       // Close the database stream
    
       ins.close( );
    
       // Output packages
    
       for( int i=0; i<np; i++ )
       {
          cout << "# " << ( i+1 ) << endl;
          cout << "Weight: " << packList[i].weight << "lbs" << endl;
          cout << "Length: " << packList[i].length << "ft" << endl;
          cout << "Width:  " << packList[i].width << "in" << endl;
          cout << "-----" << endl;
       }
    }
    This is the packages.txt database file:
    Code:
    3
    45 3000 12
    67 4000 11
    93 9000 2
    Here's some sample output:
    Code:
    Shiva:/home/kleid/Programming/Laboratory# ./a.out
    
    Enter the name of the package data file: packages.txt
    1
    2
    3
    # 1
    Weight: 45lbs
    Length: 3000ft
    Width:  12in
    -----
    # 2
    Weight: 67lbs
    Length: 4000ft
    Width:  11in
    -----
    # 3
    Weight: 93lbs
    Length: 9000ft
    Width:  2in
    -----

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ??help
    By Sal79 in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-21-2007, 06:28 AM

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