Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:11:48+00:00 2026-06-15T01:11:48+00:00

Please have a look at the following code #include <iostream> #include <stdlib.h> using namespace

  • 0

Please have a look at the following code

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    enum Movement{STAND,WALK,RUN,CRAWL};

    Movement state = (Movement)(1+rand()%4);

    for(int i=0;i<100;i++)
    {

    cout << state << endl;

    switch(state)
    {
        case STAND: 
            cout << "You can walk or crawl" << endl;        
            while(state==WALK || state==CRAWL){
            state = (Movement)(1+rand()%4);}
            break;


        case WALK: 
            cout << "You can stand or run" << endl;
            while(state==STAND || state==RUN){
            state = (Movement)(1+rand()%4);}
            break;


        case RUN: 
            cout << "You can walk" << endl;
            while(state==WALK){
            state = (Movement)(1+rand()%4);}
            break;

        default: 
            cout << "You can stand" << endl;
            while(state==STAND){
            state = (Movement)(1+rand()%4);}

    }

    }
}

In here, there are few rules. Lets call them as legal transitions

  1. From stand, he can walk or crawl
  2. From Walk, he can stand or run
  3. From Run, he can walk
  4. From Crawl, he can stand

So, here, if the legal transitions should be randomly selected. But, however, it should be according to the rules provided above. For an example, if “STAND” is selected, next thing to be selected should be “WALK” or “CRAWL” likewise. But as you can see here, all the result

2
You can walk

Why is that? Please help!

UPDATE:

Following code is with do..while loop, as suggested in a reply

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    enum Movement{STAND,WALK,RUN,CRAWL};

    Movement state = (Movement)(1+rand()%4);

    for(int i=0;i<10;i++)
    {

    cout << state;

    if(state==STAND)
    {
        cout << " STAND is selected" << endl;

    }
    else if(state==WALK)
    {
        cout << " WALK is selected" << endl;

    }
    else if(state==RUN)
    {
        cout << " RUN is selected" << endl;
    }
    else if(state==CRAWL)
    {
        cout << " CRAWL is selected" << endl;
    }

    switch(state)
    {
        case STAND: 
            //cout << "You can walk or crawl" << endl;        
            do{state = (Movement)(rand()%4);}
            while(state==WALK || state==CRAWL);

            break;


        case WALK: 
            //cout << "You can stand or run" << endl;
            do{state = (Movement)(rand()%4);}
            while(state==STAND || state==RUN);
            break;


        case RUN: 
            //cout << "You can walk" << endl;
            do{state = (Movement)(rand()%4);}
            while(state==WALK);
            break;

        default: 
            //cout << "You can stand" << endl;
            do{state = (Movement)(rand()%4);}
            while(state==STAND);

    }

    }
}

It is still the same. Now I get different answers, but not correct ones!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:11:49+00:00Added an answer on June 15, 2026 at 1:11 am

    Your logic is in the wrong order; your while loops will all be skipped because the state is still what it was when you entered the particular case. You might rewrite them with do-while to ensure they run at least once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code. Main.cpp #include <iostream> #include Maximum.h using
i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int
Please have a look at the following code #include <iostream> #include <stdlib.h> #include <ctime>
Please have a look at the following code #include <iostream> #include <stdlib.h> #include <ctime>
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.