### Thread: How to output one value rather than several with a while loop? C++

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0

#### How to output one value rather than several with a while loop? C++

I am new to c++. I have a problem outputting one value (or a few) rather than several values The program is designed to fin a value for X. such value of X must make the value of H as close to 10 as possible. Thus, variable "close =H-10" must be close to 0. When the program is ran, several values are displayed, going from 9 to 13 adding DX=0.0125 every time. How can I output (display) only one value, or less than 4 rather than hundreds of them?
The first part of the code where I state the variables is right, I believe. The the last part is where I need help.

CODE:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
double x=9;
while (x<13) // The while loop is used to calulate the numbers from 10 to 13.

{
double H,A,B; // H is a function where A and B are the variables(H=(A,B)). A and B are sides of triangles.
double DX; // DX is the amount added every time.
double close; // The answer must be close to 10.
A= sqrt (pow(20,2)- pow(x,2)); // From Pythagoras' theorem.
B= sqrt (pow(30,2)- pow(x,2));
H= (A*B)/(A+B);
DX=0.125;
closeness = H-10.0;
cout<< H <<endl;
x= x+DX;

{
const double DELTA = 0.50; // This is how close the answer has to be from 10
if(fabs(closeness)<=DELTA);
cout <<closeness << endl; // Display answer, only one!
}

}
cin.get();
return 0;
}
2. When you post code here, please use code tags. HTML strips out white space, which includes leading spaces and tabs that are used to indent code. Code tags keep HTML from doing that. These are code tags: [code] [/code] Simply type them and then copy-and-paste your code between them. Having retrieved your formatting via the Reply button, here is what code tags would have done for you:
Code:
```#include <iostream>
#include <math.h>

using namespace std;

int main()
{
double x=9;
while (x<13) // The while loop is used to calulate the numbers from 10 to 13.

{
double H,A,B; // H is a function where A and B are the variables(H=(A,B)). A and B are sides of triangles.
double DX; // DX is the amount added every time.
double close; // The answer must be close to 10.
A= sqrt (pow(20,2)- pow(x,2)); // From Pythagoras' theorem.
B= sqrt (pow(30,2)- pow(x,2));
H= (A*B)/(A+B);
DX=0.125;
closeness = H-10.0;
cout<< H <<endl;
x= x+DX;

{
const double DELTA = 0.50; // This is how close the answer has to be from 10
if(fabs(closeness)<=DELTA);
cout <<closeness << endl; // Display answer, only one!
}

}
cin.get();
return 0;
}```
I also highlighted in red what your mistake was: an extraneous semicolon. Since it probably doesn't jump out at you, here it is again (with recommended indenting included):
Code:
```            if(fabs(closeness)<=DELTA);
cout <<closeness << endl; // Display answer, only one!```
An extra semicolon all by itself creates what's called an empty statement. So by placing that extra semicolon at the end of the if-statement you are telling the program to do nothing if that expression is true ... and by the nature of an if, it will also do nothing if that expression is false. And since the cout that follows is not in any way connected to the if-statement, it will be executed every single time.

What you need to do is to remove that red semicolon.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0
dwise1_aol, thanks for your prompt response. I do appreciate your advice. I fixed the mistake I made by typing a semicolon after the if-statement.Nevertheless, I still have the same problem. I would like to output only one value like to cout only one value instead of all computed values.
4. To start with, you code will not compile, how could it be doing anything at all? You haven't declared the variable, closeness, but rather a variable, close, which you then do not use. It's a simple type or oversight, but it still keeps the program from compiling. Therefore, since you say that you're running the program, the code you posted here cannot possibly be the code that you are talking about.

When you post code here, it needs to be the code that actually produces the results you are talking about. That should be obvious, wouldn't you think?

Second, just exactly do you want your program to do? You are telling it to display the value of H each and every time until x reaches or exceeds 13. Isn't that what you want it to do? Furthermore, you are telling it to display all values of closeness that's less than or equal to 0.5 for however many times until x reaches or exceeds 13. Isn't that what you want it to do?

If either or both of those two things is not what you want your program to do, then you need to decide just what you do want it to do. Then you need to structure your code to do that. Design your code by writing down the steps that you would take yourself to perform the task. It may help guide you through that process if you were to write down a kind of a specification of exactly what you want your program to do. Such a specification would also help to communicate the same thing to us.

I like programming, but what we want to do is not how it's done professionally. When you program professionally, you start with the program requirements, what the end result needs to be, what you want the program to accomplish. Then you develop the specification, a detailed description of how the program will meet those requirements, including how the program will be structured and organized in general and what formulae and techniques are to be used. Then using the specification you design the program, deciding what classes and other data structures to use, what functions to write, what each function will do and how it will do it, what limits and boundary conditions exist and how to handle them, etc, in as much detail as possible. Only then do you actually start to write code, followed by testing, debugging, and integration. Most of the time is spent in the design phase and very little in the coding phase. And the more time we spend in the design phase, the less time we spend in debugging.

Most of us are code pigs who want to just start coding immediately, but if we approach a project in that manner then we end up with bad designs, weird problems, and huge amounts of waste as we end up having to throw away a lot of the code that we wrote. Of course, when we program professionally our projects commonly consist of many tens of source files, hundreds even. We absolutely need that kind of discipline in a professional project. You don't really need the same amount of discipline in a school project, but if you start to learn to apply even a little of that approach and discipline then it will help you greatly.