Question on Select Case?

This is a discussion on Question on Select Case? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. Im just getting started on my own programming skills, and I have a slight problem. I forgot how ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    3

    Question Question on Select Case?

    Hello everyone. Im just getting started on my own programming skills, and I have a slight problem. I forgot how to do select case. Where its a condition and you hit a certian key (ex: y/n) to activate certian code or to re loop to start the program over again.

    I kinda remember what it is, but im not sure. Something like this?

    Select case

    case 1:

    I forgot how to do the slect case right here. Im sorry but i havent been in a programming class for about a year, and I forgot almost everything. Some help here? What im really trying to do is make it to where at the end of my program, i want it to ask the user do you want to do this again? (y/n), and if they hit anything other than y, then it will end the program, if they hit y, then it will re-loop back to the top and run it again clearing the screen. Any help would be very much appricated.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    switch(MemoryLevel())
    {
       case Low:
          cout << "I can't remember switch!";
          break;
    
       case Medium:
          cout << "It's coming back to me now!";
          break;
    
       case High:
          cout << "Ah, I remember everything now!";
          break;
    
       default:
          cout << "Brain short circuit...";
          break;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    3

    No no no, not switch case, Select Case

    In my programming class we did it to where you hit the letter n, or letter y, and when you did that, it would automatically either end the program, or loop back to the top and start the program over again. For example lets make something easy

    #include <iostream.h>

    int main()
    {
    int x, y;

    cout << "Enter a number: ";
    cin >> x;
    endl;
    cout << "Enter another number: ";
    cin >> y;
    endl;
    cout << "The sum of the two numbers is: << (x+y);
    endl;
    cout << "Run the addition caculator again? (y/n)";

    // this is where i need to put the selection between yes or no, and i forgot how to do it.


    }

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    You could use a while-loop, its not a perfect solution but it works.

    Code:
    .......
    int x, y;
    char again = 'n';
    //Not perfect error-handling
    while (n == 'Y' || n == 'y')
    {
    cout << "Enter a number: ";
    cin >> x;
    cout << endl << "Enter another number: ";
    cin >> y;
    cout << endl << "The sum of the two numbers is: << (x+y) << endl;
    cout << "Run the addition caculator again? (y/n)";
    cin >> again;
    }
    .....

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    3
    yep, i was just typing this reply as ripper079 answered your question. a while loop is probably the best solution.

    ps select case is a visual basic control structure.

  7. #7
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    [code]

    char y_or_n;

    while(true){

    /***********

    Do Stuff

    ************/

    cout << "Would you like to go again?(y/n)";
    cin >> y_or_n;

    if(y_or_n == 'y')continue; else break;

    }
    Last edited by beege31337; 12-08-2002 at 07:58 PM.

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: No no no, not switch case, Select Case

    Originally posted by Wescb
    In my programming class we did it to where you hit the letter n, or letter y, and when you did that, it would automatically either end the program, or loop back to the top and start the program over again. For example lets make something easy

    #include <iostream.h>

    int main()
    {
    int x, y;

    cout << "Enter a number: ";
    cin >> x;
    endl;
    cout << "Enter another number: ";
    cin >> y;
    endl;
    cout << "The sum of the two numbers is: << (x+y);
    endl;
    cout << "Run the addition caculator again? (y/n)";

    // this is where i need to put the selection between yes or no, and i forgot how to do it.


    }
    you can do this:
    Code:
    char choice;
    cout << "Run the addition caculator again? (y/n)";
    choice=getch();
    if ( choice == 'y' )
    //loop
    else if ( choice == 'n' )
    //exit
    else
    //not valid
    Is that what you want
    none...

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    3

    Thanks for all your help guys.

    Your input (all of you) helped me solve my problem. Thanks again! Come to think of it, C++ is easier than most people think. It just takes patience and persistence. Thanks everyone.

    Wes B

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 08:44 AM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01:08 PM
  5. Tic - Tac - Toe...
    By TheUnknowingOne in forum Game Programming
    Replies: 1
    Last Post: 09-10-2002, 06:01 AM

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