What am I doing wrong?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1440 Views - Last Post: 15 December 2012 - 08:16 AM Rate Topic: -----

#16 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: What am I doing wrong?

Posted 15 December 2012 - 06:45 AM

View Postjimblumberg, on 15 December 2012 - 06:37 AM, said:

Please don't open a new topic for an existing problem. Topics merged.


Jim


Just seeing this!! Sorry about that Jim!!

View Postjimblumberg, on 15 December 2012 - 06:44 AM, said:

	ofstream file(file_name);

...
	file.close();
...
	file.open(file_name, ios::in);
...
	file<<id_no<<qoh<<price;




You have created an instance of an ofstream, this can only be used for output. Try using an fstream instead. Then you won't need to open and close the file to do input and output to this stream.

Jim


ok, I will try this!!
Was This Post Helpful? 0
  • +
  • -

#17 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: What am I doing wrong?

Posted 15 December 2012 - 06:52 AM

View Postllwilkins, on 15 December 2012 - 06:45 AM, said:

View Postjimblumberg, on 15 December 2012 - 06:37 AM, said:

Please don't open a new topic for an existing problem. Topics merged.


Jim


Just seeing this!! Sorry about that Jim!!

View Postjimblumberg, on 15 December 2012 - 06:44 AM, said:

	ofstream file(file_name);

...
	file.close();
...
	file.open(file_name, ios::in);
...
	file<<id_no<<qoh<<price;




You have created an instance of an ofstream, this can only be used for output. Try using an fstream instead. Then you won't need to open and close the file to do input and output to this stream.

Jim


ok, I will try this!!


This stopped the never ending loop but it is still not giving me all of the totals that are over 3000.00

I put in for the first record: 10, 100, 10.00 (second) 20,200,20.00,(third)30, 300, 30.00. So with the calculations the second record as well as the third suppose to be displayed but only the third one is being displayed.
Was This Post Helpful? 0
  • +
  • -

#18 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: What am I doing wrong?

Posted 15 December 2012 - 06:57 AM

Post your updated code...

And please do not quote unnecessarily, just use the reply editor in the bottom...
Was This Post Helpful? 0
  • +
  • -

#19 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: What am I doing wrong?

Posted 15 December 2012 - 07:03 AM

OK gotcha!! I didn't even notice that.


//Lynette Wilkins
// Programming Problem 14.1 #1

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
	char file_name[81];

	int id_no;
	int qoh;
	double price;
	double total;
	int response;

	cout <<setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);

	cout<<"Enter the name of the inventory file you want to write to: ";
	cin.getline(file_name, 81);
	fstream file(file_name);


	if(!file)

	{
		cerr<<endl;
		cerr<<"ERROR: File could not be opened." <<endl;
		cerr<< "Program Terminated" <<endl;
		exit(1);
	}

	cout<<endl;
	cout<<"Create an inventory record? (1 for Yes/ 0 for No): ";
	cin>>response;
	while (response)
	{
		cout<<endl;
		cout<<"ID Number:";
		cin>>id_no;
		cout<<endl;
		cout<<"Quantity On Hand: ";
		cin>>qoh;
		cout<<endl;
		cout<<"Price:";
		cin>> price;

		//Write inventory record
		file<<id_no<<" "<<qoh<<" "<<price<<endl;

		cout<<endl;
		cout<< "Create an inventory record? (1 for Yes/0 for No): ";
		cin>>response;
	}
	
	
	cout<<endl<<endl;
	cout<<"Inventory File "<<file_name<<" created. "<<endl<<endl;
	

	

	if (!file)
	{
		cerr<<endl;
		cerr<<"ERROR: File could not be opened." <<endl;
		cerr<< "Program Terminated"<<endl;
		exit(1);
	}


	cout<<"Inventory that is over 3,000.00"<<endl<<endl;


	while (!file.eof())
	{
		
		total = qoh*price;
			if (total > 3000)
			{
				cout<<setw(8) <<id_no<<setw(10)<<qoh<<setw(10)<<price<<setw(12)<<total<<endl;
			}
			
			//obtain the next file record
			file>>id_no>>qoh>>price;
	}
	
	file.close();
	
	cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}


This post has been edited by jimblumberg: 15 December 2012 - 07:16 AM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#20 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: What am I doing wrong?

Posted 15 December 2012 - 07:12 AM

Well! ofstream defaults to ios::out and ifstream defaults to ios::in... So we can write something like,
ofstream out("inventory.txt");



With fstream we need to specify that explicitly(ios::in and ios::out),
fstream file("inventory.txt", ios::in | ios::out);


Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: What am I doing wrong?

Posted 15 December 2012 - 07:26 AM

Well first you removed your priming read. In your original code you had a "read" operation before the loop, you still need that read because of the way your read loop is structured. But the best way of actually fixing this would be to use the read to control the loop.

