output to files


Hello everyone.I have a program witch lets you in put some data in it.My task is to have this data output to a file,the problem is i am not sure if this is possible with this code ,and if its possible where should I put the file open and close part.Any advice is appreciate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  #include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;

class store {
public:
    virtual double calcTax() = 0;
    virtual void printInfo() = 0;
protected:
    string ID;
	string model;
	string articul;


};


class Buy : public store {
public:
    Buy(string s,string j,double d) {
        ID = s;
        model= j;
		zena=d;
    }
    virtual double calcTax() {
        return zena;
    }
    virtual void printInfo() {
		cout << "Ime " << ID  <<"Model"<<model<<"zena"<<zena << endl;
    }
private:
    double zena;
	string model;
};

class Izplashtane : public store  {
public:
	Izplashtane(string s,string n, string m, int a, int c) {
        ID = s;
		articul=n;
        model = m;
        zena = a;
		vnostki = c;

    }
    virtual double calcTax() {
        double vn= 0;
		vn=zena/vnostki;
        return  vn;
    }
    virtual void printInfo() {
		cout<<endl;
		cout << "Ime " << ID<<endl;
		cout<<"Articul"<<articul<<endl;
		cout<< "Model "<<model<<endl;
		cout<< "zena"<<zena<<endl;
		cout<<"vnoski"<<vnostki<<endl;
		cout<<"Mesecna vnoska"<<zena/vnostki<<endl;
		
    }
private:
    string model;
    int zena;
    int vnostki;


};

class queueTax {
public:
    void add(store*p);
    void process();
    void list();
private:
    vector<store * >v;

};

void queueTax::add(store*p) {
    fstream f2;
    f2.open("d:\\data\\broi123.txt", ios::out);
    
	v.push_back(p);


    f2.close();
	
}

void queueTax::process() {
    if (v.size() == 0) return;
    
   
	v[0]->printInfo();
   
    delete v[0];
    v.erase(v.begin());
}

void queueTax::list() {
    for (int i = 0; i < v.size(); i++) v[i]->printInfo();
    cout << "====\nTOTAL: " << v.size() << endl;
}


int main()
{
    queueTax q;

    double h; 
	string s,n,m;
    string ch = "9";
    int  a, c;
	fstream f2;
    f2.open("d:\\data\\broi.txt", ios::out);
    


    
    while (ch != "0") {
        cout << "\n\n\tMAIN MENU\n\n" << "(1)Pokupka\n(2)Na izplashtane\n"
            << "(3)PROCESS\n(4)LIST\n" << "-------\n(0)EXIT\n\n>";
       
			
            getline(cin, ch);
			
			
        if (ch == "0") 
		{cout<<"nqma da izlizash";
		return 0;}
       
        
        else if (ch == "1") {
            cout << "Ime: ";
            getline(cin, s);
            cout << "model: ";
           getline(cin,m);
			cout<<"zena";
		    cin>>h;
          
            q.add(new Buy(s,m,h));
        }
        else if (ch == "2") {
            cout << "Ime: ";
            getline(cin, s);
			cout<<"Articul";
			getline(cin,n);
            cout << "model: ";
            getline(cin,m);
			try{
			cout << "zena: ";
					cin >> a;
					cout << "vnoski: ";
					cin >> c;
			if(!cin.good())
				throw 0;
				
			}catch(...)
			{
				cout<<"Trqbva da se vuvedat chisla";
				break;
				
			};
			
			
			

			q.add(new Izplashtane(s,n, m, a, c));


        }
        else if (ch == "3") q.process();
        else if (ch == "4") q.list();
		f2.close();
    }
	
}
Example code for declaring a file and writing to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
// Create a file.
ofstream theFile;
// Open the file.
theFile.open("thisFile.txt"); // Makes this a text file.
// Write a string to the file.
theFile << "Hi user!";
// Close the file
theFile.close();

return 0;
}
Last edited on
Topic archived. No new replies allowed.