I need help with this file problem....???

Ok, the problem i am having with this problem is specifically number 3.

i don't know how to take a specific piece of info from the file and convert it into a double so i can perform the arithmetic.

please help.



The Bite-a Vite-a vitamin store needs an "expert" computer programmer to help keep track of their inventory. The store maintains all their vitamin stock information on disk. The data is organized as follows:
 
The first column contains the vitamin name (A, B, C, etc.)
The second column contains the unit price of a single jar of that particular vitamin.
The third column has the number of jars of that vitamin in the store.
 
For example:
 
A 12.95 23
K 9.99 56
Z 6.99 25
 
Write a C++ program, on the next page, that will do the following:
 
1. Read data from a data file called inventory located on the store’s hard drive in a directory called c:\store. The store inventory fluctuates so you do not know how much data there is to process.
 
2. Call a function that will return the total value of the store’s inventory for that one vitamin product (unit_price * number_of_jars).
 
3. You will also calculate:
i)  The average price of a vitamin
ii) The total inventory (total number of jars)
 
4. Print the output so that it is organized as follows:
 
 
Vitamin-- Price-- Inventory-- Total Inventory
--------------------------------------------------
A-- 12.95-- 23-- 297.85
K-- 9.99-- 56-- 559.44
Z-- 6.99-- 25-- 174.75
 
The average price for a vitamin is: $9.98
The total store inventory is 104 jars of vitamins
 
This seems like simple division and addition to me. What, specifically, are you having trouble with?
my problem isn't the arithmetic, it's the fact that i have to extract the numbers from a txt file, i don't know how to do that.

i am searching a little on google and found something that would go like this:

birds>>birdname>>price>>inventory;

i think that might extract the specific values and put them into variables but now how do i do it for specific lines.
This should be a good starting point for you. Once you've gone over this a little bit, you should have a better understanding of what you're looking at.

http://www.cplusplus.com/doc/tutorial/files/
ok this is what i have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream vitamins;
int price, numofvitamins;
char vname[25];
vitamins.open("inventory.txt");
if(!vitamins)
{
	cout<<"File Not Found";
	return 1;
}
vitamins>>vname>>price>>numofvitamins;
cout<<"Vitamin\tPrice\tInventory\tTotal Inventory\n";
cout<<vname<<'\t'<<price<<'\t'<<numofvitamins<<'\t'<<price*numofvitamins<<'\n';

return 0;
}


i still need help... line 15 above only takes the info from the first line of the txt file, how do i get it from the second and third line.

also when i run this program my inventory shows up as -858993460, why is it doing that.


p.s. my inventory.txt file reads as follows:

A 12.95 23
K 9.99 56
Z 6.99 25
only takes the info from the first line of the txt file, how do i get it from the second and third line.
You keep reading, till there is nothing to read.
¿How will you do it if the input comes from the user?

when i run this program my inventory shows up as -858993460, why is it doing that.
Because the price is writted as a double, but you are using an integer to read it.
A 12.95 23
1
2
3
'A' -> name
12 -> price
'.' -> numofvitamins //fail 
You keep reading, till there is nothing to read.
¿How will you do it if the input comes from the user?


umm... i have no idea, i probably know, but i am just blanking out right now.

sorry for being so slow, i am still VERY new to c++

is there any way you could show me an example on how to do this, you don't have to necessarily answer this problem

Because the price is writted as a double, but you are using an integer to read it.
A 12.95 23


wow... i feel like a fool now, thank you for that.
1
2
3
while( input>>name>>price>>quantity ){
  //process 1 product
}
This problem can be done online.

But you could also store the info in a container, to use it later.
1
2
3
4
5
vector<product> container;
while( input>>name>>price>>quantity )
  container.push_back( product(name,price,quantity) );

//Now you have all the products in the vector 
whewww, i figured it out.

i would like to thank everybody who helped me out with this, all of you brought one step closer to figuring it out.... lol

anyway this is what i came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void totalinventory(double, double);
int main()
{
ifstream vitamins;
double price, numofvitamins, sum, avg, totalinv, count;
char vname[25];
vitamins.open("inventory.txt");
count=0;
if(!vitamins)
{
	cout<<"File Not Found";
	return 1;
}
sum=0;
totalinv=0;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Vitamin\tPrice\tInventory\tTotal Inventory\n";
while (!vitamins.eof())
{
vitamins>>vname>>price>>numofvitamins;
cout<<vname<<'\t'<<price<<'\t'<<numofvitamins<<"\t\t";
totalinventory(price, numofvitamins);
count++;
sum=sum+price;
totalinv=totalinv+numofvitamins;
}
avg=sum/count;
cout<<"\nThe average price for a vitamin is: "<<avg<<endl;
cout<<"The toal store inventory is "<<totalinv<<" jars of vitamins\n\n";
return 0;
}
 void totalinventory(double x,double y)
 {
	 double z;
	 z=x*y;
	 cout<<z<<endl;
 }
Topic archived. No new replies allowed.