I cant figure out how to import a file which contains the price of an item. and once i import the price idk how to put it in my program. AND I DONT TO USE IT AN ARRAY. I AM IMPORTING EACH ITEM ONE AT A TIME.

void main ()
{
    int itemlist, itemlistnumb=0, errorlist=0;// errorlist keeps track of errors and I HAVE TO DISPLAY THEM AT THE END.
    ofstream numb_item_list ("itemlist.txt");// list of avaliable items
    ofstream da_price_list ("pricelist.txt");// list of avaliable item prices
    numb_item_list << " What items would you like to purchase, please type the number of the item ";
    ifstream items("itemlist.txt");// takes input from user
   {
    if(items !=("itemlist.txt"))// if user chooses an item that is not on the list then an error occurs
        {
            errorlist++; // counts number of errors occur
            cout<< " Error " << items << " is not a item " << endl;
        }
    else{
        itemlistnumb++; // keeps track of items purchase
         while( items == ("itemlist.txt"))
         {
             // how would i import the price of the item so i could do this. I have not
             // declared price or curr_total i am at a wall. but i wanna use them
             cout << itemnumber << "," << price << "," <<  curr_total << endl;
        }
   }
    items.close();
}

Your terminology sucks -- its "reading the data file", not "importing" it. Your program is reading the data file all wrong. google for and read tutorial about "c++ file i/o".

im attempting to do this:
You are to write a program that process a list of items purchased and write a receipt showing the total cost of the items in the list. The list is in a file called “itemlist.txt”. This file contains the inventory numbers of an unknown number of items that have been purchased. A second file, called “pricelist.txt” contains a list of items (stored as inventory numbers) and their respective prices. You must also create an output file called “receipt.txt” that contains a header and the receipt data.
Read the itemlist file one item at a time and for each item you must use the pricelist file to find the cost of the item. Once you have found the price of the item, write the item number, its price, and the current total of the purchase to an output file called “receipt.txt”. The receipt file’s data must be displayed in a neatly-formatted table (you can look at the example file for ideas on this).

my problems are these:
i got a couple errors coming up on 2 lines and i cant figure out why. also i have a couple question within the program if someone could answer i could move forward. i also want to know if i am reading a file and writing the file.

problem wit program:

ifstream in_stream;
    ifstream in_stream2;

    ofstream.open("itemlist.txt"); // list of avaliable items & I GET A SYNTEX ERROR C2143 MISSING ';' BEFORE '_'
        while(! in_stream.eof() && in_stream2.eof() ) // reads in_stream and in_stream2 until the end of file
        {
            in_stream.open("itemlist.txt"); // i need to get the items list which is in this file and check to see if item is in item list
                                           // if so i need to send it to receipt.txt
            in_stream2.open("pricelist.txt");// i need to get the price list which is in this file and check to see if price is in price list
                                            // if so i need to send it to receipt.txt
            cout << next;                  // reads next item and it needs to check to 
            in_stream.get(next);
        }
    ofstream.open("pricelist.txt"); // list of avaliable item prices & I GET A SYNTEX ERROR C2143 MISSING ';' BEFORE '_'
        while(! in_stream2.eof() ) // reads pricelist until end of line
        {
            in_stream2.get(next); // reads price 
            cout << next;         // i need to get the price list which is in this file and check to see if price is in price list
                                            // if so i need to send it to receipt.txt   
            in_stream2.get(next);
        }

COULD SOMEONE HELP OUT AND GET ME ON THE RIGHT TRACK IF I AM WRONG???

when using i/o steam alot of examples use the variable "fin". and i cant figure out what it means. does anyone know how to use "fin" in i/o stream or can i use any variable like i am doing above.

you can use any variable name you want.

Here's a short code snippet how to read a file

ifstream fin("pricelist.txt");
float price;
int itemnum;
// read all the prices in the file
// the loop will continue until 
// end-of-file is reached.
while( fin >> itemnum >> price )
{
   cout << itemnum << ' ' << price << '\n';
}

Edited 6 Years Ago by Ancient Dragon: n/a

you can use any variable name you want.

Here's a short code snippet how to read a file

ifstream fin("pricelist.txt");
float price;
int itemnum;
// read all the prices in the file
// the loop will continue until 
// end-of-file is reached.
while( fin >> itemnum >> price )
{
   cout << itemnum << ' ' << price << '\n';
}

could i use
ifstream in_stream2 in place of fin?

then y does everyone use fin then? im just asking i really dont know it was confusing me

and 4 my code above and yours the variable price and item number. correct me if im wrong i will need to declare them which u have done and then initalize them? which is giving the variables info meaning to get the price i have to open pricelist.txt which has the itemlist and pricelist in 2 different columns.
1. i will need to make an formula that reads the difference between itemlist and pricelist.
2. the prices on the pricelist have decimal places so could i make a bool statement r some equation or expression that checks to see if it is data of type double then it is a price in the pricelist and if not then it is an item number on the itmelist?
3. and how could i make sure that item number and price is correct
for example:
items price
5 10.15
2 8.14
- make sure that it when it reads item 5 it gets the price of 10.15 not 8.14

please help i second guess myself constantly cuz i am new at c++

Edited 6 Years Ago by timbomo: n/a

This article has been dead for over six months. Start a new discussion instead.