still need help beginner function, program running in loop now

when i add the next function the previous results i get from the previous functions wont show anymore and it will only output the new functions results , how would i get it to output all the functions.

this is my result:

The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36

and my program :

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
// read a series of integers and determine
// the following information about each integer
// is it a multiple of 7,11,13
//is the sum of the digits odd or even
// what is the square root value (if positive)
// is it a prime number


#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<cmath>

#define in_file "data.txt"
#define out_file "result.txt"

using namespace std;

void multiples();
void oddoreven();
void squareroot();


int main ()

{

	multiples();

	oddoreven();

	squareroot();

} // end main 


void multiples() // function deinition 

{ 
	ifstream ins;
	ofstream outs;

	ins.open (in_file);
	outs.open(out_file);

	int number;

	while (!ins.eof())
	{

		ins >> number; 

		if (number % 7 == 0)

		outs << number << " is a multiple of 7" << endl;

		else if (number % 11 == 0)

			outs << number << " is a multiple of 11" << endl;

		else if (number % 13 == 0)

			outs << number << " is a multiple of 13" << endl;



	}

}
	void oddoreven () // function definition

	{ 

	ifstream ins;
	ofstream outs;

	ins.open (in_file);
	outs.open(out_file);

	int number;

		while (!ins.eof())
	 {

		ins >> number; 

		if ( number % 2 == 0 )

			outs << number << " is even " << endl;

		else if (number % 2 == 1 )

			outs << number << " is odd " << endl;

	 }

}

	void squareroot () // function definition 

	{ 

	ifstream ins;
	ofstream outs;

	ins.open (in_file);
	outs.open(out_file);

	double number;
	double result;

		while (!ins.eof())
	 {

		ins >> number; 

		if ( number > 0 )

		result = sqrt(number);
	
		outs << "The square root of " << number << " is " << result << endl;

	}

}
Last edited on
You are opening your file for overwriting in your functions. Use ios::app mode to apppend results.
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
im not sure where to put the append right now i have it like this but it is not working

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
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<cmath>

#define in_file "data.txt"
#define out_file "result.txt"

using namespace std;

void multiples();
void oddoreven();
void squareroot();


int main ()

{

	std::ofstream ofs ("result.txt", std::ofstream::app);

	multiples();

	oddoreven();

	squareroot();

} // end main 
You are opening your file for overwriting in your functions
And yet for some reason you added useless hidden parameter in main().You forgot where you opening file in your function? Hint: it is lines 45, 79 and 105
i got that to work now , but now my functions are repeating even though i have the while (!ins.eof()) , sorry im very new to programming , here is what im outputting now:

The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36
104 is a multiple of 13
3773 is a multiple of 7
13 is a multiple of 13
121 is a multiple of 11
77 is a multiple of 7
30751 is a multiple of 7
104 is even
3773 is odd
13 is odd
121 is odd
77 is odd
30751 is odd
The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36
104 is a multiple of 13
3773 is a multiple of 7
13 is a multiple of 13
121 is a multiple of 11
77 is a multiple of 7
30751 is a multiple of 7
104 is even
3773 is odd
13 is odd
121 is odd
77 is odd
30751 is odd
The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36
-858993460 is a multiple of 13
-858993460 is even
-858993460 is a multiple of 13
-858993460 is even
The square root of -9.25596e+061 is -9.25596e+061
-858993460 is a multiple of 13
-858993460 is even
The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36
104 is a multiple of 13
3773 is a multiple of 7
13 is a multiple of 13
121 is a multiple of 11
77 is a multiple of 7
30751 is a multiple of 7
104 is even
3773 is odd
13 is odd
121 is odd
77 is odd
30751 is odd
The square root of 104 is 10.198
The square root of 3773 is 61.4248
The square root of 13 is 3.60555
The square root of 121 is 11
The square root of 77 is 8.77496
The square root of 30751 is 175.36



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
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<cmath>

#define in_file "data.txt"
#define out_file "result.txt"

using namespace std;

void multiples();
void oddoreven();
void squareroot();



int main ()

{

	multiples();

	oddoreven();

	squareroot();

} // end main 


void multiples() // function deinition 

{ 

	ifstream ins;
	std::ofstream ofs ("result.txt", std::ofstream::app);

	ins.open (in_file);
	

	int number;

	while (!ins.eof())
	{

		ins >> number; 

		if (number % 7 == 0)

		ofs << number << " is a multiple of 7" << endl;

		else if (number % 11 == 0)

			ofs << number << " is a multiple of 11" << endl;

		else if (number % 13 == 0)

			ofs << number << " is a multiple of 13" << endl;



	}

}
	void oddoreven () // function definition

	{ 

	ifstream ins;
	std::ofstream ofs ("result.txt", std::ofstream::app);

	ins.open (in_file);
	

	int number;

		while (!ins.eof())
	 {

		ins >> number; 

		if ( number % 2 == 0 )

			ofs << number << " is even " << endl;

		else if (number % 2 == 1 )

			ofs << number << " is odd " << endl;

	 }

}

	void squareroot () // function definition 

	{ 

	ifstream ins;
	std::ofstream ofs ("result.txt", std::ofstream::app);

	ins.open (in_file);
	

	double number;
	double result;

		while (!ins.eof())
	 {

		ins >> number; 

		if ( number > 0 )

		result = sqrt(number);
	
		ofs << "The square root of " << number << " is " << result << endl;

	}

}
Last edited on
Each run you are appending to your file. Clear file manually and see that it actually runs once.

To counter this, do something like in the beginning of main():
1
2
std::ofstream out("result.txt", std::ios::trunc);
out.close();
Last edited on
thanks , that fixed the problem
Topic archived. No new replies allowed.