Don't Understand the problem i'm facing

This is not a compling issue

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3067 Views - Last Post: 03 January 2010 - 04:07 PM Rate Topic: -----

#1 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Don't Understand the problem i'm facing

Post icon  Posted 02 January 2010 - 02:28 PM

Assignment: Write a program that merges the numbers in two files and writes all the numbers into a third file. your program takes input from two different files and writes it output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to largest. after the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. your program should define a function that is called with the two input - file streams and the out - put file stream as three arguments. if this is being done as a class assignment, obtain the file names from your instructor.

my code
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in_stream;
  ofstream out_stream;
  int numbersA,numbersB;
  
  in_stream.open("MERGEB.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEB.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
  while (in_stream >> numbersB)
  {
   in_stream >> numbersB;
  } 
  
  in_stream.close();
  
  in_stream.open("MERGEA.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEA.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
  while (in_stream >> numbersA)
  {
   in_stream >> numbersA;
  }
  out_stream.open("MERGER.DAT", ios::app);
  if (out_stream.fail())
  {
   cout << "The Program was not able to create the file MERGER.DAT";
   cout << "The Program will now exit\n";
   system("pause");
   exit(1);
  }  
  in_stream.close();
  out_stream.close();
  cout << endl;
  cout << "<program end>";
  cout << endl << endl << endl;
  system("pause");
  return 0;
}


download the file attached

after you have downloaded and compiled my code in the same place as the 2 dat files
Question: why doesn't the program open the second file? i have tried to place in_stream.close

I hope someone is able to answer my Question

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Don't Understand the problem i'm facing

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 02:40 PM

 while (in_stream >> numbersB)
  {
   in_stream >> numbersB;
  } 



This is the problem (or one of the problems)... not need to use in_stream >> numbersB inside the while loop. This is what while already does. You could do something like that if you want:
while (!in_stream.eof()) // eof = end of file. So loop while we aren't at the end of the file
{
   in_stream >> numbersB;
   // blalblal here
}



EDIT: I have checked the rest of code too, and it is wrong. You've used 'in_stream >> numbersB'. It is clearly that 'numbersB' will end up storing the last value from your input file. Use arrays to store every number from the input file, then manipulate it as you want ...

This post has been edited by sarmanu: 02 January 2010 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 02:44 PM

Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 03:39 PM

sarmanu said:

This is the problem (or one of the problems)... not need to use in_stream >> numbersB inside the while loop.


no

while (!in_stream.eof()) // eof = end of file. So loop while we aren't at the end of the file
{
   in_stream >> numbersB;
   // blalblal here
}



will hang the loop in the i/o error case because the eof flag never will set

his is right

but his should be
	while (in_stream >> numbersB)
	   ;


the loop with an empty body

This post has been edited by c.user: 02 January 2010 - 03:43 PM

Was This Post Helpful? 0

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 03:45 PM

I've always used .eof() with no problems.

 while (in_stream >> numbersB)
	  ;



This does nothing ... he needs to store every number from the input file into an array, for later use!

This post has been edited by sarmanu: 02 January 2010 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 03:46 PM

still need
help

i havent been able to get the program to merge the files

heres what i got so far after figuring that out.

#include <iostream>
#include <fstream>
using namespace std;

void Mergeb(ifstream& in_stream, ofstream& out_stream, int& numbersA, int& numbers, int& less);
void Merging_time(ifstream& in_stream, ofstream& out_stream, int& numbersA, int& numbers, int& less);
int main()
{
  ifstream in_stream;
  ofstream out_stream;
  int numbers,numbersA,less(0);
  out_stream.open("MERGER.DAT", ios::app);
  if (out_stream.fail())
  {
   cout << "The Program was not able to create the file MERGER.DAT";
   cout << "The Program will now exit\n";
   system("pause");
   exit(1);
  }
  in_stream.open("MERGEA.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEA.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
	while (! in_stream.eof())
  {
   in_stream >> numbers;
   cout << endl; 
   if (numbers > less) 
   {
	less = numbers;
	cout << less;
	out_stream << less;
	out_stream << endl;
	}
   cout << endl;
 }  
 in_stream.close();
 out_stream.close();

  Mergeb(in_stream,out_stream,numbersA,numbers,less);

  
  

  cout << endl;
  cout << "<program end>";
  cout << endl << endl << endl;
  system("pause");
  return 0;
}
void Mergeb(ifstream& in_stream, ofstream& out_stream, int& numbersA, int& numbers, int& less)
{
  out_stream.open("MERGER.DAT", ios::app);
  in_stream.open("MERGEB.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEB.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
  while (!in_stream.eof())
  {
   in_stream >> numbersA;
   cout << endl; 
   if (numbers > less) 
   {
   less = numbersA;
	cout << less;
	out_stream << less;
	out_stream << endl;
	}
   cout << endl;
 }
  

  in_stream.close();
  out_stream.close();
}

Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 03:47 PM

View Postsarmanu, on 2 Jan, 2010 - 10:45 PM, said:

I've always used .eof() with no problems.

 while (in_stream >> numbersB)
;



This does nothing ... he needs to store every number from the input file into an array, for later use!

You should never use 'eof' in a while loop - have a look here:

http://www.parashift...t.html#faq-15.5
http://www.dreaminco...topic145699.htm

the correct solution to this problem is
 while (in_stream >> numbersB)
 {
		//do something with numbersB
 } 
anything else risks ending up with either an infinite loop or overflowing since "eof" will only be set after attempting to read past the end of a file - meaning that you might have no more meaningful data in the file but eof will not be set because there's a whitspace character at the end; the result is that the final iteration of the loop (If there is one) can add invalid data to your program.

This post has been edited by Bench: 02 January 2010 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 03:56 PM

sarmanu said:

I've always used .eof() with no problems.

because you never obtained an i/o error (your hardware is good for instance)

sarmanu said:

This does nothing ... he needs to store every number from the input file into an array, for later use!

yes, I didn't check for the rest of what is he doing
just considered his loops

while he is reading like while (input >> variable) the loop will read while the end of file doesn't occure or while the file has no a read error

This post has been edited by c.user: 02 January 2010 - 03:59 PM

Was This Post Helpful? 0

#9 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 04:10 PM

I have tried both possible ways. I did use while (in_stream >> numbersB) but that does not seem to work.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_c.user*


Reputation:

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 05:43 PM

AhmerjavedC++ said:

Write a program that merges the numbers in two files and writes all the numbers into a third file.

open two files
copy numbers from the first to the second

	ifstream first;
	ofstream second;
	string number;

	// open here

	while (first >> number) {
		second << number;
		second << " ";
	}



then close the first file and open the third file as the first file

repeat the copying
	while (first >> number) {
		second << number;
		second << " ";
	}



close two files (the third file and the second file), enjoy

This post has been edited by c.user: 02 January 2010 - 05:48 PM

Was This Post Helpful? 0

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Don't Understand the problem i'm facing

Posted 02 January 2010 - 06:25 PM

View PostAhmerjavedC++, on 2 Jan, 2010 - 01:28 PM, said:

Assignment: Write a program that merges the numbers in two files and writes all the numbers into a third file. your program takes input from two different files and writes it output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to largest. after the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. your program should define a function that is called with the two input - file streams and the out - put file stream as three arguments. if this is being done as a class assignment, obtain the file names from your instructor.

my code
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in_stream;
  ofstream out_stream;
  int numbersA,numbersB;
  
  in_stream.open("MERGEB.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEB.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
  while (in_stream >> numbersB)
  {
   in_stream >> numbersB;
  } 
  
  in_stream.close();
  
  in_stream.open("MERGEA.dat");
  if(in_stream.fail())
  {
   cout << "program was not able to open the file MERGEA.DAT file\n";
   cout << "program will now exit\n";
   system("pause");
   exit(1);
  }
  while (in_stream >> numbersA)
  {
   in_stream >> numbersA;
  }
  out_stream.open("MERGER.DAT", ios::app);
  if (out_stream.fail())
  {
   cout << "The Program was not able to create the file MERGER.DAT";
   cout << "The Program will now exit\n";
   system("pause");
   exit(1);
  }  
  in_stream.close();
  out_stream.close();
  cout << endl;
  cout << "<program end>";
  cout << endl << endl << endl;
  system("pause");
  return 0;
}


download the file attached

after you have downloaded and compiled my code in the same place as the 2 dat files
Question: why doesn't the program open the second file? i have tried to place in_stream.close

I hope someone is able to answer my Question


You need to clear the error flags for your input file between closing and opening.


in_stream.close();
in_stream.clear();  
in_stream.open("MERGEA.dat");




Jim
Was This Post Helpful? 0
  • +
  • -

#12 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 03 January 2010 - 11:56 AM

Thanks,

Another issue just popped up.


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void merging_time(ifstream& in_stream2, ofstream& out_stream2, int& numbers, int& numbersA, int last);

int main()
{
  ifstream in_stream,in_stream2;
  ofstream out_stream,out_stream2;
  int numbers,numbersA,numbersB,last(0);
  
  in_stream.open("MergeA.dat");
  if (in_stream.fail())
  {
	cout << "Program was unable to open file MergeA\n";
	cout << "The Program will now exit\n";
	cout << endl;
	system("pause");
	exit(1);
  }
  
  out_stream.open("MERGEB.dat",ios::app);
  if (out_stream.fail())
  {
	cout << "Program was unable to open file MergeB for output\n";
	cout << "Program will now exit\n";
	cout << endl;
	system("pause");
	exit(1);
   }
   
  while (in_stream >> numbersA)
   {
	in_stream >> numbersA;
	out_stream << " ";
	out_stream << numbersA;
	out_stream << " ";
   }
   out_stream.close();
   in_stream.close ();
 
   
   merging_time(in_stream2,out_stream2,numbers,numbersA,last);
   
   
  cout << "<Program End>";
  cout << endl << endl << endl;
  system("pause");
  return 0;
}
void merging_time(ifstream& in_stream2, ofstream& out_stream2, int& numbers, int& numbersA, int last)
{

 
 in_stream2.open("MERGEB.dat");
   if (in_stream2.fail())
	 {
	 cout << "Program was unable to open the file MergeB\n";
	 cout << "Program will now exit\n";
	 cout << endl << endl;
	 system("pause");
	 exit(1);
	 }
   out_stream2.open("merger.dat");
   if (out_stream2.fail())
	 {
	 cout << "Program was unable to create file merger.dat\n";
	 cout << "program will now exit\n";
	 cout << endl << endl << endl;
	 system("pause");
	 exit(1);
	 }
   while (in_stream2 >> numbers)
   {
	in_stream2 >> numbers;
	if (numbers < last)
	{
	 last = numbers;
	 out_stream2 << endl;
	 out_stream2 << last;
	 out_stream2 << endl;
	}
	 in_stream2.close();
	 out_stream2.close();
   }
}
	 
  



the data is not being copied properly can someone just explain to me why my code is doing that? so i can go ahead and attempt to fix it and get the error corrected.

This post has been edited by AhmerjavedC++: 03 January 2010 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 Guest_c.user*


Reputation:

Re: Don't Understand the problem i'm facing

Posted 03 January 2010 - 12:41 PM

while (in_stream >> numbersA)
   {
	in_stream >> numbersA;


you repeated your old error in both loops

This post has been edited by c.user: 03 January 2010 - 12:42 PM

Was This Post Helpful? 0

#14 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 03 January 2010 - 12:59 PM

ok here is what i have done now. so now the output is going through to the file merger.dat but now the problem is simple sorta. the if statement i am trying to get the numbers sorted.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;


int main()
{
  ifstream in_stream,in_stream2;
  ofstream out_stream,out_stream2;
  int numbers,numbersA(0),numbersB,last(0);
  
  in_stream.open("MergeA.dat");
  if (in_stream.fail())
  {
	cout << "Program was unable to open file MergeA\n";
	cout << "The Program will now exit\n";
	cout << endl;
	system("pause");
	exit(1);
  }
  
  out_stream.open("MERGEB.dat", ios::app);
  if (out_stream.fail())
  {
	cout << "Program was unable to open file MergeB for output\n";
	cout << "Program will now exit\n";
	cout << endl;
	system("pause");
	exit(1);
   }
   
  while (in_stream >> numbersA)
   {
	out_stream << " ";
	out_stream << numbersA << " ";
   }
   out_stream.close();
   in_stream.close ();
   
	in_stream2.open("MERGEB.dat");
   if (in_stream2.fail())
	 {
	 cout << "Program was unable to open the file MergeB\n";
	 cout << "Program will now exit\n";
	 cout << endl << endl;
	 system("pause");
	 exit(1);
	 }
   out_stream2.open("merger.dat");
   if (out_stream2.fail())
	 {
	 cout << "Program was unable to create file merger.dat\n";
	 cout << "program will now exit\n";
	 cout << endl << endl << endl;
	 system("pause");
	 exit(1);
	 }
   while (in_stream2 >> numbers)
   {
	 if (numbers > last)
	 {
	 out_stream2 << numbers;
	 out_stream2 << endl;
	 }
   }
	 in_stream2.close();
	 out_stream2.close();
	 
	  
  cout << "<Program End>";
  cout << endl << endl << endl;
  system("pause");
  return 0;
}


This post has been edited by AhmerjavedC++: 03 January 2010 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#15 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Don't Understand the problem i'm facing

Posted 03 January 2010 - 01:14 PM

currently out put in Merger.dat

2
4
9
13
21
24
99
1
3
5
8
16
42
100
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2