Results 1 to 7 of 7

Thread: New into C++ Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360

    Talking

    Alright. I am real new at C++ and have a simple bracket { question.

    I am not sure of the placement in a nested if, else if statement. Please help!
    Here is an example...It gives the same response no matter what number you enter, although I don't know why:



    #include <iostream.h>

    void main()
    {
    //declare and initialize variable
    short code = 0;

    //get code from user
    cout << "Enter the code (1 through 5): ";
    cin >> code;

    //display salary
    if (code = 1)
    cout << "$45,000" << endl;

    else
    if (code == 2 || code == 5)
    cout << "$33,000" << endl;

    else
    if (code == 3 || code == 4)
    cout << "$25,000" << endl;

    else

    cout << "Entry error" << endl;
    //end if (code == 3 || code == 4)
    //end if (code == 2 || code == 5)
    //end if (code == 1)

    } //end of main function
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  2. #2
    Lively Member
    Join Date
    Jun 2000
    Posts
    122
    //display salary
    if (code = 1)
    cout << "$45,000" << endl;
    Use "==" instead. Here you are assigning the number 1 to code instead of checking to see if it equals 1.

  3. #3
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Also, for me at least, it is easier to read 'if' statments if the you put {} around the statment you want to be executed.

    Like:

    Code:
    if (x==y) {do this}
    else if (x==z) {do this}
    else {do this}

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360

    Thumbs up

    Thanks! I spaced on the "=="
    VB was a breeze, this is tough!
    You guys are awesome!
    Cheers!
    Lee
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  5. #5
    Addicted Member machine_wars's Avatar
    Join Date
    Dec 2000
    Location
    NC
    Posts
    147
    You use the {}'s if there is more than one code line of events to do.

    if( a == 4 ) {
    cout << "This is a test" << endl;
    cout << "This is only a test" << endl;
    }
    else
    cout << "Bye" << endl;

  6. #6
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    yeah i know, but when using nested if statments you need them and i usually use more than one conditional. So i just always put them and it seems to make it easier for me to read.

    Just my opinion though

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also if you indent each bracket level it makes it a lot easier to follow the flow:
    Code:
    if(x) {
        if(y) {
            cout << "x && y" << endl;
        }
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width