### 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 :

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````// 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 #include #include #include #include #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

 ``123456789101112131415161718192021222324252627282930`` ``````#include #include #include #include #include #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 ``````
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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120`` ``````#include #include #include #include #include #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():
 ``12`` ``````std::ofstream out("result.txt", std::ios::trunc); out.close();``````
Last edited on
thanks , that fixed the problem
Topic archived. No new replies allowed.