当前位置: 代码迷 >> C语言 >> 一天一夜未眠,还是找不出原因,救救我吧
  详细解决方案

一天一夜未眠,还是找不出原因,救救我吧

热度:295   发布时间:2005-07-18 15:24:00.0
一天一夜未眠,还是找不出原因,救救我吧

输入用户名和密码,[(Z,3333),(L,4444),(W,5555),(S,6666)] 如果用户名没有里面的就提示用户名错误,重新输入。如果密码错误,重新输入。直到用户名和密码都正确为止: #include<stdio.h> main() {char c; int i=0, n; clrscr(); printf("Wellcome to elective system \n"); while(i==0) /*检验用户名是否存在*/ {printf("please enter name:"); scanf("%c",&c); printf("\n"); printf("please enter password:"); scanf("%d",&n); printf("\n"); switch(c) /*如果存在,给 i 赋值*/ {case ''Z'' : i=3333; break; case ''L'' : i=4444; break; case ''W'' : i=5555; break; case ''S'' : i=6666; break; default : printf("the consumer is error\n"); } } while (n!=i) /*检验密码是否正确*/ {printf("password is error\n"); printf("Enter password again:"); scanf("%d",&n) ; }

printf("Susses Enter System\n"); } 当我输入用户名A和密码6666时 跳出 the consumer is error please enter name: please enter password: 问题出现在这里,为什么用户名和密码一起出现,不是先出现“please enter name” 等我输入用户名后再跳出“please enter password:”吗 请按我的输入顺序去输入,就知道我的意思了 (A↓ 6666↓ Z↓ 6666↓ 3333↓ ) 也可以看我上传上去的附件 还有,要是输入改成 (A↓ 6666↓ Z↓ 6666↓ A↓)会出现另一种结果,去试试看

document.body.clientWidth*0.5) {this.resized=true;this.width=document.body.clientWidth*0.5;this.style.cursor='pointer';} else {this.onclick=null}" alt="" />
搜索更多相关的解决方案: 未眠  

----------------解决方案--------------------------------------------------------
修改后的源代码和编译后的程序:
[attach]2611[/attach]

你的错误是字符要用单引号括起来,字符串才是用双引号括。还有就是你的scanf()函数的用法,详细情况请看knocker的“scanf释疑”一文

[此贴子已经被作者于2005-7-18 16:35:13编辑过]



----------------解决方案--------------------------------------------------------
强,果然是高手
请问
fflush(stdin);
这个是什么意思
是什么功能
谢谢啦
没有QQ,能不能加我
----------------解决方案--------------------------------------------------------
fflush的功能是清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
stdin是流的指针。
(快点去买本C/C++库函数词典~~~这种纸书读起来就是比电子版的有感觉,如果诚心诚意想学编程,就不要吝啬那几十元钱)
----------------解决方案--------------------------------------------------------
要是把switch里面的名字改成字符串如'Z'[改成  "Zhang san"
电脑提示需要用常量表达式
那是不是不能改了
要改的话,怎么改
----------------解决方案--------------------------------------------------------
使用字符数组,或者使用链表
----------------解决方案--------------------------------------------------------
也可以另外定义一个整形变量来对应字符,
如:int N;
输入'Z'--&gt;N=0;输入'L'--&gt;N=1;。。。。
switch(N);
{......
....
default;}

----------------解决方案--------------------------------------------------------
#include&lt;stdio.h&gt;
main()
{char c; int i=0, n;
clrscr();
printf("Wellcome to elective system \n");
while(i==0) /*检验用户名是否存在*/
{printf("please enter name:");
scanf("%c",&amp;c);
printf("\n");
printf("please enter password:");
scanf("%d",&amp;n);
printf("\n");
switch(c) /*如果存在,给 i 赋值*/
{case 'Z' : i=3333; break;
case 'L' : i=4444; break;
case 'W' : i=5555; break;
case 'S' : i=6666; break;
default : printf("the consumer is error\n");
}
}
while (n!=i) /*检验密码是否正确*/
{printf("password is error\n");
printf("Enter password again:");
scanf("%d",&amp;n) ;
}printf("Susses Enter System\n");
}

兄弟你要搞清楚,双引号和单引号的区别!!!!!
----------------解决方案--------------------------------------------------------
楼上的,他的问题我已经回答过了,而且你也没改对,他的错误不止这一个,scanf()函数也用错了,详细的看2楼
----------------解决方案--------------------------------------------------------
楼上的,其实scanf()这个函数没有用错,你在这函数前加了fflush(stdin)这个库函数我不知道是什么用处,好像介绍说是用来清除一个流,我不知道为什么这里要用到这个,这里并没有涉及到流的问题。
----------------解决方案--------------------------------------------------------
  相关解决方案