自己寫的一個練習程序

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章