What am I doing wrong?

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 llwilkins  Icon User is offline

  • D.I.C Head

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

What am I doing wrong?

Posted 11 December 2012 - 12:04 PM

I put a system pause in the program so that I can see it and it still is disappearing when I enter 0.
//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;
	}
	
	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);
	}



	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;
			}

			
	}
	file.close();
	cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: What am I doing wrong?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: What am I doing wrong?

Posted 11 December 2012 - 12:07 PM

Quote

I put a system pause in the program so that I can see it and it still is disappearing when I enter 0.

The system("Pause") only executes if execution actually gets to that point. Do you see any other place where you might be ending the program before the system("pause") ?
Was This Post Helpful? 0
  • +
  • -

#3 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 11 December 2012 - 12:25 PM

View Postjjl, on 11 December 2012 - 12:07 PM, said:

Quote

I put a system pause in the program so that I can see it and it still is disappearing when I enter 0.

The system("Pause") only executes if execution actually gets to that point. Do you see any other place where you might be ending the program before the system("pause") ?


yes the cerr point. But if this is the case I am not seeing where I am doing anything wrong. This is my first week at files so I am very very new to it . But I am practicing with the file Input/Output and the only thing in our text that it is showing us any example of is just one buffer not what to do when you have more than one variable like i have.

View Postllwilkins, on 11 December 2012 - 12:23 PM, said:

View Postjjl, on 11 December 2012 - 12:07 PM, said:

Quote

I put a system pause in the program so that I can see it and it still is disappearing when I enter 0.

The system("Pause") only executes if execution actually gets to that point. Do you see any other place where you might be ending the program before the system("pause") ?


yes the cerr point. But if this is the case I am not seeing where I am doing anything wrong. This is my first week at files so I am very very new to it . But I am practicing with the file Input/Output and the only thing in our text that it is showing us any example of is just one buffer not what to do when you have more than one variable like i have.


so this means my error is somewhere between here!


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

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

	

	while (!file.eof())
	{
		file>>id_no>>qoh>>price;

		total = qoh*price;
			if (total > 3000.00)
			{
				cout<<setw(8) <<id_no<<setw(10)<<qoh<<setw(10)<<price<<setw(12)<<total<<endl;
			}

			
	}
	file.close();
	cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 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 11 December 2012 - 12:45 PM

If your program prints these error messages then its clear that you are calling exit() and it leaves the program from this point without executing the code any further...
    if (!file)
    {
        cerr<<endl;
        cerr<<"ERROR: File could not be opened." <<endl;
        cerr<< "Program Terminated"<<endl;
        exit(1);
    }



Well! You might call for PAUSE before exit():
system("pause");
exit(1);



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

#5 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 11 December 2012 - 12:50 PM

Ok. I did do the system pause before and it is telling me it could not call the file. Why is this? The file was created because when I looked into the project I saw where it created it and it was created correctly because i did it in .txt file so that I could see it.

Here is the output to the file I created

10 100 10
20 200 20
30 300 30


The only other thing that I can see not knowing the file thing to a tee is this

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

that is right above the cerr. Am I calling the file right here? Because its obvious that if the file is failing to open that it is not been called right.
Was This Post Helpful? 0
  • +
  • -

#6 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 11 December 2012 - 01:09 PM

Why reopening it? Just do it at once,
fstream file(file_name, ios::in | ios::out);


This would work with istream objects but I think it might not work with fstream object:
13 while (!file.eof())

I think this works! That would be while(file) which might not work with fstream...

This post has been edited by AKMafia001: 11 December 2012 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 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 11 December 2012 - 01:40 PM

We have learned that part yet just the part
file.open(file_name, ios::in);

Looks like something is going right it is doing what it suppose to the only problem now is that when it displays the record that is over 3,000.00 it won't stop it keeps repeating it.


//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);
	ofstream 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;
	}
	
	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);
	}

	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;
			}
			
	}
	file.close();
	
	cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: What am I doing wrong?

Posted 11 December 2012 - 02:00 PM

In the following snippet, how do you expect to exit this loop?
	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;
			}
			
	}


Since you aren't doing anything with the file inside this loop it should never reach eof().

Jim
Was This Post Helpful? 0
  • +
  • -

#9 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 11 December 2012 - 02:01 PM

Quote

it won't stop it keeps repeating it.

Because the file pointer is just in its place and is not moving, hence not reaching EOF... That's because you didn't read anything from the file in the loop...

And why even you would use a loop here,
77	    while (!file.eof())
78	    {
79	         
80	         
81	        total = qoh*price;
82	            if (total > 3000.00)
83	            {
84	                cout<<setw(8) <<id_no<<setw(10)<<qoh<<setw(10)<<price<<setw(12)<<total<<endl;
85	            }



You are doing the same stuff again and again, calculate total, check total, do it again(with the same values)...
Was This Post Helpful? 0
  • +
  • -

#10 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 11 December 2012 - 02:44 PM

That was the problem. I was looking at an example and thought we had to do this for this weeks program. I fixed it and it works perfectly. Thank you so much guys!!!!


//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);
	ofstream 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;
	}
	
	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);
	}

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

system("PAUSE");

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 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:29 AM

For some reason this program is compiling but it is going through a never ending loop when it is doing the search for the total greater than 3,000.00. I was told before to take out the while loop by a person up here and when I did this, it only displayed the last total that was greater than 3,000.00 but not all of them. I took what I had to my instructor and he said I was missing a while loop. So its obvious it is needed. The problem is when it is put back in it still just keeps going and going. Any assistance will help!!!

//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);
	ofstream 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;
	}
	
	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;


	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
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: What am I doing wrong?

Posted 15 December 2012 - 06:37 AM

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


Jim
Was This Post Helpful? 0
  • +
  • -

#13 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:40 AM

This is insertion operator <<, then how come you say obtain a new record from file... It just writes to the file...
090	            //obtain the next file record
091	            file<<id_no<<qoh<<price;



If you want to read from the file, use the extraction operator >>...

Read post #9...
Was This Post Helpful? 0
  • +
  • -

#14 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:43 AM

View PostAKMafia001, on 15 December 2012 - 06:40 AM, said:

This is insertion operator <<, then how come you say obtain a new record from file... It just writes to the file...
090	            //obtain the next file record
091	            file<<id_no<<qoh<<price;



If you want to read from the file, use the extraction operator >>...

Read post #9...


when I use the extraction operator >> the compiler gives me this error
:\users\student\documents\visual studio 2010\projects\week 9 rough\week 9 rough\main.cpp(92): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ofstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\iomanip(243) : see declaration of 'std::operator >>'"
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: What am I doing wrong?

Posted 15 December 2012 - 06:44 AM

	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

This post has been edited by jimblumberg: 15 December 2012 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2