linkui
شنبه 11 آبان 1392, 21:20 عصر
سلام من یه برنامه نوشتم که اگر کاربر zczc نوشت بعد از اون پیغامش رو بنویسه و بعد اگه nnnn زد پیغامش نمایش داده بشه ولی نمیدونم اگه کاربر دکمه های اشتباه زد چطور از برنامه خارج بشه .
یعنی باید بنویسه :
zczc hello world nnnn
خروجی :
hello world
ولی شاید کاربر بنویسه :
tctc hello world zzzz
میخوام تا کاراکتر اول رو اشتباه زد از برنامه بیاد بیرون یا ارور بده اینم سورسش :چشمک:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <process.h>
main()
{
clrscr();
char input;
char msg[500]="";
int state=0,i;
for (i=0;i<4;i++)
{
input=getch();
switch (state)
{
case 0:
if (input=='z')
{
state=1;
break;
}
case 1:
if (input=='c')
state=2;
else
cout <<"\n You Pressed Wrong Key \n"; break;
state=0;
break;
case 2:
if (input=='z')
state=3;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 3:
if(input=='c')
state=4;
break;
}
cout <<input;
}
/*================================================= ========*/
cout <<"\n Now Type Your Message ";
gotoxy(1,7);
cin.getline(msg,500);
/*================================================= =========*/
for (i=0;i<4;i++)
{
input=getch();
switch (state)
{
case 0:
if (input=='n')
{
state=1;
break;
}
case 1:
if (input=='n')
state=2;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 2:
if (input=='n')
state=3;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 4:
if(input=='n')
{
state=4;
cout <<input;
break;
}
else
{
cout <<"\n You Pressed Wrong Key \n";
}
}
cout <<input;
}
/*================================================= =========*/
textcolor (1);
cprintf("\n Your Message Is : ");
textcolor (10);
cprintf(msg);
textcolor (500);
cprintf("\r\n Press Any Key To Exit ");
getch();
}
حالا دوستان کسی میدونه من باید چیکار کنم؟
یعنی باید بنویسه :
zczc hello world nnnn
خروجی :
hello world
ولی شاید کاربر بنویسه :
tctc hello world zzzz
میخوام تا کاراکتر اول رو اشتباه زد از برنامه بیاد بیرون یا ارور بده اینم سورسش :چشمک:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <process.h>
main()
{
clrscr();
char input;
char msg[500]="";
int state=0,i;
for (i=0;i<4;i++)
{
input=getch();
switch (state)
{
case 0:
if (input=='z')
{
state=1;
break;
}
case 1:
if (input=='c')
state=2;
else
cout <<"\n You Pressed Wrong Key \n"; break;
state=0;
break;
case 2:
if (input=='z')
state=3;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 3:
if(input=='c')
state=4;
break;
}
cout <<input;
}
/*================================================= ========*/
cout <<"\n Now Type Your Message ";
gotoxy(1,7);
cin.getline(msg,500);
/*================================================= =========*/
for (i=0;i<4;i++)
{
input=getch();
switch (state)
{
case 0:
if (input=='n')
{
state=1;
break;
}
case 1:
if (input=='n')
state=2;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 2:
if (input=='n')
state=3;
else
cout <<"\n You Pressed Wrong Key \n";
state=0;
break;
case 4:
if(input=='n')
{
state=4;
cout <<input;
break;
}
else
{
cout <<"\n You Pressed Wrong Key \n";
}
}
cout <<input;
}
/*================================================= =========*/
textcolor (1);
cprintf("\n Your Message Is : ");
textcolor (10);
cprintf(msg);
textcolor (500);
cprintf("\r\n Press Any Key To Exit ");
getch();
}
حالا دوستان کسی میدونه من باید چیکار کنم؟