Oct 20, 2009 at 5:42pm UTC
Im having problem with the output of this program that i made that supposed to count the numbers of successful a's and b's based on the pattern indicated in my condition.
*********************************************************************
#include<iostream>
using namespace std;
int main()
{
int lookahead;
int q1=0, q2=0, q3=0, q4=0, q5=0, q6=0;
int count_a=0;
int count_b=0;
while((lookahead = getchar())!='.')
{
switch(lookahead)
{
case 'a':{
if(q1==0 && q2==0 && q3==0 && q4==0 && q5==0 && q6==0) // 1 give a ~~> 2
{
q1=0;
q2=1;
q3=0;
q4=0;
q5=0;
q6=0;
}
else if ( q1==0 && q2==1 && q3==0 && q4==0 && q5==0 && q6==0 ) // 2 give a ~~> 3
{
q1=0;
q2=1;
q3=1;
q4=0;
q5=0;
q6=0;
}
else if ( q1==0 && q2==1 && q3==1 && q4==0 && q5==0 && q6==0) // 3 give a ~~> 6 then a++
{
q1=0;
q2=0;
q3=0;
q4=0;
q5=0;
q6=0;
count_a++;
}
else if ( q1==0 && q2==0 && q3==0 && q4==1 && q5==0 && q6==0) // if press a while in 4 2==1
{
q1=0;
q2=1;
q3=0;
q4=0;
q5=0;
q6=0;
}
else if (q1==0 && q2==0 && q3==0 && q4==1 && q5==1 && q6==0) // if press a while in 4==1 && 5==1 ~~> 2==1
{
q1=0;
q2=1;
q3=0;
q4=0;
q5=0;
q6=0;
}
}
break;
case 'b':{
if(q1==0 && q2==0 && q3==0 && q4==0 && q5==0 && q6==0) // 1 give b ~~> 4
{
q1=0;
q2=0;
q3=0;
q4=1;
q5=0;
q6=0;
}
else if ( q1==0 && q2==0 && q3==0 && q4==1 && q5==0 && q6==0 ) // 4 give b ~~> 3
{
q1=0;
q2=0;
q3=0;
q4=1;
q5=1;
q6=0;
}
else if ( q1==0 && q2==0 && q3==0 && q4==1 && q5==1 && q6==0) // 5 give b ~~> 6 then a++
{
q1=0;
q2=0;
q3=0;
q4=0;
q5=0;
q6=0;
count_b++;
}
else if ( q1==0 && q2==1 && q3==0 && q4==0 && q5==0 && q6==0) // if press b while in 2 4==1
{
q1=0;
q2=0;
q3=0;
q4=1;
q5=0;
q6=0;
}
else if (q1==0 && q2==1 && q3==1 && q4==0 && q5==0 && q6==0) // if press a while in 2==1 && 3==1 ~~> 4==1
{
q1=0;
q2=0;
q3=0;
q4=1;
q5=0;
q6=0;
}
}
break;
default:{
q1=0;
q2=0;
q3=0;
q4=0;
q5=0;
q6=0;
}
}
cout<<"\n count_a :"<<count_a;
cout<<"\n count_b :"<<count_b;
}
return 0;
}
*****************************************************************
when i didnt enter any letter the output is:
count_a:0
count_b:0
when the letter increase the number of count display multiplies too and it shouldnt'..
like this; input words "aaa"
count_a:0
count_b:0
count_a:0
count_b:0
count_a:1
count_b:0
count_a:1
count_b:0
Where do you think is the problem on my codes?
Oct 20, 2009 at 6:34pm UTC
Use the debugger! I really have no idea what this program does but the output you are getting looks good to me based on what you have told me. You said you entered 3 a's but did you enter the '.' to end the loop? This should be very simple to solve with the debugger.
Oct 20, 2009 at 6:38pm UTC
the exact output for "aaa" should be"
count_a: 1
count_b: 0
if "abbb"
count_a: 0
count_b: 1
etc.
Its a program for a Finite State Machine that accept a language that has 3 consecutive letters in the end.
Oct 20, 2009 at 6:42pm UTC
sorry a 3 consecutive same letters at the end of the an expressions.... and every time it met the condition count_a or _b will increment....
ill try the '.'.. tnx ^_^
Oct 20, 2009 at 6:54pm UTC
You're expectations are wrong. The cout executes at the bottom of EVERY SINGLE LOOP. If you enter 3 characters, the loop runs three times and prints the values. It's that simple.
Oct 20, 2009 at 6:58pm UTC
oooohh really! i didnt know that .... im still learning c++ for almost 2 months now...
What should i used instead of cout? thank you for that ^_^
Oct 20, 2009 at 7:16pm UTC
i dont know what to used rather than cout.. and im still learning this language..... pls pls pls help me...
should i put each cout after every single loop? or add/remove some codes in there?