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

  • SEARCH
  • Home
  • 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 9245051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:10:43+00:00 2026-06-18T09:10:43+00:00

Following is a simple program which implements a DFA(deterministic finite automata). However, my problem

  • 0

Following is a simple program which implements a DFA(deterministic finite automata). However, my problem does not involve DFA.

#include<iostream>
using namespace std;

int main()
{
    char c;
    int state=1;
    while((c=cin.get())!='4')
    {
        switch(state)
        {
            case 1:
            if(c=='0')
            state=2;
            if(c=='1')
            state=1;
            if(c=='2')
            state=2;

            break;

            case 2:
            if(c=='0')
            state=5;
            if(c=='1')
            state=1;
            if(c=='2')
            state=3;

            break;

            case 3:
            if(c=='0')
            state=1;
            if(c=='1')
            state=5;
            if(c=='2')
            state=4;

            break;

            case 4:
            if(c=='0')
            state=3;
            if(c=='1')
            state=4;
            if(c=='2')
            state=5;

            break;

            case 5:
            if(c=='0')
            state=5;
            if(c=='1')
            state=4;
            if(c=='2')
            state=1;

            break;

            default:
            cout<<"Input will not be accepted"<<endl;

        } //switch
        cout<<"Current state is "<<state<<endl; 
    } //while


    return 0;
}

When I ran the code, I found that every line was being output twice. For, example when I entered 0 1 0 0 4, the DFA goes from state 1->2->1->2->5 and thus output should be:

Current state is 2
Current state is 1
Current state is 2
Current state is 5

But the output is:

Current state is 2
Current state is 2
Current state is 1
Current state is 1
Current state is 2
Current state is 2
Current state is 5
Current state is 5

Can anyone point out the cause?

  • 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-18T09:10:45+00:00Added an answer on June 18, 2026 at 9:10 am

    cin.get() reads one character, so you are also reading spaces. Then after each space your program just outputs previous state because space doesn’t match anything. You want to use cin >> c instead.

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

Sidebar

Related Questions

I successfully compiled the following program simple.cc: #include <gtkmm.h> int main (int argc, char
I need to write a simple program for work that does the following: read
I've just started programming in Qt framework. Following is a very simple program: #include
i have the following problem. The 1st step is to implement a program, which
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Based on the following simple program the bitwise left shift operator works only for
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I cannot figure out why the following simple program doesn't (create) and then write
I am new to learning MPI and I coded up the following simple program
I considered the following experinment: simple C program, that only return 0, but linked

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.