quick question (please help

This is a discussion on quick question (please help within the C++ Programming forums, part of the General Programming Boards category; i am quite new to C, so my understanding of a few statements are unclear. first, what is the meaning ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    11

    Unhappy quick question (please help

    i am quite new to C, so my understanding of a few statements are unclear. first,

    what is the meaning of selection, i know it has to do with programs being executed on basis of true of false, but a concrete defintion or example is still a bit unclear for me.

    then, what could be a few good examples of if statement, if else, else statements

    i did a few, dont know if i am correct, perhaps you could make some just to make it more clear.

    if statement
    # include<iostream.h>

    void main()
    {
    int num1;

    cout<<"Enter a number:";
    cin>>num1;

    if(num1>0)
    cout<<"Number is greater then zero";
    if(num1<0)
    cout<<"Number is lesser than zero";
    }

    if else

    #include <iostream.h> //Include the header file

    void main() //Most important function
    {
    int age; //Variable declaration


    cout<<"Enter your age please:"; //Prompt user for input
    cin>>age; //Accept input

    if(age>=18)
    cout<<"Congrats! You are eligible to vote!";
    else
    cout<<"Sorry!You are not eligible!";

    }


    switch case

    # include<iostream.h>

    void main( )
    {
    int num1,num2,ch;

    cout<<"\t\t\t MATH CALCULATOR\n\n\n";
    cout<<"\t1. Add\n";
    cout<<"\t2. Sub\n";
    cout<<"\t3. Mul\n";
    cout<<"\t4. Div\n";
    cout<<"Enter 2 numbers:\n";
    cin>>num1>>num2;
    cout<<"Enter ur choice:";
    cin>>ch;

    switch(ch)
    {
    case 1:
    cout<<num1+num2;
    break;
    case 2:
    cout<<num1-num2;
    break;

    case 3:
    cout<<num1*num2;
    break;
    case 4:
    cout<<num1/num2;
    break;
    default:
    cout<<"Wrong menu option!";
    break;
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    any useful webpages that may be helpful

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Welcome to the world of C++!

    Your if, if-else, and switch statements look OK to me upon cursory inspection, although I haven't tried to compile them (this is the best way to learn a programming idiom or control structure...put it in a simple program and try to compile it). My only comments:

    1. Please, please, please use code tags when you post code! Read the sticky notes at the top of the board for instructions.

    2. Some members on this board, including myself, feel very strongly that in C++, as opposed to C, the main() function should be declared as type int, not void, i.e.
    Code:
    int main() { ... }
    not
    Code:
    void main() { ... }
    The C++ standard specifies that main returns an int value.

    As to your selection question, I'm not sure that I understand what you are referring to, maybe someone else could clear that up?
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    thanks for the help.

    but dont you have a definition with a few short examples for selection

    so far, my definition has been

    "Function in C++ where the execution of the entire program branches out to a other different part within the program, depending if the value of the condition is TRUE or FALSE."

    anything i should add, like an examples

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Okay, according to my favorite reference (Deitel & Deitel, "C++: How to Program") a selection is a generic term for a control structure that performs one of a series of tasks. Thus, the if, if-else, and switch structures are all selection structures. They imply that these are the only selection structures, but I don't think they come right out and say so.

    Hope this helps
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    Originally posted by geophyzzer
    Okay, according to my favorite reference (Deitel & Deitel, "C++: How to Program") a selection is a generic term for a control structure that performs one of a series of tasks. Thus, the if, if-else, and switch structures are all selection structures. They imply that these are the only selection structures, but I don't think they come right out and say so.

    Hope this helps
    alright, that would make a good addition there. thanks.

    what kind of program would be best for an example of a switch statement

    so far i have the math calculator one only, but that is too long.

    --------------------------------------

    # include<iostream.h>

    int main ( )
    {
    int num1,num2,ch;

    cout<<"\t\t\t MATH CALCULATOR\n\n\n";
    cout<<"\t1. Add\n";
    cout<<"\t2. Sub\n";
    cout<<"\t3. Mul\n";
    cout<<"\t4. Div\n";
    cout<<"Enter 2 numbers:\n";
    cin>>num1>>num2;
    cout<<"Enter ur choice:";
    cin>>ch;

    switch(ch)
    {
    case 1:
    cout<<num1+num2;
    break;
    case 2:
    cout<<num1-num2;
    break;

    case 3:
    cout<<num1*num2;
    break;
    case 4:
    cout<<num1/num2;
    break;
    default:
    cout<<"Wrong menu option!";
    break;
    }
    }

    ^-----------------------------

    i am basically looking for a more brief program which is simpler and shorter.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    49

    Smile switch

    This would be an example of a program using switch:
    Code:
    #include <iostream.h>
    int main()
    {
        int nVar;
        cout << "Pick a number (1/2)" << endl;
        cin >> nVar;
        switch(nVar)
        {
            case 1:
                cout << "You picked the number 1!" << endl;
                break;
            case 2:
                cout << "You picked the number 2!" << endl;
                break;
            default:
                cout << "Dumbass! Pick either 1 or 2!" << endl;
                break;
        }
        return 0;
    }
    Or you could make a guessing game
    // Kavity

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    11

    Re: switch

    Originally posted by Kavity
    This would be an example of a program using switch:
    Code:
    #include <iostream.h>
    int main()
    {
        int nVar;
        cout << "Pick a number (1/2)" << endl;
        cin >> nVar;
        switch(nVar)
        {
            case 1:
                cout << "You picked the number 1!" << endl;
                break;
            case 2:
                cout << "You picked the number 2!" << endl;
                break;
            default:
                cout << "Dumbass! Pick either 1 or 2!" << endl;
                break;
        }
        return 0;
    }
    Or you could make a guessing game
    // Kavity
    thanks a lot mate. but the dumbass part isnt something you want to have when presenting this to a group of 40 year olds...

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    49

    Hehe...

    I dunno it would be kind of fun
    // Kavity
    i'm not stupid, just a little short on brains.
    Kav's game!
    Featuring:
    # - Goodguy mc goodguy
    * - Bad guy mc badguy
    $ - Princess Mc Cess
    @ - Mcdonalds Mc chicken! mmmmmm

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    so do you know anything about hacking? does it involve C++

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Before you consider this line of questioning, please read the Announcement at the top of the board:
    http://cboard.cprogramming.com/annou...p?s=&forumid=3, specifically guideline #6,
    and be very clear on whether you are talking about hacking or cracking. If you're confused about the difference, hacking good, cracking bad.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    any useful webpages that may be helpful
    There are some good tutorials on this website, check them out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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