By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Lost

P: 3
I am in the last week of a C++ class and lost on vectors can someone point me to a site that has information. My book lacks it and my program requires it, i am on right track just lost on how to read them in correctly from an input file. It does not seem to have to have a push back. Any suggestions ..

Code as follows:

void printResults(ifstream& infile, vector<int>& itemId, vector<char>& itemName, vector<int>& pOrdered, vector<int>& pInstore, vector<int>& psold, vector<double>& manufPrice, vector<double>& sellingPrice, int noOFRows);
void readFile (ifstream& infile, vector<int>& itemId, vector<char>& itemName, vector<int>& pOrdered, vector<int>& pInstore, vector<int>& psold, vector<double>& manufPrice, vector<double>& sellingPrice, int noOFRows);
//void readFile(ifstream& inData, string item_Name[], int item_ID[],
// int p_Ordered[], int p_In_Store[], int p_Sold[], int manu_Price[],
// int selling_Price[], int& no_Of_Rows);


int _tmain(int argc, _TCHAR* argv[])
{

vector<int>itemID;
vector<char>itemName;
vector<int>pOrdered;
vector<int>pInStore;
vector<int>pSold;
vector<double>manufPrice;
vector<double>sellingPrice;
ifstream infile;
int pos = 0;
infile.open("c:\\inData.txt");
if (!infile)
{
cout << "cannot open the input file." << endl;
cout << "program terminates !!!" << endl;
return 1;
}

while(infile)
{
infile >> itemID[pos] >> itemName[pos] >> pOrdered[pos] >> pInStore [pos]>> pSold[pos]
>> manufPrice[pos] >> sellingPrice[pos];
pos++;

}




void readFile (ifstream& infile, int itemId, char itemName, int pOrdered, int pInstore, int psold, int manufPrice, int sellingPrice, int noOFRows);
{
int pos;
string temp;


while(infile)
{
itemID.push_back(inData);
getline(inData, temp);
itemName.push_back(temp);
pOrdered.push_back(inData);
manufPrice.push_back(inData);
sellingPrice.push_back(inData);
}//End while
no_Of_Rows = item_Name.size();

pInstore.resize(no_Of_Rows);
pSold.resize(no_Of_Rows);

for(pos = 0; pos < noOfRows; pos++)
{
pInStore[pos] = pOrdered[pos];
pSold[pos] = 0;
}//End for
}//End readFile


{
void printResults(ifstream& infile, vector<int>& itemId, vector<char>& itemName, vector<int>& pOrdered, vector<int>& pInstore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice,int noOfRows);
}
//infile.close();

//void printResults(ifstream& infile, vector<int>& itemId, vector<char>& itemName, vector<int>& pOrdered, vector<int>& pInstore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice,int noOfRows);
}




void printResults(vector<int>& itemId ,vector<char>& itemName, vector<int>& pOrdered, vector<int>& pInstore, vector<int>& pSold, vector<int>& manufPrice, vector<int>& sellingPrice)

{
double sum = 0;
double totalinv = 0;
int i;
int j;
int noOfRows = 0;
for(j = 0; j< noOfRows; j++)
{
pInstore[j] = pOrdered[j] - pSold[j];
sum = sum + pInstore[j];
totalinv = totalinv + (pInstore[j] * manufPrice[j]);

}

for(i = 0;i < noOfRows; i++)
{
pInstore[i] = pOrdered[i] - pSold[i];
sum = sum + pInstore[i];
}

//for(j = 0; j < noOfRows; j++)
{
cout << left;
cout << setw (4) <<itemId[i];
cout << right;
cout << setw(15) <<itemName[i];
cout << right;
cout << setw(4) <<pOrdered[i];
cout << right;
cout << setw(4) <<pInstore[i];
cout << right;
cout << setw(4) << pSold[i];
cout << right;
cout << setw(6) <<manufPrice[i];
cout << right;
cout << setw(6) <<sellingPrice[i];
cout << right;
cout << endl << " Total Inventory:$ " << totalinv <<endl <<endl;
cout <<" Total number of items in the store: " << sum <<endl <<endl;
//infile.close();
}

}
Jun 20 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
ifstream infile;
int pos = 0;
infile.open("c:\\inData.txt");

vector<int>itemID;


while(infile)
{
itemID.push_back(inData);
getline(inData, temp);
vector::push_back is not your issue. Your are confusing infile with inData plus you attempt to push_back inData (the file name) into a vect<int>.

Your code suffers from several type mismatches. If you clear these up, it should compile OK. Then you can see about whether irt works or not.
Jun 20 '07 #2

P: 3
Your code suffers from several type mismatches. If you clear these up, it should compile OK. Then you can see about whether irt works or not.

The book has 4 pages on vectors that is it and one bad example, inData of coarse is the file name and I have no information on how many are in the file.

I have removed inData from itemName.push_back(); same issue and had added
//itemName.push_back(infile); there , problem lies in I have no example to work with.. Is there any pages out there that can give me a clue on how to set it up. also if i just comment that whole section out it compiles but i get a major malfunction of expression: vector subscript out of range and per all the seaching i can find tells me one of them is out of control.
Jun 20 '07 #3

P: 3
vector::push_back is not your issue. Your are confusing infile with inData plus you attempt to push_back inData (the file name) into a vect<int>.

Your code suffers from several type mismatches. If you clear these up, it should compile OK. Then you can see about whether irt works or not.
the book has one example with is this

int main()
{
vector<int> intlist;
unsigned int i;

than
intlist.push_back(13); <-------- this is when you know what you are pushing there. I dont have an idea of what will be read in from the file, so at a loss of what you actually place there.
Jun 20 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
The intlist is a vector<int>. You can only push_back an int.

If you are reading from a disc file, only the ints can be put in this vector.

Remember that chars are ints. A char is an 8-bit integer. So you can push_back a char in a vector<int>. The compiler will automatically convert the char to an int. A char with a value of B would be converted to an int of 66. The 66 and be used with vector<int>::push_back().

You could read you file byte by byte and push everything into a vector<int>. The 13 would be read as two bytes.

Yout could then iterate the container and assign all values in the container to a char and then display the char. You should see your text where the value of the int has a value that has a glyph in the ASCII table.
Jun 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.