My input text file contains this:

-4.1 -2.2 -0.5 1.2 4.6 5.1  2.1 0.2 -3.6 -4.1 0.2 0.5 2.2 4.1
-0.2 -1.2 -3.3 -4.6 -5.0 -2.2 -1.1 0.8 3.2 -0.1 -4.8

And I need to average and do some other functions with these numbers. However I can't get started, or even get my program to spew the numbers back to me.

Please help me get the ball rolling.

This is as far as I've gotten:

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
    //double rA, rQ, maxRuf;
    double surfDat =0;

    ifstream surfDat("suface.txt");

Thanks.

Recommended Answers

All 7 Replies

Wow! That's already quite an accomplishment!
What does ifstream do? What methods does it expose? (hint: look for get...)

ifstream is a command for inputting a file. I'm unfamiliar with get commands. For averaging I'm pretty sure that I need to make a loop that will count how many times it has added and then divide by that number. (?) I'm really unsure though.

My apologies for the misuse of my limited computer jargon. I hope that I didn't upset you.
I think that I have worked out nearly all the kinks. Thanks for the help.

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{

	double sum = 0;
	double n = 0;
	double min = -0.000001;
	double max = 0.000001;
	double rA, rQ, maxRuf, sDat;

	ifstream infile("surface.txt", ios::in);
	
	while ( infile >> sDat )
	{
		sum = sum + sDat;
		n = n + 1;

		if (sDat <= min){
			min = sDat;
		}
		else{
			min = min;
		}
		if (sDat >= max) {
			max = sDat;
		}
		else{
			max = max;
		}
		infile >> sDat;
	}

	rA = sum / n;
	rQ = sqrt((sum / n));
	maxRuf = max - min;

	cout << "Arithmetic Mean = " << rA << endl;
	cout << "Root-mean-square Average = " << rQ << endl;
	cout << "Maximum Roughness Height = " << maxRuf << endl;

	return 0;
}

}
infile >> sDat;
}

Drop this call to >>, only do it in the conditon of the while(). Otherwise only every other piece of data in the file is going to be used in the running totals and calculations.

Not every if has to be followed by an else. Assigning min to min and max to max is meaningless, so don't do it. You'll just wear out your keyboard faster by typing unnecessary strokes.

As time goes by you probably should get into the habit of checking your stream status. That is make sure the ifstream opened the file befor trying to use it, and once the while loop is done find out why it stopped by determining whether it reached the end of file or whether it stopped for some other, unexpected reason, indicating a probable corrupt file, but who knows. Really down the road you may even want to do data validation, unless you're sure the input is valid.

For now, though, not too bad.

However, message boards like to muck up your indentation, etc which makes it hard for those looking at your posts to read your code. So if you want to post code here in the future, please take a few minutes to learn how to enclose the code in code tags that will tell the message board to keep the nice, useful, and appropriate indenting I'm sure (yes?) you use. The watermarks in the Message box where you enter your post and the announcements at the top of the board, etc, tell how to use the code tags.

Thanks lerner. I'll fix my formatting next time I put something up.

I still have one small problem with my square-root function. Its outputting nonsense to me. Can anyone see whats wrong with it?

nevermind that last one, it was failing because it was trying to take the root of a negative number. thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.