Errors in Program

I'm having a lot of trouble with this program I end up with a variety of errors and i'm not sure how to fix it

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
#include <fstream>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(void)
{
	double epsilon = .00000001, a, b, i, x0, x1;
	char zzz;
	const char filepath[] = "C:\\CoutputFiles\\File3.txt";
	const char ErrorMessage[] = "Can't open file ";
	fstream OutStream(filepath, ios::out);
	if (OutStream.fail())
	{
		cerr << ErrorMessage << filepath;
		cin >> zzz;
		exit(-1);
	}

	cout << "Program will approximate the square root of a number + 40\n\n";
	cout << "Type in the last 2 digits of your University X number:\n";
	cin >> a;
	b = a + 40;
	cout << "\nYou entered: " << a;
	OutStream << "You entered: " << a << "\n";
	cout << "\nFinding the square root of " << b;
	cout << "\n\nList of steps with approximations: ";
	if (a < 0.0)
	
	{
		cout << "Cannot find square root of a negative number...";
	}
	else (int i = 1);
	x0 = b;
	x1 = (x0 + b / x0) / 2;
	while (fabs(x1 - x0)) > epsilon)

	{
	(x0 = x1;);
	x1 = (x0 + b / x0) / 2;
	cout << "\nStep " << i << ": " << x1;
	OutStream << i << ", " << x1 << ", ";
	i++;
	}

	cout << "\nThe square root of " << b << "is " << x1;
	OutStream << "\nThe square root of " << b << "is " << x1;
	OutStream.close();
	cout << "\nPress any character then 'Enter'...";
	cin >> zzz;
	return 0;
}







You had some ; line 42 for instance. You also have an int variable i which you enclosed in () braces instead of { } ones (line 36), which gives a warning - unused variable i. You already have a variable, use it. ;-)

Anyway, here is the code, it compiles:

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
#include <fstream>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(void)
{
	double epsilon = .00000001, a, b, i, x0, x1;
	char zzz;
	const char filepath[] = "C:\\CoutputFiles\\File3.txt";
	const char ErrorMessage[] = "Can't open file ";
	fstream OutStream(filepath, ios::out);
	if (OutStream.fail())
	{
		cerr << ErrorMessage << filepath;
		cin >> zzz;
		exit(-1);
	}

	cout << "Program will approximate the square root of a number + 40\n\n";
	cout << "Type in the last 2 digits of your University X number:\n";
	cin >> a;
	b = a + 40;
	cout << "\nYou entered: " << a;
	OutStream << "You entered: " << a << "\n";
	cout << "\nFinding the square root of " << b;
	cout << "\n\nList of steps with approximations: ";
	if (a < 0.0)
	
	{
		cout << "Cannot find square root of a negative number...";
	}
	else {
	    i = 1;
	    }
	x0 = b;
	x1 = (x0 + b / x0) / 2;
	while ((fabs(x1 - x0) > epsilon))
	{
	x0 = x1;
	x1 = (x0 + b / x0) / 2;
	cout << "\nStep " << i << ": " << x1;
	OutStream << i << ", " << x1 << ", ";
	i++;
	}

	cout << "\nThe square root of " << b << "is " << x1;
	OutStream << "\nThe square root of " << b << "is " << x1;
	OutStream.close();
	cout << "\nPress any character then 'Enter'...";
	cin >> zzz;
	return 0;
}
Line 36: What are you trying to do here? You don't want to define a variable (i) in an else clause. What happens when you come through the other path? Hint: i (defined at line 11) is an uninitialized variable. Besides, the () around the statement is not valid syntax.

Line 34: If a is less than 0, you output a message, but then continue as if nothing is wrong.

Line 39: unmatched parenthesis.

Line 42: What's with the parens?






Last edited on
Got it, thanks for the help guys, I appreciate it.
Topic archived. No new replies allowed.