File input.txt

Write a function to prepare a text file input.txt by storing 100 random integers in the range from -50 to 50, one per line.
The file is returned as a result. Write a function inputfile () that takes a file as an argument and returns a sequential container filled with numbers from the file. Write a modify () function that receives the result container of the inputfile () function as an argument. The modified container is returned as a result. Add container-result of calculation of sum and average
arithmetic in absolute value. Use a vector, deque, and list as the container.

I have problems for 38, 56, 73, 116 strings of code (I maked this like comment)
This code make random integers but it does not make modify() and Add(). So on console I have: File is done! and Maiking of container... What Do I need to do?

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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<ctime>
#include<vector>
#include<deque>
#include<list>
#include<algorithm>
#include<cstdlib>
#include <iterator>
#include <cmath>
#include <stdio.h>
#pragma warning(suppress : 4996)

using namespace std;

#include <stdio.h>

FILE* File(int N, int count) {

    srand(time(NULL));
    FILE* file;
    fopen_s(&file, "input.txt", "w");
    fstream in(file);
  

        for (int i = 1; i <= count; ++i) {
            in << rand() % (2 * N + 1) - N << endl;
        }

        cout << "File is done!" << endl;

        return file;
}

vector<int> inputfile_to_vector(FILE *File){
   // ifstream in(File);
   fstream in("input.txt", fstream::in | fstream::out);
    
    in.seekg(0);
    int x;
    vector<int> vector;
    in >> x;
   
     while (!in.eof()) {
           vector.push_back(x);
           in >> x;
     }
    
     return vector;
}

deque<int> inputfile_to_deque(FILE *File){
    ifstream in(File);
   // fstream in("input.txt", fstream::in | fstream::out);
    
     in.seekg(0);
     int x;
     deque<int> deque;
     in >> x;
   
     while (!in.eof()){
           deque.push_back(x);
           in >> x;
     }
     
     return deque;
}

list<int> inputfile_to_list(FILE *File){
    ifstream in(File);
   // fstream in("input.txt", fstream::in | fstream::out);
    
     in.seekg(0);
     int x;
     list<int> list;
     in >> x;
    
    while (!in.eof()){
          list.push_back(x);
          in >> x;
    }
    
    return list;
}

template <class T>
T& modify(T& x){

    int firstnechot = 0;

    for (const auto& i : x){
        if (std::abs(i) % 2 == 1){
            firstnechot = std::abs(i);
            break;
        }
    }
    for (auto& i : x) { i += firstnechot; }
   
    return x;
}

template <class T>
    inline void Add(T& conteiner){
    typename T::const_iterator pos;
    int S = 0, SA = 0, N = 0;

    for (pos = conteiner.begin(); pos != conteiner.end(); ++pos) {
        S += *pos;
        SA += abs(*pos);
        N++;
    }
   
    conteiner.push_back(S);
    //conteiner.push_back(SA / N);
    
    cout << "Sum and average were add to container" << endl;
}

int Random(){ return rand() % (2 * 50 + 1) - 50; }  


int main(){

setlocale(0, "");

    FILE *F = File(50, 5);

    cout << "Maiking of container..." << endl;
    vector<int> V = inputfile_to_vector(F);
    deque<int> D = inputfile_to_deque(F);
    list<int> L = inputfile_to_list(F);

    cout << "Changing of container..." << endl;
    modify(V);
    Add(D);
  
    system("pause");
}




You're using a queer mixture of c_style and C++ file steams. Is there any reason you're using c-style file functions etc (FILE, fopen_s() etc) ?

I've sorted out your file functions so that it will compile using C++ file streams.

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
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <iterator>
#include <cmath>

using namespace std;

void File(int N, int count)
{
	ofstream out("input.txt");

	for (int i = 1; i <= count; ++i) {
		out << rand() % (2 * N + 1) - N << endl;
	}

	cout << "File is done!" << endl;
}

vector<int> inputfile_to_vector(/*FILE* File*/) {
	vector<int> vec;
	fstream in("input.txt", fstream::in | fstream::out);

	if (!in.is_open())
		cout << "Cannot open file\n";
	else
		for (int x {}; in >> x; )
			vec.push_back(x);

	return vec;
}

deque<int> inputfile_to_deque(/*FILE* File*/) {
	deque<int> deq;
	fstream in("input.txt", fstream::in | fstream::out);

	if (!in.is_open())
		cout << "Cannot open file\n";
	else
		for (int x {}; in >> x; )
			deq.push_back(x);

	return deq;
}

list<int> inputfile_to_list(/*FILE* File*/) {
	list<int> list;
	fstream in("input.txt", fstream::in | fstream::out);

	if (!in.is_open())
		cout << "Cannot open file\n";
	else
		for (int x; in >> x; )
			list.push_back(x);

	return list;
}

template <class T>
T& modify(T& x) {

	int firstnechot = 0;

	for (const auto& i : x) {
		if (std::abs(i) % 2 == 1) {
			firstnechot = std::abs(i);
			break;
		}
	}
	for (auto& i : x) { i += firstnechot; }

	return x;
}

template <class T>
inline void Add(T& conteiner) {
	typename T::const_iterator pos;
	int S = 0, SA = 0, N = 0;

	for (pos = conteiner.begin(); pos != conteiner.end(); ++pos) {
		S += *pos;
		SA += abs(*pos);
		N++;
	}

	conteiner.push_back(S);

	cout << "Sum and average were add to container" << endl;
}

int Random() { return rand() % (2 * 50 + 1) - 50; }


int main()
{
	srand(time(NULL));
	setlocale(0, "");

	File(50, 5);

	cout << "Making of container..." << endl;

	vector<int> V = inputfile_to_vector();
	deque<int> D = inputfile_to_deque();
	list<int> L = inputfile_to_list();

	cout << "Changing of container..." << endl;

	modify(V);
	Add(D);
}

Last edited on
Thank you.
But what do I need to do with
conteiner.push_back(S);

As I understend It should be in the end of container but in input.txt only sequence is. Or I mistaken?
For Add(D), D is of type deque<int> which as been initialised from the contents of the file. So conteiner.push_back(S) will push the value of S onto the deque. You are passing by ref, so after Add(D) in main(), D will reflect the new value that was pushed. If you display D before and after Add(D), then you should see the extra element.
Thank you very much!
If you display D before and after Add(D), then you should see the extra element.


It seems to me that I misunderstood you...
How can I realize it?
Realize what?

You haven't a function to display the contents of the containers. You need something like (not tried):

1
2
3
4
5
6
7
template <class T>
void display(const T& x) const {
    for (const auto& i : x)
        std::cout << i << "  ";

    std::cout << '\n';
}


Then you can have:

1
2
3
display(D);
Add(D);
display(D);


which will display the contents of the container D, add elements to D and then display D again.

Last edited on
which will display the contents of the container D, add elements to D and then display D again.

Thanks! It works
Topic archived. No new replies allowed.