首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为什么有时候只要小弟我第一次输错了,接上来都是输错的,咋回事?

2012-07-22 
为什么有时候只要我第一次输错了,接下来都是输错的,怎么回事??C/C++ code#includeiostream.h#includest

为什么有时候只要我第一次输错了,接下来都是输错的,怎么回事??

C/C++ code
#include<iostream.h>#include<string.h>int main(){    char a[7];    int i = 0;    while(1)    {        memset(a,0,sizeof(a));        cout<<"请输入名字:"<<endl;        for(i = 0;i < sizeof(a) - 1;i++)            cin>>a[i];        a[i] = '\0';        if(!strcmp(a,"哈哈哈"))            cout<<"哈哈哈\n"<<endl;        else if(!strcmp(a,"哦哦哦"))            cout<<"哦哦哦\n"<<endl;        else             cout<<"输入错误\n"<<endl;        }    return 0;}


请输入名字:
aaaaaaa
输入错误

请输入名字:
哈哈哈
输入错误

请输入名字:
哦哦哦
输入错误


请输入名字:
哈哈哈
哈哈哈

请输入名字:
哦哦哦
哦哦哦

请输入名字:
大声的撒
输入错误

请输入名字:
哈哈哈
输入错误

请输入名字:

[解决办法]
给你的代码改了下,用C++的iostream 好用了
C/C++ code
//C/C++ code#include<iostream>#include<string.h>#include <stdio.h>using namespace std;int main(){    char a[7];    int i = 0;    while(1)    {        memset(a,0,sizeof(12));        cout<<"请输入名字:"<<endl;        for(i = 0;i < sizeof(a) - 1;i++)            cin>>a[i];        a[i] = '\0';        if(!strncmp(a,"哈哈哈",6))            cout<<"哈哈哈\n"<<endl;        else if(!strncmp(a,"哦哦哦",6))            cout<<"哦哦哦\n"<<endl;        else            cout<<"输入错误\n"<<endl;        while(getchar()!='\n');       // system("pause");    }    return 0;} 

热点排行