Thread: Small program using "if" and some variables assistance.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Pielord View Post
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    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...

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Attached Images Attached Images Small program using &quot;if&quot; and some variables assistance.-triangle-png 
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    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 :/

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    So I cannot use it with codeblocks?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code::Blocks is an IDE, not a compiler, so it is possible.
    Go into options and look at the compiler options.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #24
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    "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?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    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).

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post your code and error messages. Finally, make sure "C++0x" is enabled when you post your errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    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 >.<

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's good news. You finally got it working.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    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...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-07-2011, 06:23 PM
  2. How to reference variables inside an "asm"?
    By User Name: in forum C Programming
    Replies: 9
    Last Post: 06-22-2010, 01:26 PM
  3. small programming confusion in "C"...
    By GajananDon in forum C Programming
    Replies: 1
    Last Post: 06-20-2010, 10:11 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM