nothing being outputted to the output file? (ifstream and ofstream)

I am new to programming and these type of forums so please forgive me if the question asked sounds weird but I am getting the two files that are needed which are "output.txt" and "output_sorted.txt" but when I check them, its just blank.

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
 #include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;


double calcDistance(double  x1, double  y1, double  z1, double  x2, double  y2, double  z2) {
	double sqr =((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
	double dist = (sqrt(sqr));
	 return dist;
}


int main() {

	double x1, x2, y1, y2, z1, z2;
	std::vector<double> dis;
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	double distance;

	while (fin.good()) {
		fin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
		distance = calcDistance(x1, y1, z1, x2, y2, z2);
		dis.push_back(distance);
		fout << x1 << " " << y1 << " " << z1 << " " << x2 << " " << y2 << " " << z2 << " distance = " << distance << endl;
	}

	sort(dis.begin(), dis.end());
	ofstream fout2("output_sorted.txt");
	for (vector<double>::iterator itr = dis.begin(); itr != dis.end(); ++itr)
    fout2 << *itr <<endl;
return 0;

}
	
Last edited on
change line 34 to cout, do you get anything?
same up above, print what should go into the file, make sure its printing something.
Also, learn how to use a debugger, keep an eye on the values of the variables as you step through the code 1 line at a time. See if the vector is populated.

Just be careful with line 26, there is an existing std::distance , it may not have an effect here (I haven't tested it), I like to avoid anything that is already in std:: To fix this get rid of line 6 and put std:: before each std thing (sqrt, ifstream, ofstream, sort)

Ideally one should put their own code into it's own namespace, that is what they are for.

https://en.cppreference.com/w/cpp/language/namespace
https://en.cppreference.com/w/cpp/language/namespace_alias

It's usually a good idea to test if a file was opened, regardless.

There is a std::hypot which you could use instead of your calcDistance function if you wish.
https://en.cppreference.com/w/cpp/numeric/math/hypot

Instead of iterator in line 33, one could do this (range based for loop):

1
2
3
for (const item& : dis) {
     fout2 << item <<endl;
}
Either use a range-based for loop, as suggested by TheIdeasMan, or if you want to use iterators in a for loop consider using auto to let the compiler properly type the loop variable, and use the vector's constant interators for the beginning and end when not modifying the contents:

for (auto itr = dis.cbegin(); itr != dis.cend(); ++itr)

(C++11 needed for the const iterators)

Having using namespace std; is going to bite you in the butt sooner or later, it bypasses the reasons why namespaces were introduced, and can be seen as being lazy.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Last edited on
Another take on why using namespace std; shouldn't be used:

https://isocpp.org/wiki/faq/coding-standards#using-namespace-std

The key take-away:

The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldn’t use it on a regular basis, at least not in your new C++ code.
ISOCPP FAQ wrote:
But either way is fine. Just remember that you are part of a team, so make sure you use an approach that is consistent with the rest of your organization.

https://isocpp.org/wiki/faq/coding-standards#using-namespace-std

Hmm. What is the benefit of consistency in this case?
For those that don't like typing std:: everywhere, consider setting up code snippets in your favorite IDE.

I have them so it will write the whole container or algorithm with const as well. The mnemonics is up to the user. I have csstr meaning const std::string and csvec meaning const std::vector<> The 's' means std, I could 'b' there to mean something from boost.

As for using std::cout; it wasn't long before that gave me the shits as well, it's a pain to write lots of those types of statements in the file.
Last edited on
I just have a keybinding where Alt-M directly inserts the text std::
L24/25 is usually written as :

 
while (fin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2) {


Also it's good practice to check that files have been opened ok before reading/writing them.

After L21 something like:

1
2
if (!fin || !fout)
    return (std::cout << "Cannot open files\n"), 1;

Topic archived. No new replies allowed.