while (file>>id_no>>qoh>>price)
{
// the rest of your logic, except the read operation.

The above will read your file stopping whenever it encounters an error.

Quote

With fstream we need to specify that explicitly(ios::in and ios::out),

No the fstream defaults to ios::in and ios::out. The only time you need to specify this is if the default open mode is not acceptable. The biggest difference between a fstream and ofstream is that the ofstream class truncates the file by default, the fstream doesn't. Also an ofstream will create a new file if the file doesn't exist by default the fstream will not create a new file unless you change the open mode parameters.


Jim
Was This Post Helpful? 0
  • +
  • -

#22 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: What am I doing wrong?

Posted 15 December 2012 - 08:03 AM

@jimblumberg, Well! there seems to be a conflict...

It Doesn't Defaults:
MSDN:

Quote

nMode

An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:

ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.

ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.

ios::in The file is opened for input. The original file (if it exists) will not be truncated.

ios::out The file is opened for output.

ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified.

ios::nocreate If the file does not already exist, the function fails.

ios::noreplace If the file already exists, the function fails.

ios::binary Opens the file in binary mode (the default is text mode).

Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.


Learncpp:

Quote

Note that ios::in and ios::out are already defaults for the ifstream and ofstream classes respectively. If you opt to use fstream (which can do both input and output), you explicitly have to pass in ios::in and/or ios::out depending on which mode you’d like to use.



It Defaults:
This says it defaults to in | out...

CPlusPlus too...

The std:

Quote

For the basic_fstream::open function the std is inconsistent. section 27.8.1.11 shows this decl
void open(const char* s, ios_base::openmode mode = ios_base::out);

section 27.8.1.13 shows this decl but without the default modes.
void open(const char* s, ios_base::openmode mode);


With this inconsistency, it would depend on the compiler...
Was This Post Helpful? 0
  • +
  • -

#23 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: What am I doing wrong?

Posted 15 December 2012 - 08:11 AM

Guys thanks for all the help. I finally got something that worked



//Lynette Wilkins
// Programming Problem 14.1 #1

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
	char file_name[81];

	int id_no;
	int qoh;
	double price;
	double total;
	int response;

	cout <<setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);

	cout<<"Enter the name of the inventory file you want to write to: ";
	cin.getline(file_name, 81);
	fstream file(file_name, ios::out);

	if(!file)

	{
		cerr<<endl;
		cerr<<"ERROR: File could not be opened." <<endl;
		cerr<< "Program Terminated" <<endl;
		exit(1);
	}

	cout<<endl;
	cout<<"Create an inventory record? (1 for Yes/ 0 for No): ";
	cin>>response;
	while (response)
	{
		cout<<endl;
		cout<<"ID Number:";
		cin>>id_no;
		cout<<endl;
		cout<<"Quantity On Hand: ";
		cin>>qoh;
		cout<<endl;
		cout<<"Price:";
		cin>> price;

		//Write inventory record
		file<<id_no<<" "<<qoh<<" "<<price<<endl;

		cout<<endl;
		cout<< "Create an inventory record? (1 for Yes/0 for No): ";
		cin>>response;
	}
	
	file.close();
	cout<<endl<<endl;
	cout<<"Inventory File "<<file_name<<" created. "<<endl<<endl;
	

	file.open(file_name, ios::in);

	if (!file)
	{
		cerr<<endl;
		cerr<<"ERROR: File could not be opened." <<endl;
		cerr<< "Program Terminated"<<endl;
		exit(1);
	}


	cout<<"Inventory that is over 3,000.00"<<endl<<endl;

	cout<<setw(8) <<"ID No." << setw(10) <<"Quantity"<<setw(10) << "Price"<< setw(12) <<"Total"
		<<endl<<endl;

	file >>id_no>>qoh>>price;
	while (!file.eof())
	{
		
		total = qoh*price;
			if (total > 3000.00)
			{
				cout<<setw(8) <<id_no<<setw(10)<<qoh<<setw(10)<<price<<setw(12)<<total<<endl;
			}
			
			//obtain the next file record
			file>>id_no>>qoh>>price;
	}
	
	file.close();
	
	cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}



Was This Post Helpful? 0
  • +
  • -

#24 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: What am I doing wrong?

Posted 15 December 2012 - 08:16 AM

Quote

With this inconsistency, it would depend on the compiler...

There is actually not inconsistency in the standard.

Well from the C++11 standard:

Quote

27.9.1.14
Class template basic_fstream
...
explicit basic_fstream(const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
void open(const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
void open(const string& s,
ios_base::openmode mode = ios_base::in|ios_base::out);


So as I said the fstream() defaults to in | out.



Jim

This post has been edited by jimblumberg: 15 December 2012 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2