Thread: New to C++/ Couple of questions...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    22

    Question New to C++/ Couple of questions...

    Hi everybody. I'm new to C++, and am very grateful for this site/board. I plan to post on this board a good deal, and to work hard learning C++.

    Today I went through the first 4 Lessons, and have a couple of questions I was hoping you guys could help me out with.

    1) #include <(header files)> - Where do these header files come from? In the tutorial it discusses using iostream.h, but I was very confused on this subject. Basic info would help alot.

    2) I need a little more tutoring on the C++ Boolean Operators, such as !, |, &&. I became confused with this, and need some more info and maybe some sample code using these operators. I understand the basics, but I don't understand problems like:

    A. !(1 || 0) Answer: 0
    B. !(1 || 1 && 0) Answer: 0
    C. !((1 || 0 ) && 0) Answer: 1

    3) I am not totally clear on the == operator. What is the difference between = and ==? I believe that == is used for setting things equal, so what is = used for?

    -- Thanks for any help you guys can give. I love programming, and am having frustrating fun learning C++!
    - Visual C++, Adobe Photoshop -

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    1- header files come with your compiler as far as i know. you can look at them, just double click on iostream.h in mycomputer (don't save anything though)

    2-?, i've mainly used logic operators as conditions in loops and stuff . . . if (x<2 || x>7)

    3- read "a==b" as equals "a equals b"; "a=b" read as b"b copied into a".

    fyi, go get a book. the tutorials are great, and most people on the board are helpful. but a book is the best way if your not actually taking a class.

    does that help?

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    || = or

    && = and

    for example:

    Code:
    if(var1 == 1 || var1 == 2) { // if var 1 is equal to 1 or 2
    
    } else if(var1>1 && var1!=3) {
    //if var1 is more than 1 but not 3
    }

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    1.
    The #include directive adds the contents of the file you specify. The standard header files iostream, math, etc can be found in the include directory for your compiler (go ahead and look at them). C/C++ has a set of these headers that ANSI has decided would be beneficial to programmers. There are many other libraries as well that can be used but are not "part of C/C++." Two such libraries that frequently cause problems are graphics.h and conio.h. Some compilers include them and some do not, they are not part of the standard so you should be careful if you use them as other people compiling your code may not have them. (sorry that got a bit long)

    2.
    || is a logical OR meaning if (at least) one of the two boolean expressions are true than the result is true.

    && is a logical AND meaning that only if both expressions are true will the result be true.

    ! simply negates (switches) an expression so !(true) becomes false.

    3.
    = is the assigment operator (in Pascal or ADA it's := which I think is less confusing) which assigns a value to a variable.

    == is the equivalency operator (this will not work for character strings however, so be forewarned).
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    22
    okay thanks for the help guys. i do plan on getting a book soon, these tutorials are giving me a good basis of c++ though to start out w/.
    - Visual C++, Adobe Photoshop -

  6. #6
    Unregistered
    Guest
    here's an on line free for use full text version of a reasonable text book. Not the latest edition anymore (I don't think anyway), but can take you farther than the tutorials, and it's free. It ain't like having a hard copy version handy IMO, but ain't bad in a pinch or for general reading when not trying to do a project.

    http://www.stud.fim.ntnu.no/~oystesk/CPP/htm/ch01.htm

  7. #7

    Lightbulb Weird Expressions

    Hey. The posts above contain good information, but so far, i don't see any explanations of A B and C.
    A. !(1 || 0) Answer: 0
    B. !(1 || 1 && 0) Answer: 0
    C. !((1 || 0 ) && 0) Answer: 1
    Did they have a question mark infront of them?
    A. ?!(1 || 0)) Answer: 0
    B. ?!(1 || 1 && 0) Answer: 0
    C. ?!((1 || 0 ) && 0) Answer: 1
    if they did, the explanation is easy.

    They basically read "If 1 or 0 is true, then Answer. Otherwise, 0"
    the format if the shortened if statement is "?(condition)true:false;" and it evaluates to the value of the expressionchosen by the if statement.

    For example,
    [CODE]
    int a,b,c;
    char and[
    a = 1; // 1 is also equal to true
    b = 0; // 0 is also equal to false
    c = ? (a || b) 1 : 0; // wll make c equal to 1 because the condition is true

  8. #8
    Sorry about that, it cut off.

    In summary, the ?: operator is a short version of the if block.

    Code:
    int a;
    bool expression = true;
    a = ?(expression) 1 : 0;
    is the same as
    Code:
    int a;
    bool expression = true;
    if (expression)
    {	a = 1;
    }
    else
    {	a = 0;
    }
    Obviously, the first is shorter, therfore more prefrable.

    ~Inquirer

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    22
    sorry for the confusion, but what i meant was:

    in the online tutorial from this site, it tells of operators and what they mean. then it lists some examples of problems, like algebra problems, of how the operators work. like:

    A. !(1 || 0) Answer: 0
    B. !(1 || 1 && 0) Answer: 0
    C. !((1 || 0 ) && 0) Answer: 1

    so that A. is like problem 1, and Answer: 0 means the answer is 0.

    lol sorry for confusing again.

    i've gotten through the 10 lessons in the tutorial now, and understand them to some point. to bad i still have not finished downloading my visual c++ compiler, so i can't put my knowledge to use yet. oh well at least im understanding the language a little more.

    one more quick question:

    what does "endl" stand for? what does it do?

    thanks for all the help, you guys are great. if it is lame for me to be asking all this, plz just let me know.
    - Visual C++, Adobe Photoshop -

  10. #10
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    endl:

    cout << "Hello universe!" << endl;
    // makes a newline, beeps, and i think it flushes the input buffer...

    [edit]
    You are not lame when you ask questions! What do you think this forum exists for
    [/edit]

  11. #11
    are you sure it beeps?
    i haven't noticed it doing that in my programs...

    ~Inquirer

  12. #12
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Well actually that was a thing i wasn't sure of =) I was thinking of putting in "(not sure)" there.
    I just seem to remember that it beeped for me......
    Must've been some weird imagination

  13. #13
    I traced the beeping mine made one time to the unsigned character equivalent of 7, or \a, which beeps when inserted into the stdin. but the << endl was totally silent on my computer. It may also be the compiler. I am using Bloodshed Dev-C++ 4.

    ~Inquirer

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Dont think anyone else has mentioned that
    0 == false and non-zero == true...

    that's quite key for the examples that you wanted a description of.

    eg:
    Code:
    while(1)  // infinite loop, always true
    {
        DoSomething();
    }
    
    while(0) // never executed, always false
    {
        DoSomethingElse();
    }
    cheers
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 02:50 PM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  5. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM