重写书店处理程序
运行报错,在Sales_data类里,[Error] ‘istream’ does not name a type + [Error] ‘ostream’ does not name a type
#include "Sales_data.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) //不会用
{
ifstream input(argv[1]); //打开销售记录文件
ofstream output(argv[2]);
Sales_data total; //保存销售总额的变量
if (read (input, total))
{
Sales_data trans;
while(read(input, trans))
{
if(trans.isbn() == total.isbn())
total.combine(trans);
else
{
print(output, total)<<endl;
total = trans;
}
}
print(output, total)<<endl;
}
else
cerr<< "错误"<<endl;
return 0;
}
标准答案(一样的错误)
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 2)
{
cerr << "请给出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while(read(in, trans))
{
if (total.isbn() == trans.isbn() )
total.combine(trans);
else
{
print(cout, total) << endl;
total = trans;
}
}
print ( cout, total) <<endl;
}
else
cerr << "没有数据"<< endl;
return 0;
}
8.7
自己的
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 2)
{
cerr << "请给出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
ofstream out()argv[2]; //修改的地方
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while(read(in, trans))
{
if (total.isbn() == trans.isbn() )
total.combine(trans);
else
{
print(out, total) << endl; //修改的地方
total = trans;
}
}
print ( out, total) <<endl; //修改的地方
}
else
cerr << "没有数据"<< endl;
return 0;
}
答案
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 3) //修改的地方
{
cerr << "请给出输入、输出文件名" << endl; //修改的地方
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
ofstream out(argv[2]);
if(!out) //修改的地方
{
cerr<< "无法打开输出文件“ << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while(read(in, trans))
{
if (total.isbn() == trans.isbn() )
total.combine(trans);
else
{
print(out, total) << endl; //修改的地方
total = trans;
}
}
print (out, total) <<endl; //修改的地方
}
else
cerr << "没有数据"<< endl;
return 0;
}
8.8
自己的
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 3)
{
cerr << "请给出输入、输出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
ofstream app(argv[2]);
if(!app) //修改的地方
{
cerr<< "无法打开输出文件“ << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while(read(in, trans))
{
if (total.isbn() == trans.isbn() )
total.combine(trans);
else
{
print(app, total) << endl; //修改的地方
total = trans;
}
}
print (app, total) <<endl; //修改的地方
}
else
cerr << "没有数据"<< endl;
return 0;
}
答案
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 3)
{
cerr << "请给出输入、输出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
ofstream out(argv[2], ofstream::app);//修改的地方
if(!out)
{
cerr<< "无法打开输出文件“ << endl;
return -1;
}
Sales_data total;
if (read(in, total))
{
Sales_data trans;
while(read(in, trans))
{
if (total.isbn() == trans.isbn() )
total.combine(trans);
else
{
print(out, total) << endl;
total = trans;
}
}
print (out, total) <<endl;
}
else
cerr << "没有数据"<< endl;
return 0;
}
8.9
自己的(该怎么体现打印的 是istringstream对象呢?)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
//函数编写
istream & f(istream &in)
{
string v;
while (in >> v, !in.eof())
{
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail())
{
cerr << "数据错误" << endl;
in.clear():
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
cout << "请输入一些string,按Ctrl+Z结束" << endl;
f(cin);
return 0;
}
答案
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
using namespace std;
istream& f(istream &in)
{
string v;
while (in >> v, !in.eof())
{
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail())
{
cerr << "数据错误" << endl;
in.clear();
in.ignore(100,'\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
ostringstream msg;
msg << "C++ Primer 第五版" << endl;
istringstream in(msg.str());
f(in);
return 0;
}
8.10
自己的
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
int main()
{
//打开文件
ifstream in("文件名.in");
if(!in)
{
cerr<< "无法打开文件" << endl;
return -1;
}
//把文件中的行保存到vector<string>中
string line;
vector<string> vs;
while(getline(in,line))
vs.push_back(line);
//输入完毕,关闭文件
in.close();
//使用isstringstream从vector中读取单词,每次读一个(是否需要迭代器?)
ostringstream msg;
msg <<vs;
istringstream in(msg.str());
//不会了
答案(上面代码“使用isstringstream从vector中读取单词,每次读一个(是否需要迭代器?)”之后开始,之前的都一样)
vector<string>::const_iterator it = words.begin();
while(it != words.end())
{
istringstream line_str(*it);
string word;
while(line_str>>word)
cout << word <<" ";
cout <<endl;
++it;
}
return 0;
}
8.11
答案
分析record的作用后,代码改为
string line, word;
vector<PersonInfo> people;
istringstream record;
while ( getline (cin, line))
{
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while ( record >> word)
info.phones.push_back(word);
people.push_back(info);
}
8.12
由于每个人的电话号码数量不固定,因此更好的方式不是通过类内初始化指定人名和所有电话号码,而是在缺省初始化之后,在程序中设置哎人名并逐个添加电话号码。(什么是缺省初始化?默认初始化吗)