-
!?!?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;
}
-
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.
-
I've modified your post to make your code readable. Please use code tags in the future.
-
>> inPack(ins, &);
maybe:
Code:
inPack(ins, packList[i]);
-
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
-----