您的位置:首页 > 编程语言 > PHP开发

自己写的一个练习程序

2012-08-29 18:06 267 查看
/*
base.h
*/
#ifndef BASE_H
#define BASE_H
#include <string>
#include <iostream>
using namespace std;

class Base{
public:
Base(){}
Base(string x_name, double x_price,
int x_num):name(x_name), price(x_price), num(x_num){}
void setName(string name){this->name = name;}

string name;
double price;
int num;
};
#endif

#include <vector>
#include <fstream>
#include "base.h"
const int NUM = 2;
int main()
{
ofstream output;
output.open("wei.txt");

string in_name;
double in_price;
int in_num;
vector<Base> iter;
for (int i = 0; i != NUM; ++i)
{
cout << "Please enter name of book: ";
cin >> in_name;
cout << "Please enter price of book: ";
cin >> in_price;
cout << "Please enter number of book: ";
cin >> in_num;
Base book(in_name, in_price, in_num);
iter.push_back(book);
}
/*	for (int j = 0; j != NUM; ++j)
{
cout << iter[j].name << endl;
cout << iter[j].price << endl;
cout << iter[j].num << endl;
}
*/

for (int j = 0; j != NUM; ++j)
{
output << iter[j].name << " " << iter[j].price << " "
<< iter[j].num << endl;
}
output.close();

ifstream input;
input.open("wei.txt");
for (int k = 0; k != NUM; ++k)
{
input >> iter[k].name >> iter[k].price >> iter[k].num;
cout << iter[k].name << " " << iter[k].price << " " << iter[k].num << endl;
}

input.close();
cout << "DONE!" << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string output input
相关文章推荐