Codes runs once but not a second time

It has been happening to me ever since I started writing codes.I write a code, it runs, gives me the right outcome and then the next time I run it again or rewrite the code it gives me weird answers or doesn't even work even though I basically copy and paste it. for example the code bellow...It doesn't work when it did a week ago. What is happening????

thanks for your help.
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
  #include <iostream>
using namespace std;
double sqrtnum( double num)
{
	double lower_bound=0;
	double upper_bound=num;
	double temp=0;
	while((temp*temp) - num > 0.01)
	{
		temp=(lower_bound + upper_bound)/2;
		if(temp*temp>=num)
			upper_bound=temp;
		else
			lower_bound=temp;
	}
	return num;
}
int main()
{
	int a,b,c;
	int delta;
	cout<<"enter 3 numbers"<<endl;
	cin>>a>>b>>c;
	delta=b*b - (4*a*c);
	if(delta>0)
	{
		cout<<"X1="<<((b+sqrtnum(delta) / 2*a))<<endl;
		cout<<"X2="<<((b - sqrtnum(delta) / 2*a))<<endl;
	}
	else if(delta==0)
	{
		cout<<"X="<<((b+sqrtnum(delta) / 2*a))<<endl;
	}
	else if(delta<0)
	{
		cout<<"No root"<<endl;
	}
	return 0;
}
What IDE are you using?

Also, your sqrtnum function literally does nothing. It does not modify num- it just returns it.
Last edited on
@Ispil How should I write it then?
And what is IDE?
Last edited on
An IDE is an "Integrated Development Environment" It's basically a program that allows to make other programs. Some include: Visual Studios, Codeblocks, and lots others.
Visual Studio...Yes...I use Visual Studio. and about the function...I'd appreciate if you helped me.
Describe what exactly you are trying to do. Is the function just supposed to return the square root of a number?
yes...the rest is to find the roots of a function which I don't have a problem with...But my sqrtnum is, as mentioned above, giving me wrong answers...
I'm not sure. What I would do is start putting cout statements all over the place (or you can use asserts) and you might be able to figure it out that way. But honestly I don't know.
Thanks anyways.
Well, you actually don't need to make your own sqrt function. If you add #include <cmath> to the header, than the std library has a built-in square root function called sqrt. You can save all of the headway by just using that instead of trying to... well, create your own.
Reread the first comment by ispil. Look at line 17. You are returning num, the value that you passed into the function. This is not the right thing to return.
Should it return temp?
I changed it but it still gave me wrong answer
Topic archived. No new replies allowed.