Hey How do you go about debugging? like tips and tricks?
I have been looking at my code (below) for like an hour and cant figure out what i have done wrong.....

#include <fstream> // Header File for File Reading
      #include <iostream> // Header File for General I/O-put
      #include <math.h>
      using namespace std; // Use namespacing
 
int main()
{

ifstream in_stream;
ofstream out_stream;

	int a(0), b(0), c(0), Lasta(0), Lastb(0), Lastc(0), counter(0);
	double bsqrd, fourac, root, root1, root2;
		

in_stream.open("inputfile.txt");
while (1)
      {
		Lasta=a; Lastb=b; Lastc=c;

		in_stream >> a >> b >> c;
		if (a==Lasta && b==Lastb && c==Lastc){break;}



		// calculations for quadratic equation

		bsqrd = b*b;
		fourac = 4*a*c;
		root = sqrt(bsqrd - fourac);
		root1 = (-b + root)/4;
		root2 = (-b - root)/4;

		NumberOfPositiveRoots(root1, root2);		// counting positive roots function


		cout << "the three values for this quadratic are" << " " << a << " " << b << " " <<c << endl;
		cout << "The value of x1 is: " << root1 << endl;
		cout << "The value of x2 is: " << root2 << endl;
		cout << counter << "of the x values are positive." << endl; 
		
		
      }



in_stream.close();

return 0;
}


      void NumberOfPositiveRoots(double first, double second)		// function for positive roots
      {
      int counter = 0;
    
	  if (first >= 0)
      {
      counter++;
      }

      if (second >= 0)
      counter++;
      }
      return counter;
      }

Depends on your debugger. I use Visual C++, which I think has one of the best debuggers, as you can right click on the error, and select "go to location". This will automatically position your cursor on the line of your problem. Since I do not know what your debugger is, I cannot really give you much advice..

Anyway, the problem with your code is as follows:
1) You have an extra bracket.
2) You need to initialize the function at the top, otherwise the program will not recognize it.
3) You do not need a return for a 'void' function.
4) I think you may have some logic errors, not sure though.

If you would like to Download Visual C++, go here: http://www.microsoft.com/express/download/#webInstall


I hope this post helps!

Edited 7 Years Ago by restrictment: n/a

Comments
Agree about vc++ debugger

lines 31 and 32 are:

root1 = (-b + root)/4;
root2 = (-b - root)/4;

while they should be:

root1 = (-b + root)/4*a;
root2 = (-b - root)/4*a;

What's more, I'm not sure about lines 19-22:

Lasta=a; Lastb=b; Lastc=c;
 
in_stream >> a >> b >> c;
if (a==Lasta && b==Lastb && c==Lastc){break;}

The most logical assuption is that program should exit form the loop when user inputs 3 zeros. And that's how it works for the first iteration. Now think what will happen if someone will input f.e. 2,3,4 in first iteration. What will happen in the second?

ya, i caught the missing a but good catch.

those lines work (19-22), as it runs through the input file it checks to see if the last three values it ran through are the same ie. if instream was unable to change a,b,c then its done.

This article has been dead for over six months. Start a new discussion instead.