variables question

I've got a question regarding variables that i can not work out...
If I comment out the global double input = 0;
and uncomment the local double input = 0; in the fuction inputnumber() the code complies and runs but not with expected results.
If I enter zer0 or less it keeps asking for input even if i put a positive number in.
However if I use the global variable and comment out the local variable in the function it works as expected and does not enter an infinite loop.

To be honest I don't know why? Can anyone explain this to me please!!

Regards

James

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
  #include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include<sstream>

using namespace std;

double sqroot (double number,long double previteration);//sqaure root function declarartion
double inputnumber (); //input number function declaration


double roottofind = 0;
double input = 0;


int main()
{
	roottofind = inputnumber(); //call input function and assign value
	double firstiteration = roottofind/2;// assign value to first iteration
	//call sqroot function and output result
	cout << "Squareroot is: " << setprecision(15) << sqroot(roottofind,firstiteration);
	cout << endl;
	
	return 0;

}

double sqroot (double number,long double previteration)
{	
	long double root = (0.5 * (previteration + (number/previteration)));// iteration	
	if (abs ((root*root) - number) > 0.0000000000000001) //exit condition
		return sqroot(number,root);
	else
		return root;
}

double inputnumber ()
{
//	double input = 0;
	string mystr;
	cout << "Input a positive number to find it's square root: ";
	getline (cin,mystr);
	stringstream(mystr) >> input;
	
	while ( input <= 0 )
		inputnumber();


	return input;
}
In line 47 your not returning anything from inputnumber (), so input remains the same for each iteration.

I think inputnumber could be simplified like this too.

1
2
3
4
5
6
7
8
9
10
11
12
double inputnumber ()
{
	double input = 0;
	cout << "Input a positive number to find it's square root: " << endl;

	do {
		cout << "\t--[ ";
		cin >> input;
	} while ( input <= 0 );
	
	return input;
}

cin is intuitive enough to understand your entering a double.
Your use of global vars "roottofind" and "input" seem misguided. They don't really aid you in information-sharing between multiple functions, and only make the rest of your funcs easier to mistype, so I would get rid them altogether and just have be local variables within main() or inputnumber().

Are you required to use this custom sqrroot function? B/c if you #include <cmath> there's a very good "sqrt()" function for just that purpose?

Your use of stringstream in this program doesn't really gain you anything either. If you want to validate the user's input and make sure it works as a double, that's not a bad thing tho. You should do something like read in the input as string first, then run std::strtod() (good documentation on that can be found on this site or cppreference.com) on it. Or read it in as double and then check if cin failed using "if (!cin)".

The reason you were stuck in this loop-of-asking you described is that your local variable input was never assigned the new value within the while-loop on line 46. it should be:
1
2
while(input <= 0) 
   input = inputnumber();


Also I'd avoid recursion wherever possible as its slower than iteration and eats up stack space. In some cases recursion is very helpful, but in yours it doesn't look like it is, especially because you have a large and expensive stream object eating up much, much stack space! A simple while loop in inputnumber() that demands re-entering the number if its <= 0, will do.
Thanks very much! I had somethig similar on a previous 'version'
The exercise was supposed to be an insight into recursion, so I have a recursive squareroot function, and maybe I am trying to be to clever for my level, but I tried to get inputnumber() call itself if the input was not above zero.


Thanks so far!!
@magnum pi

Thanks for the explanantion, I think I understand. We had to write our own recursive square root function as an exercise otherwise I would have done as you said. I will go away and 'adjust' and see what happens!!

Thanks again
Topic archived. No new replies allowed.