//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;
}
23 Replies - 683 Views - Last Post: 15 December 2012 - 08:16 AM
#1
What am I doing wrong?
Posted 11 December 2012 - 12:04 PM
Replies To: What am I doing wrong?
#2
Re: What am I doing wrong?
Posted 11 December 2012 - 12:07 PM
Quote
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") ?
#3
Re: What am I doing wrong?
Posted 11 December 2012 - 12:25 PM
jjl, on 11 December 2012 - 12:07 PM, said:
Quote
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.
llwilkins, on 11 December 2012 - 12:23 PM, said:
jjl, on 11 December 2012 - 12:07 PM, said:
Quote
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;
}
#4
Re: What am I doing wrong?
Posted 11 December 2012 - 12:45 PM
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!
#5
Re: What am I doing wrong?
Posted 11 December 2012 - 12:50 PM
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.
#6
Re: What am I doing wrong?
Posted 11 December 2012 - 01:09 PM
fstream file(file_name, ios::in | ios::out);
This would work with istream objects but I think it might not work with fstream object:
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
#7
Re: What am I doing wrong?
Posted 11 December 2012 - 01:40 PM
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;
}
#8
Re: What am I doing wrong?
Posted 11 December 2012 - 02:00 PM
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
#9
Re: What am I doing wrong?
Posted 11 December 2012 - 02:01 PM
Quote
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)...
#10
Re: What am I doing wrong?
Posted 11 December 2012 - 02:44 PM
//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;
}
#11
Re: What am I doing wrong?
Posted 15 December 2012 - 06:29 AM
//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;
}
#12
Re: What am I doing wrong?
Posted 15 December 2012 - 06:37 AM
Jim
#13
Re: What am I doing wrong?
Posted 15 December 2012 - 06:40 AM
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...
#14
Re: What am I doing wrong?
Posted 15 December 2012 - 06:43 AM
AKMafia001, on 15 December 2012 - 06:40 AM, said:
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 >>'"
#15
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
|
|

New Topic/Question
Reply



MultiQuote



|