Click here to Skip to main content
14,639,649 members
Rate this:
Please Sign up or sign in to vote.
See more:
My code is stated below, the problem occurs when the program comes to the if loop. I have floated the value of temp1 because i am trying to get the tan 45 which is 1 but since i'm using visual studio it runs in radians therefore i have a few lines of code for the conversion to occur. It works out to be a quarter pie which is a long number therefore i used a float to make it have a value of 1.
Anyways before the if statement i have 2 test lines which tell me the values of the 2 variables that i am going to compare these values do match but the statement isn't entered.
Also in the if loop it is worth pointing out if i replace the variable temp1 with the number 1 the loop works as it should be.

float degrees = 45 ; 
	float temp1,temp2, t9;
	////Generating a ray function//////////////////////
	row_old = 7; 	column_old = 7;
	
	t9 = degrees / (180 / 3.1415926);//converting from degrees to radiance
	temp1 = tan(t9);						//finding out what the tan value
	//cout << t9; 
	//cout << temp1 << endl;
	for (temp_row = 1; temp_row < 10; temp_row++){
		for (temp_column = 1; temp_column < 10; temp_column++){
			temp2 = temp_row / temp_column; 
			//cout << "temp2:"<< temp2;			// testing statement
			//cout << " temp1:" << temp1 << endl;	//testing statement
			if (temp2 == temp1 ){
				//cout << temp_column << endl; 
				//cout << "inside the loop the column value is" << temp_column << endl;
				row = temp_row; 
				column = temp_column; 
				temp_row = 10001; 
				temp_column = 10001; 
				cout << "solution found\n"; 
			}
		}
	}


What I have tried:

different variables, replacing the variable with a value, adding lines of code to see what is occurring inside the loop
Posted
Updated 28-Mar-16 4:56am
Comments
Sergey Alexandrovich Kryukov 28-Mar-16 10:45am
   
Programming is not done by trial. Just use the debugger.
English word "try" has at least two meanings, which are quite different: 1) put effort (such as in "try hard"), 2) take a chance, make an attempt.
In the title "What I have tried", only the first meaning is applied. The intent is: you show your code sample in this section, so we could find out if it has some problem.
—SA
bahjat93 28-Mar-16 10:52am
   
I know what the problem is, the problem is the temp5 variable which takes the tan(t9) value doesn't seem to be operating as programmed when i use it into the if loop. I know what the solution is and i have just applied it. I am confused to why this problem is occurring?
Rate this:
Please Sign up or sign in to vote.

Solution 1

So start with the debugger.
Put a breakpoint on the if line and run your program.
When it reaches the breakpoint, it will stop and let you look at exactly what is going on. Use the debugger to look at the contents of temp1 and temp2 by hovering the mouse over them and see what the values are. If they match, step a line and it should enter the loop. If it doesn't, then check the numbers again, really closely: you need them to be accurate all the way to the 15th decimal point in order to pass the if condition!

This is a skill - predictably enough called debugging - and you only develop it by using it, so practice it here with a trivial piece of code before you need it on a large complicated bit! :laugh:
   
Comments
Patrice T 28-Mar-16 11:00am
   
5ed
bahjat93 28-Mar-16 11:05am
   
these two lines of code check the values of the 2 variables and i thought this was debugging.
//cout << "temp2:"<< temp2; // testing statement
//cout << " temp1:" << temp1 << endl; //testing statement
I have turned these comments into a comment not to give me too much data but when i don't these values are checked and on the first try they match up.
I can't see what the problem is at all with my code because i am checking the value at multiple occasions and they match.
OriginalGriff 28-Mar-16 11:51am
   
Just because they seem to match when you print them means nothing - floating point numbers aren't that simple! When your print them, the print methods tend to round them to 4 or 5 significant figures, so 1.0000000001 looks like 0.999999999
You can't work with that, so you need to use the debugger proper to look at exactly what happens, and what contains what. "Printing things out" is the start of debugging we used to do when I started, but nowadays we have much, much more powerful and flexible tools available to us (and have done for the whole of the twenty first century! :laugh:)
I don't know what system you are using, so have a look in the help or Google "mycompiler debug tutorial" (for example "visual studio debug tutorial") and see what you can do. You can do a lot more than add print statements these days!
bahjat93 28-Mar-16 13:50pm
   
@originalgriff thanks for your input, i'm only in my early 20's and am self teaching myself c++. I feel like my methods were valid in the early 60s. None the less i have started reading the debugging tools and am now applying the knowledge to what I learnt.
Once again thanks for your input
OriginalGriff 28-Mar-16 14:02pm
   
You're welcome!
BTW: if you are teaching yourself, that's probably a mistake! :laugh:
Get a book (or better a course) and follow it through, doing all the exercises. They will introduce you to each of the facilities in order, so you don't miss anything that would have made your life a lot easier.
And if you aren't using Visual Studio, get a copy today! The Community Editions are free and it's (almost certainly) the best IDE available. It let's you code (with a lot of intelligent help), build, and debug within the same interface, and you can even change code while it's running in the debugger.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

While debugging your program, pay special attention to temp1, you may discover that it is very close to 1 but not equal to 1, thus failing your test.
   
Comments
bahjat93 28-Mar-16 11:58am
   
Thanks a lot, your answer is really helpful and nicely worded. I honestly do appreciate your help, the problem is I have self thought myself C++ and most online websites don't teach you how to debug your program so I will read up on the 4 articles you have given me. I thought i was debugging but clearly i see i was not doing it the proper way.
Once again thank you for your help
Patrice T 28-Mar-16 12:03pm
   
Thank you.
More specifically, look for articles about debugger for your compiler/IDE

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100