|
查看: 1460|回复: 17
|
C program assignment遇到问题!!
[复制链接]
|
|
|
本帖最后由 yongjie1990 于 30-3-2012 10:08 PM 编辑
现在遇到很小的bug~但不会解决~我的概念不大好~其实才接触C一个月
lecturer有拖拖拉拉~又不教东西~
所以都是自学~希望各位大大帮个忙~
就是遇到这个问题问题就是当我在relogin的时候就会出现这个bug~
如何解决呢?
代码如下- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- void login();
- int mainmenu();
- main()
- {
- login();
- }
- int mainmenu ()
- {
- int menu;
- printf("MENU\n");
- printf("1.PriceList\n2.Customer Order\n3.Generate Bills\n4.Logout\n5.Quit the system\n");
- printf("Please key in your choice:");
- scanf("%d",&menu);
- switch(menu)
- {
- case 1:printf("hello");
- break;
- case 2:printf("hello");
- break;
- case 3:printf("hello");
- break;
- case 4:system("cls");{login();}break;
- case 5:exit(0);
- default:printf("Error\n");
- system("cls");
- printf("please key in correr number!!\n\n");
- }
- return 0;
- }
- void login()
- {
- char name[10];
- char pass[10];
- printf("Welcome to A&B billing system\n");
- loop:printf("Username:");
- gets(name);
- {
- if((strcmp(name,"yongjie")==0)||(strcmp(name,"jayson")==0)||(strcmp(name,"scott")==0))
- {goto password;}
- else
- {
- printf("Invalid Usename!!\n");
- goto loop;
- }}
- password:
- printf("Password:");
-
- gets(pass);{
- if(strcmp(pass,"1234")==0)
- {system("cls");
- {mainmenu();}}
- else
- {
- printf("Invalid Password!!\n");
- goto loop;
- }}
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 30-3-2012 10:52 PM
|
显示全部楼层
scanf("%d",&menu);
getchar();
你在scanf后按了一下enter. gets 就将这个空格当作你的input,所以第一个loop来到gets时没停下就跳去Invalid Usename。 在scanf后用getchar吃掉这个enter就可以了.
C language 不提倡使用goto 跟 gets, 它们都是不安全的. 所有不该犯错误的你全都犯了. 你是那间学校的? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 30-3-2012 10:58 PM
|
显示全部楼层
本帖最后由 yongjie1990 于 30-3-2012 11:02 PM 编辑
scanf("%d",&menu);
getchar();
你在scanf后按了一下enter. gets 就将这个空格当作你的input,所以第一 ...
波多野飞机 发表于 30-3-2012 10:52 PM 
原来~
我是APxxT的~
现在还在diploma罢了~老师目前都没教到那么多~全是自学+google的~
酱gets 和 goto不用的话可以运用那种?还有可以列出我的错误吗??
谢谢啊~ |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 03:50 AM
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void login();
int password();
int mainmenu();
main()
{
login();
}
int mainmenu (){
int menu;
printf("MENU\n");
printf("1.PriceList\n2.Customer Order\n3.Generate Bills\n4.Logout\n5.Quit the system\n");
printf("Please key in your choice:");
scanf("%d",&menu);
switch(menu)
{
case 1:printf("hello");
break;
case 2:printf("hello");
break;
case 3:printf("hello");
break;
case 4:system("cls");
login();
break;
case 5:exit(0);
default:printf("Error\n");
system("cls");
printf("please key in correr number!!\n\n");
}
getch();
return 0;
}
void login(){
char name[10];
int counter = 0;
printf("Welcome to A&B billing system\n");
do{
printf("Username: ");
gets(name);
if((strcmp(name,"yongjie")==0)||(strcmp(name,"jayson")==0)||(strcmp(name,"scott")==0)){
counter = password();
}else{
printf("Invalid Usename!!\n");
}
}while(counter == 0);
mainmenu();
}
int password(){
char pass[10];
int counter = 0;
printf("Password:");
gets(pass);
if(strcmp(pass,"1234")==0){
return 1;
}else{
printf("Invalid Password!!\n");
return 0;
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 01:14 PM
|
显示全部楼层
电脑语言, 10%靠老师, 90%是靠自己努力去实习拿经验..
google search 是 现今 programmer 必用的武器..
它能解决你99%的问题.. 当然也要你会去用 key 去 search..
所以, 学用 google search 也是 programmer 的功课之一.. |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 02:23 PM
|
显示全部楼层
回复 4# _Idiot_
你只是改了原码的风格,并没有解决它的bug.
不过,你回答了lz的疑问, 应该尽量用Structured programming取代goto. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-4-2012 10:31 PM
|
显示全部楼层
电脑语言, 10%靠老师, 90%是靠自己努力去实习拿经验..
google search 是 现今 programmer 必用的武器..
...
jasonmun 发表于 1-4-2012 01:14 PM 
谢谢指导~
我有用google~只不过不懂如何寻找我这种类型的key去search~也许我笨吧~>< |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-4-2012 10:32 PM
|
显示全部楼层
#include
#include
#include
#include
void login();
int password();
int mainmenu();
main()
{ ...
_Idiot_ 发表于 1-4-2012 03:50 AM 
谢谢你的编码~
我会好好的去了解~
说实在do while 我不怎么会去运用~ |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 10:42 PM
|
显示全部楼层
|
不好意思,LZ我没写错的话你的bug已经解决了。试了告诉我, 因为波多野飞机说bug还没解决。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-4-2012 10:47 PM
|
显示全部楼层
本帖最后由 yongjie1990 于 1-4-2012 10:50 PM 编辑
不好意思,LZ我没写错的话你的bug已经解决了。试了告诉我, 因为波多野飞机说bug还没解决。
_Idiot_ 发表于 1-4-2012 10:42 PM 
没关系~多希望有多几个版本出来~好让我研究研究~学习学习~好的~明天试了给你知~ |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 11:22 PM
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void login();
int password();
int mainmenu();
main()
{
login();
}
int mainmenu (){
int menu;
printf("MENU\n");
printf("1.PriceList\n2.Customer Order\n3.Generate Bills\n4.Logout\n5.Quit the system\n");
printf("Please key in your choice:");
scanf("%d",&menu);
getchar();
switch(menu)
{
case 1:printf("hello");
break;
case 2:printf("hello");
break;
case 3:printf("hello");
break;
case 4:system("cls");
login();
break;
case 5:exit(0);
default:printf("Error\n");
system("cls");
printf("please key in correr number!!\n\n");
}
getch();
return 0;
}
void login(){
char name[10];
int counter = 0;
printf("Welcome to A&B billing system\n");
do{
printf("Username: ");
gets(name);
if((strcmp(name,"yongjie")==0)||(strcmp(name,"jayson")==0)||(strcmp(name,"scott")==0)){
counter = password();
}else{
printf("Invalid Usename!!\n");
}
}while(counter == 0);
mainmenu();
}
int password(){
char pass[10];
int counter = 0;
printf("Password:");
gets(pass);
if(strcmp(pass,"1234")==0){
return 1;
}else{
printf("Invalid Password!!\n");
return 0;
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 11:23 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 1-4-2012 11:24 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 2-4-2012 10:35 AM
|
显示全部楼层
本帖最后由 波多野飞机 于 2-4-2012 10:58 AM 编辑
波多野飞机, 对不起,我知错了。谢谢
_Idiot_ 发表于 1-4-2012 11:24 PM 
你言重了,大家互相学习而已。
LZ要记住一点, 用google search 找答案是好的。 但也不要完全相信网上的答案,老师甚至书本.
要相信你的compiler debugger. |
|
|
|
|
|
|
|
|
|
|
发表于 2-4-2012 03:27 PM
|
显示全部楼层
朋友,不要用goto,这样只会给你带来更多的logic error。goto statement基本上在programming里是被ban的。
善用for loop,while loop 和 do...while loop。 |
|
|
|
|
|
|
|
|
|
|
发表于 2-4-2012 05:15 PM
|
显示全部楼层
朋友,不要用goto,这样只会给你带来更多的logic error。goto statement基本上在programming里是被ban的。
...
andy5627 发表于 2-4-2012 03:27 PM 
没你讲的那么严重啦. goto只是不被建议使用,并没被ban. c++从c 继承过来, 如果被ban, 那么发明c++的人应该拿掉goto才对. c#又从c++改进过来,它也保留了goto
goto 也是有用的. 比如说你要从一个进到多层次的loop直接跳到最外层, 用break的话只能一层层跳, 这时候goto就适用了
lz 的例子使用goto是毫无必要的 |
|
|
|
|
|
|
|
|
|
|
发表于 3-4-2012 12:30 PM
|
显示全部楼层
没你讲的那么严重啦. goto只是不被建议使用,并没被ban. c++从c 继承过来, 如果被ban, 那么发明c++的 ...
波多野飞机 发表于 2-4-2012 05:15 PM 
当有nested loop时,就必须善用boolean和function来控制了。不可以用goto的说。
在structured programming里,goto是完全不可出现的。
goto还被保留到现在,的确是有它的用处,但它的用处是很小很小很小。
比如Java里已经没有了goto statement。
http://www.springerlink.com/content/mg1l4u7554004314/ |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 3-4-2012 09:34 PM
|
显示全部楼层
回复 17# andy5627
谢谢两位的指教~ 越来越不明你们讲什么了 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|