Thread: Declaring Integrars in a Switch // Case situtation

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    55

    Declaring Integrars in a Switch // Case situtation

    Hey guys...Im trying to declare z to either times multiply add subtract or devide. I take 2 numbers from the user at the beggining right...and then i ask them weather they wanna subtract (S) multiply (M) devide (D) or add (A) and watever they type in...the 2 numbers will do..heres the switch code:

    switch(entry)
    {
    case 'm':
    case 'M':
    int z = x*y;
    cout<<"Your numbers multiplied are:"<<z <<endl;
    break;

    case 'D':
    case 'd':

    cout<<"Your numbers devided are:"<<z <<endl;
    break;

    case 'S':
    case 's':

    cout<<"Your numbers subtracted are:"<<z <<endl;
    break;

    case 'A':
    case 'a':

    cout<<"Your numbers added are:"<<z <<endl;
    break;

    default:
    {
    cout<<"Sorry, You've entered an invalid character. Please retry!"<<endl;
    }

    }

    where it sais <<z is where its gonna do watever method to the 2 numbers x and y. Where do i declare z each new time? how do i declare it?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When you did
    cin >> x;
    cin >> y;

    Did you do a
    cin >> entry;

    And how did you declare entry.

    Otherwise, it looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    Yea i did..cuz the entry is for weather the user types in S M D or A....and the X and Y is for the numbers..and the Z is for the 2 numbers either S (subtracted) M (multiplied) D (devided) or A (added) ...but i wanna know where i declare that z = x*y for multipling or z = x-y for subtracting.etc...in the cases..where would i declare that?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OIC
    Declare z when you declare x and y

    int x, y, z;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    Yes yes yes!!!!!!! Its all coming together now!! I understand!!!!
    Im gonna take it a stetp further now...im now doing:

    cout<<" "<<x " multiplied by "<<y ", equals "<<z <<endl;


    wat is wrong with that? theres an error...but i dont know how to correct it
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    nvm got it. heh...thanks guys....next question...how would i do a loop kinda thing..where at the end..the user can press a key or sumthin and it reduz the program all over again without reopening it? anyone help?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    use a while loop...

    char GoOn = y;
    while(GoOn == y)
    {
    //do stuff
    //ask if user wants to go on
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    sorry im not familiar with while declaration....can u explain the code for me plz?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  9. #9
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    http://www.cprogramming.com/tutorial/lesson3.html

    That's all about loops...if you still have question lemme know and I'll edplan in more detail.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    yea im totally pysched out. could you go through the code with me blocks at a time? id really appreciate it
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  4. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  5. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM