Jump to content

C++, How to manage the logic..


Recommended Posts

I am totally lost here...

    std::string choices = "You can:\n1: Go to the police station.\n2: Call the police station.\n3: Throw away the mail you got from the police station.\n\n";
    std::cout<<choices;

    int response;
    std::cin>>response;
    std::cin.ignore();
    switch(response) {
        case 1:
            std::cout<<"go to police station";
            break;
        case 2:
            std::cout<<"call the police station";
            break;
        case 3:
            std::cout<<"With a heavy heart you realize you just are not cut out to be a police officer. You crumble the letter up, roll it up into a"
                <<" ball and throw it into the garbage.";
            exit(1);
            break;
        default:
            std::cout<<"Invalid Choice";
            std::cout<<choices;
            break;
    }

Very straightforward. Prevent choice..based off their choices perform actions...now what happens if they perform an invalid choice. It reloads the choices..but then exits out of the system.  From a logic standpoint..how would I present the list of choices again, as well as let the switch statement take affect again when a choice was selected the second time..In PHP for example your able to reshow a form if it's been entered incorrectly and still submit...do I have to put that switch statement into a function as well...but there are going to be tons of switch statements throughout this entire application..I am wondering if I have to have a separate one for each set of choices?

 

Any advice on this issue?

Link to comment
Share on other sites

Usually you do this with while()

 

validChoice = false;

while(!validChoice) {
  switch(response) {
    case 1:
      std::cout<<"go to police station";
      validChoice = true;
      break;
    case 2:
      std::cout<<"call the police station";
      validChoice = true;
      break;
    default:
      std::cout<<"Invalid Choice";
      std::cout<<choices;
      break;    
  }
}

Link to comment
Share on other sites

Wait..I am very, very confused now. I thought a C++ application was like a PHP application...Meaning it went line by line and ran the code as it went.  Now based off this code...it works differently?

 

I got a little lost here.  However I implemented your code, and it runs it forever..like the while loop keeps going eternally and reshowing the choices over and over again very rapidly.

 

Not to mention when I do it like this:

    bool validChoice = false;
    while(!validChoice) {
        switch(response) {
            case 1:
                std::cout<<"go to police station";
                validChoice = true;
                break;
            case 2:
                std::cout<<"call the police station";
                validChoice = true;
                break;
            case 3:
                std::cout<<"With a heavy heart you realize you just are not cut out to be a police officer. You crumble the letter up, roll it up into a"
                         <<" ball and throw it into the garbage.";
                exit(1);
                break;
            default:
                std::cout<<"Invalid Choice";
                std::cout<<choices;
        }
        std::cin.get();
    }

It does work to an extent...however it doesn't reflect the choices..so the first time if they select 6, which is an invalid choice..

then they select 1 it still goes as an invalid choices...it doesn't replay that switch statement.  If that makes any sense.

I was afraid of that, I know the while look will allow me to reshow the same choices but won't let me rerun the same switch statement.

Link to comment
Share on other sites

Reading from cin should also be within while loop :P

 

    bool validChoice = false;
    while(!validChoice) {
      std::cin>>response;
        switch(response) {
            case 1:
                std::cout<<"go to police station";
                validChoice = true;
                break;
            case 2:
                std::cout<<"call the police station";
                validChoice = true;
                break;
            case 3:
                std::cout<<"With a heavy heart you realize you just are not cut out to be a police officer. You crumble the letter up, roll it up into a"
                         <<" ball and throw it into the garbage.";
                exit(1);
                break;
            default:
                std::cout<<"Invalid Choice";
                std::cout<<choices;
        }
        std::cin.get();
    }

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.