views:

122

answers:

4

This program is supposed to read files and write them. I took the file open checks out because they kept causing errors. The problem is that the files open like they are supposed to and the names are correct but nothing is on any of the text screens. Do you know what is wrong?

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
    ifstream in_stream; // reads itemlist.txt
    ofstream out_stream1; // writes in items.txt
    ifstream in_stream2; // reads pricelist.txt
    ofstream out_stream3;// writes in plist.txt
    ifstream in_stream4;// read recipt.txt
    ofstream out_stream5;// write display.txt

    float price='  ',curr_total=0.0;
    int itemnum='  ', wrong=0;
    char next;

    in_stream.open("ITEMLIST.txt", ios::in); // list of avaliable items
    out_stream1.open("listWititems.txt", ios::out); // list of avaliable items           
    in_stream2.open("PRICELIST.txt", ios::in);           
    out_stream3.open("listWitdollars.txt", ios::out);               
    in_stream4.open("display.txt", ios::in);    
    out_stream5.open("showitems.txt", ios::out);

  in_stream.close(); // closing files.
  out_stream1.close();
  in_stream2.close();
  out_stream3.close();
  in_stream4.close();
  out_stream5.close();
  system("pause");

    in_stream.setf(ios::fixed);
    while(in_stream.eof())
    {
        in_stream >> itemnum;
        cin.clear();
        cin >> next;
    }

    out_stream1.setf(ios::fixed);
    while (out_stream1.eof())
    {
        out_stream1 << itemnum;
        cin.clear();
        cin >> next;
    }

    in_stream2.setf(ios::fixed);
    in_stream2.setf(ios::showpoint);
    in_stream2.precision(2);
    while((price== (price*1.00)) && (itemnum == (itemnum*1)))
         {
             while (in_stream2 >> itemnum >> price) // gets itemnum and price
             {
                 while (in_stream2.eof())  // reads file to end of file
                 {
                 in_stream2 >> itemnum;
                in_stream2 >> price;
                price++;
                curr_total= price++;
                in_stream2 >> curr_total;
                cin.clear();  // allows more reading
                cin >> next;
                }

            }
        }

    out_stream3.setf(ios::fixed);
    out_stream3.setf(ios::showpoint);
    out_stream3.precision(2);
    while((price== (price*1.00)) && (itemnum == (itemnum*1)))
        {
         while (out_stream3 << itemnum << price)
             {
                 while (out_stream3.eof())  // reads file to end of file
                 {
                 out_stream3 << itemnum;
                 out_stream3 << price;
                 price++;
                 curr_total= price++;
                 out_stream3 << curr_total;
                 cin.clear();  // allows more reading
                 cin >> next;
                 }
                return itemnum, price;
            }
        }

    in_stream4.setf(ios::fixed);
    in_stream4.setf(ios::showpoint);
    in_stream4.precision(2);
    while ( in_stream4.eof())
    {
        in_stream4 >> itemnum >> price >> curr_total;
        cin.clear();
        cin >> next;
    }

    out_stream5.setf(ios::fixed);
    out_stream5.setf(ios::showpoint);
    out_stream5.precision(2);
    out_stream5 <<setw(5)<< " itemnum " <<setw(5)<<" price "<<setw(5)<<" curr_total " <<endl; // sends items and prices to receipt.txt 
    out_stream5 << setw(5) <<  itemnum << setw(5) <<price << setw(5)<< curr_total; // sends items and prices to receipt.txt 
    out_stream5 << " You have a total of " << wrong++ << " errors " << endl; 


}
+4  A: 

for instance: why are you closing your in_stream before reading it ?

while(in_stream.eof()) should be while(in_stream.good()) or while(!in_stream.eof()), assuming that you'd kept file opened

PW
my homework says i have to do this:file contains the inventory numbers of an unknown number of items that have been purchased. A second file,contains a list of items (stored as inventory numbers) and their respective prices. You must also create an output file that contains a header and the receipt data.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
@user: In what file does it show the number of items purchased?
Emile Cormier
the ITEMSLIST.txt has the original items and PRICELIST.txt has the item numbers and prices in 2 columns and these are the two files that i am wanting to read from and the file to be listWititems.txt and listWitprices.txt write to and display.txt and showitems.txt are the files that show the receipt. i do appreciate the help i am learning this way
Does the ITEMSLIST.txt file show the quantity of each item purchased (in a second column)? Or are the the item numbers repeated if more than one of the same type of item is purchased?
Emile Cormier
no the ITEMLIST.txt does not show the quantity of item purchased. ITEMLIST.txt is just a whole bunch of whole numbers without decimal places. and if an itemnum is repeated it just does the same. it doesnt say a specical message. is that what you are asking?
A: 

You should put the file open checks back in and add code which will detect these error conditions and give you descriptive messages about what is wrong. Then when you get those messages you should research those conditions in order to find out how to correct them.

I don't know the proper way for reporting those error conditions in C++, but here is how I used to do it in C:

FILE *infile = fopen(filename, "r");
if (infile == NULL)
{
  perror(filename);
  exit(1);
}

This gives nice error messages like: thisfile: No such file or directory

The exit() call also means that your code exits at the first error. This prevents your program from going on to do damage with invalid assumptions (suppose you accidentally opened the wrong file, and didn't realize it? or accidentally read a bunch of null values because your file wasn't open, and you didn't know?) Often resolving the first error encountered will resolve later errors as well, or at least make them resolvable.

I had to look up the manpage for fopen() to see what it returns in the event of an error to learn that I should do the if (infile == NULL) check. You will have to read the documentation for what you are using.

perror() works by checking the externally defined errno variable. Other C programmers skip perror() and check errno themselves; perror() is simply a convenience method. C++ may have some other, better convenience method. (Without perror(), I would again need to carefully read the fopen() manpage to see that an error causes errno to be set and what the possible values are.)

Perl has an even more convenient means of handling this, which I always liked:

open my $infile, $filename or die "Can't open $filename: $!";

The $! magic variable basically corresponds to the output of the perror() function. And again, internally it's just C code checking errno.

skiphoppy
+1  A: 

so i have broke this code down and i believe the area i should look at is the

while((price== (price*1.00)) && (itemnum == (itemnum*1)))

code that is causing the problem does anyone agree? so now i need to figure out away to tell the difference bw itemnum and price so i can tally them up to get the total of each at the end? I have to make it so it can read any number and any price.

A: 

its a problem with the ifstreams but i dont know how to fix them do anyone know how too??