3 Replies - 4219 Views - Last Post: 19 July 2011 - 02:58 PM Rate Topic: -----

#1 AndiW   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-July 11

Learning C++: Make switch 'default' loop to beginning of switc

Posted 19 July 2011 - 02:12 PM

Hi, I have recently started learning C++, but there is something I haven't learnt yet but it is confusing me. I am trying to make small sample applications and i just want to make sure i know how to cover every option available of from the start.

#include <iostream>

using namespace std;

int main()
{
    int input;

    cout<< "Please pick a number: \n\n";
    cout<< "1\n";
    cout<< "2\n";
    cout<< "3\n";
    cout<< "4\n";
    cout<< "\nSelection: ";
    cin>> input;
    cout<< "\n";

    switch (input) {
    case 1:
    cout<< "You have chosen number 1.\n";
    break;
    case 2:
    cout<< "You have chosen number 2.\n";
    break;
    case 3:
    cout<< "You have chosen number 3.\n";
    break;
    case 4:
    cout<< "You have chosen number 4.\n";
    break;
    default:
    cout<< "Invalid number entered.";
    // Here, I want a syntax that will send the program back to ask the "Please pick a number again"
    }
    cin.get();
}



Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Learning C++: Make switch 'default' loop to beginning of switc

#2 alke4   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 26-May 10

Re: Learning C++: Make switch 'default' loop to beginning of switc

Posted 19 July 2011 - 02:21 PM

To do this, simply place the Switch Case and choices within a loop of some kind, I suggest a Do-While loop. Have a choice that breaks from the loop when a particular choice is chosen. That would be how I would do that, if I understand your question correctly.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,785
  • Joined: 08-August 08

Re: Learning C++: Make switch 'default' loop to beginning of switc

Posted 19 July 2011 - 02:44 PM

Functions are your friends:
#include <iostream>

using namespace std;
int get_input();
bool test_input(int input);

int main()
{
    int input;
    do  {
        input = get_input();
    } while (!test_input(input));
    cin.get();
}

int get_input() {

    int input;
    
    cout<< "Please pick a number: \n\n";
    cout<< "1\n";
    cout<< "2\n";
    cout<< "3\n";
    cout<< "4\n";
    cout<< "\nSelection: ";
    cin >> input;
    cout<< "\n";
    return input; 
}

bool test_input(int input) {    
    switch (input) {
        case 1:
            cout<< "You have chosen number 1.\n";
            break;
        case 2:
            cout<< "You have chosen number 2.\n";
            break;
        case 3:
            cout<< "You have chosen number 3.\n";
            break;
        case 4:
            cout<< "You have chosen number 4.\n";
            break;
        default:
            cout<< "Invalid number entered.";
            return false;
    }
    return true;
}

This post has been edited by CTphpnwb: 19 July 2011 - 02:45 PM

Was This Post Helpful? 1
  • +
  • -

#4 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Learning C++: Make switch 'default' loop to beginning of switc

Posted 19 July 2011 - 02:58 PM

There are multiple ways to do what you want.

Here are two: First with a function through recursion and then with just a loop

Through function with recursion:
void checkOption(int num)
{

    switch(num)
    {
        case 1:
        cout << "Option 1" << endl;
        break;
        case 2:
        cout << "Option 2" << endl;
        break;
        case 3:
        cout << "Option 3"  << endl;
        break;
        default:
        cout << "Illegal option: please input valid 1-3" << endl;
        int value;
        cin >> value;
        checkOption(value);
        break;

    }

}

int main()
{

checkOption(4);


    return 0;
}



Through loop:
int main()
{

//checkOption(4);

bool reloop = true;
int num = 0;
while(reloop)
{
    cout << "Enter option 1-3" << endl;
 cin >> num;

    switch(num)
    {
        case 1:
        cout << "Option 1" << endl;
        reloop = false;
        break;
        case 2:
        reloop = false;
        cout << "Option 2" << endl;
        break;
        case 3:
        reloop = false;
        cout << "Option 3"  << endl;
        break;
        default:
        cout << "Illegal option: please input valid 1-3" << endl;
        int value;

        break;

    }

}

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1