# Small program using "if" and some variables assistance.

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 01-22-2012
Elysia
Quote:

Originally Posted by Pielord
Oh, been looking it up and there seem to be numerous different styles involved, any you would recommend or doesn't it really matter as long as I follow something?

Yup.

The last code looks like the easiest thing to implement, but what exactly these lines:

Code:

```structureA triangle;   trig *tri;       structureA.hyp;   structureA.adj;   structureA.opp;```
do, I have no idea.

Anyhow, start off with something easy, and then expand upon it. You could start with the above example, basically having the user input whatever information is necessary for the program to calculate the angle.
Then you could go a little more advanced.

Let me show you an example of how you could make a program to let the user enter all three sides (some being unknown) and all three angles (some being unknown) and allow you to calculate angle or sides.

First off, ask user for all sides and angles. User can input 0 if they are unknown.
Then ask what angle to calculate (name the angles 1, 2 and 3).
From that, determine the opposite side, etc. Then calculate.

A simple example might look like:

Code:

```std::array<int, 3> side; std::array<int, 3> angle; int angle_to_calc; std::cout << "Enter sides (0 for unknown): "; std::cin >> side[0] >> side[1] >> side[2]; std::cout << "Enter angles (0 for unknown): "; std::cin >> angle[0] >> angle[1] >> angle[2]; std::cout << "Enter angle to calculate: "; std::cin >> angle_to_calc; int hypol = side[1], oppol, nearl; switch (angle_to_calc) {     case 1: oppol = side[3]; nearl = side[2]; break;     case 2: oppol = side[2]; nearl = side[3]; break; } float result; if (hypol != 0 && nearl != 0)     result = acos((float)nearl / hypol); else if (hypol != 0 && oppol != 0)     result = asin((float)oppol / hypol); else if (nearl != 0 && hypol != 0)     result = atan((float)oppol / nearl); else {     std::cout << "Insufficient information to calculate angle!\n";     return 1; } std::cout << "The angle is " << result << " radians.\n";```
You could make it more complicated, for example, if some needed information is not known, it could try calculating that unknown information. Say you need the hypotenuse. You might calculate that using some other known information. And if some information needed to calculate that is unknown, you could calculate that unknown with some other known information.
That might be a little tricky, but it's certainly possible (especially using recursion).
• 01-22-2012
Pielord
I think I finally 'get' arrays now after seeing it used that way o.o, I also felt like shouting "Eureka!" when I saw how you used those boolean operators, ty so much its exactly what I needed to understand.
A few questions, why is std:: in front of all the input and output codes? And are the hypo. oppo and near variables floating points or integers, I can't tell with the arrays and the actual formulas...
• 01-22-2012
Elysia
Everything in the standard library resides in the std namespace. Hence, to access them, I have to tell the compiler what namespace to search in. That's what I do with the "std::". Another alternative is to simply tell the compiler to take everything in the namespace std and dump it in the global namespace. That's what you do with "using namespace std;". The later can cause name collisions (for example, you can't name function min or max because they already exist in the std namespace and because of the importing, now in the global namespace).

hypol, oppol and nearl are all integers (see the declaration).
Only result is a float.

Btw, forgot to attach this picture. The example I showed assumes the triangle looks like this.
• 01-22-2012
Pielord
When I try to use your notation with arrays it doesn't work, " "array" was not declared in this scope". I also get a whole bunch of errors when I try adding variables :/
• 01-22-2012
Elysia
Be sure to include <array> and switch on C++11 for your compiler (or get a C++11 compliant compilers, such as GCC or Visual Studio).
• 01-22-2012
Pielord
So I cannot use it with codeblocks?
• 01-22-2012
Elysia
Code::Blocks is an IDE, not a compiler, so it is possible.
Go into options and look at the compiler options.
• 01-23-2012
grumpy
Quote:

Originally Posted by Elysia
Another alternative is to simply tell the compiler to take everything in the namespace std and dump it in the global namespace. That's what you do with "using namespace std;".

"That" is not what "using namespace std;" does.

The "using namespace std;" directive tells the compiler to treat any names in namespace std as candidates to match names used in code. It does not dump the contents of namespace std into the global namespace. If it did, it not be possible to consistently resolve any naming clash by using the :: operator. For example, "::cout" would always match std::cout, even if there was something else named cout in the global namespace.
• 01-23-2012
Pielord
"C++11" does not appear anywhere on codeblocks in any options i can find. I've been scrolling though compilers and their associated options for an hour...
Is there another way to accept the input and match to variables without using arrays?
• 01-23-2012
Elysia
It's a compiler option. I strongly suggest you make it work instead of avoiding it since you are missing out on a lot of stuff otherwise.
Perhaps it might help if you mention what compiler you are using. For that matter, GCC works with C::B and has support for C++11.
C++11 might also be known as "C++0x" in some places.
• 01-23-2012
Pielord
I have tried everything from enabling only the section that contains "c++0x" to enabling almost everything. Not a single option in any of the compilers with GCC in their names gets rid of that array error message (In fact with one attempt I managed to get rid of every other error but that one).
• 01-23-2012
Elysia
Post your code and error messages. Finally, make sure "C++0x" is enabled when you post your errors.
• 01-23-2012
Pielord
Its one of those moments again...
I'm so sorry for wasting your time. I went through all the past messages and checked everything. I've tried everything suggested so far but I didn't quite do them simultaneously... I once had array included, and later I had enabled c++11, but not until now have I remembered both. Everything works perfectly and I'm fairly sure I know where to go from here, ty for all the help.
Again, sorry >.<
• 01-23-2012
Elysia
That's good news. You finally got it working.
• 01-26-2012
Pielord
I ran into a bit of an issue when I tried using everything mentioned thus far in this thread for another program. I mentioned the Physics variables one earlier, anyway I tried it and everything works great from where the user types in the value of each variable and uses 0 if it is unknown. The only problem is that when i have an actual value that IS = 0, it doesn't work because the if statement reads "if ( var=="v" && a!=0 etc etc). So it detects the 0 as an unknown rather than a value...I've tried strings and all other things but none work with boolean algebra unless they're numbers. Do i have to double all my coding and first use strings and char with if statements to determine the unknowns and the values and THEN only run it through the main formulas part, or is there another way?
(If that made as little sense to you as to me I'll post the program...)
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last