1

could someone also tell me if the actions i am attempting to do which are stated in the comments are correct or not. i am new at c++ and i think its correct but i have doubts

#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 wrong=0;
 int itemnum='  ';
 char next;
 in_stream.open("ITEMLIST.txt", ios::in); // list of avaliable items
   if( in_stream.fail() )// check to see if itemlist.txt is open
   {
       wrong++;
      cout << " the error occured here0, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n" << endl;
      exit(1);
      }
   else{
    cout << " System ran correctly " << endl;

 out_stream1.open("listWititems.txt", ios::out); // list of avaliable items
   if(out_stream1.fail() )// check to see if itemlist.txt is open
   {
       wrong++;
      cout << " the error occured here1, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n";
      exit(1);
      }
   else{
    cout << " System ran correctly " << endl;
   }

 in_stream2.open("PRICELIST.txt", ios::in);    
  if( in_stream2.fail() )
   {
       wrong++;
      cout << " the error occured here2, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n";
      exit (1);
   }
   else{
    cout << " System ran correctly " << endl;
   }

 out_stream3.open("listWitdollars.txt", ios::out);    
  if(out_stream3.fail() )
   {
      wrong++;
      cout << " the error occured here3, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n";
      exit (1);
   }
   else{
    cout << " System ran correctly " << endl;
   }

 in_stream4.open("display.txt", ios::in);
  if( in_stream4.fail() )
   {
       wrong++;
      cout << " the error occured here4, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n";
      exit (1);
   }
   else{
    cout << " System ran correctly " << endl;
   }


 out_stream5.open("showitems.txt", ios::out);
   if( out_stream5.fail() )
   {
       wrong++;
      cout << " the error occured here5, you have " << wrong++ << " errors"  << endl;
      cout << "Error opening the file\n";
      exit (1);
   }
   else{
    cout << " System ran correctly " << endl;
   }
    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; 


}
Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
user320950
  • 187
  • 3
  • 7
  • 15
  • This is entirely too much code. You also need to fix your indentation. I suspect you are actually missing a } somewhere, but it's hard to tell because your code has no reasonable structure. – Billy ONeal Apr 20 '10 at 04:23
  • duplicate of http://stackoverflow.com/questions/2672367/three-out-of-five-file-streams-wont-open-i-believe-its-a-problem-with-my-ifstrea same code – Eddy Pronk Apr 20 '10 at 05:18
  • 2
    This is a great example of why properly indenting your code is so important. – R Samuel Klatchko Apr 20 '10 at 05:55

3 Answers3

7

I'd hazard a guess you're missing '}' in first else block.

To avoid such problems in the future, fix your indentation (or let AStyle or similar program fix it for you) and use editor that highlights the syntax (especially blocks of code between braces).

chalup
  • 8,358
  • 3
  • 33
  • 38
  • i dont understand how to fix my indentation. i thought it was right after checking to make sure the files open. do i open the file or put file name before i do code for specific file.on mine i didnt put the file name before – user320950 Apr 20 '10 at 12:46
  • I'm not talking about logic in your code. Indentation = the number of spaces at the beginning of each code line = the way your code looks. If you use consistent, clear indentation rules, it's very easy to catch mistakes like the missing brace in the else block (which causes compilation error in the code you posted). Check http://en.wikipedia.org/wiki/Indent_style for some widely used indentation standards (I prefer the one called "1TBS variant"). It doesn't really matter which one you choose, as long as you stick to it in all code you write. – chalup Apr 20 '10 at 13:18
  • ok thanks i took out the error checks because they were causing the errors. Do you know why? – user320950 Apr 20 '10 at 15:10
  • What you just wrote doesn't make any sense. If you want to get answers, use proper terminology in your questions. – chalup Apr 20 '10 at 16:11
  • sorry, what i mean by error checks is : checks to see if the file opened correctly – user320950 Apr 20 '10 at 16:44
  • What kind of errors? If you mean compilation error, I answered this question already: you were missing a curly brace in the "else" block of the first error check. If you mean any other error (linker error? segmentation fault? one of your error checks is true?), you should describe it in details, and we might be able to help you. – chalup Apr 20 '10 at 17:57
1

Your first if else statement has a missing } after the { for your else. My suggestion: get rid of the { since it's only a single line of code.

rawr
  • 11
  • 1
0

It could be that you miss "}" in the end, another important thing that you should beware is that try to use the full address of your files. I met the same problem, and different compiler would read adresses in different ways. So try to make clear the address of files.

Mike22LFC
  • 987
  • 9
  • 15