New, not completely understanding

This is a discussion on New, not completely understanding within the C++ Programming forums, part of the General Programming Boards category; Well, I've gone through the first lesson and a half of the C++ program that this site offers, and as ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    New, not completely understanding

    Well, I've gone through the first lesson and a half of the C++ program that this site offers, and as I'm reading Lesson 2, this comes up:

    Code:
    cout<< ( 2 == 1 );
    Now, I understand what this is basically trying to say, but a couple of things are getting me. First off, what exactly does cout mean? Does it tell the program to read the statement, and decided it if it is true? But that wouldn't make sense with something where you're typing in plain text. So I'm kind of confused there. Apply that to int, float, and cin, as well.

    Also, I do not know what I should be coding in for a simple math problem. I tried to just modify the hello world into something else, but not knowing what int, float, cin, cout, etc. mean are hindering me. I may have just misread, or skipped it accidentally, but it isn't very clear to me overall. Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    cout is the way of outputting something to the console. So in that code, the result of (2 == 1) is output to the console.

    Just the existence of (2 == 1) tells the program to decide if that's true, the cout does not cause that to happen.

    So cout is the object meant for output to the console, cin is meant for input from the console, int, float, double and char are types that represent integers, floating point numbers, and characters.

    If you'd like help with your math problem attempt feel free to post what you've got and we can try to help correct your understanding. Also, consider re-reading the first lesson. Sometimes it takes two or three times for things to sink in.

    You should also consider getting a book if you're serious about learning C++. Programming is complicated and difficult to learn from a serious of tutorials.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    Hmm, so if one has cout<< (2 == 1), what else should be going on in the whole code itself? Having that may very well help me overall.

    And yeah, I am planning on picking up two books on C++, but have yet to find the time ( or resources to get me there).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    >> Hmm, so if one has cout<< (2 == 1), what else should be going on in the whole code itself?

    What do you mean? That code evaluates (2 == 1). Since it is false, it then outputs false to cout (in this case it means it outputs 0 because 1 is true and 0 is false). If it was true then 1 would be output.

    It makes no sense as anything other than an example because you know that (2 == 1) is false.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3
    No, no. I mean, what should the full code be? Or should I just type in cout<< (2 == 1) and then hit f9?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    You want to be able to compile this into a working program?

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << (2 == 1);
        return(0);
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    To anticipate the next question. std::cout means:

    std == standard c++ namespace
    cout == c - out; standard c++ output

    The operators '::' allow you to access the std namespace and '<<' allows you to output the following arguments to cout. This is a very poor explanation but it'll help.

    cout << (2 == 1);

    ...basically translates to: if( 2 equals 1) then print 1 (true) else print 0 (false).

    This technique usually involves variables since 2 will never be equal to 1, it doesn't normally make sense to evaluate the statement as true or false because its static (unchanging). Normally, you would do something like the following:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
      int x = 2; /* in your program, this variable would change and could be anything */
    
      cout << (x == 1) << endl;
      cout << (x == 1 ? "true" : "false") << endl;
    
      return 0;
    }

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is also possible to use the boolalpha format flag:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::boolalpha;
    
    int main()
    {
      int x = 2; /* in your program, this variable would change and could be anything */
    
      cout << (x == 1) << endl;
      cout << boolalpha << (x == 1) << endl;
    
      return 0;
    }
    This has the same output as JupiterV2's code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    And yeah, I am planning on picking up two books on C++
    Please make sure you get some good stuff. There is a lot of trash as well, I just bought some weeks ago a horrible book on C++. Especially for beginners, if you do not know how to judge the information it gives, or if you buy a highly advanced book.
    May I give you two suggestions?
    1) Thinking C++ (also listed in this forum)
    2) C++ How to Program (Deitel and Deitel)

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    Note that even Deitel and Deitel teaches some C style C++ if I remember correctly. It gets generally good reviews but I would prefer books that teach modern C++ style (and there are only a couple of them).

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Quote Originally Posted by Daved View Post
    Note that even Deitel and Deitel teaches some C style C++ C++ style
    Are you talking about arrays? It generally seems to go from:

    Code:
    int array[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Then into 2 Dimensional, and certain issues of it dont even touch vectors. The 5th edition does but it does concentrate more on C style like above, which of course is still worth knowing.
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. completely stuck understanding how to use time.h
    By pastitprogram in forum C++ Programming
    Replies: 11
    Last Post: 04-04-2008, 10:11 AM
  3. understanding pointer arithmetic
    By firstofthegreat in forum C Programming
    Replies: 3
    Last Post: 02-28-2008, 10:08 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21