当前位置:编程学习 > C/C++ >>

c语言中中getch()函数为何不起作用?

为了穷尽10个8之间只用加减剩除,而不用括号连接,所得的结果。因为我要在其中搜索某些结果,比如80,64,743,998等能不能被构造出来。我编制了如下程序,虽然可以得出所有的组合结果,但是即使我我在最后加一个getch();程序运行一会儿也会直接结束。以前我用getch()都能挺,怎么这儿没挺呢。难道是这儿的计算量太大了吗?????
程序如下:
#include<stdio.h>
void main()
{
float b[10],sum=0;
int i,p,m,a[10],c[10];
a[0]=1;b[0]=8;
for(a[1]=1;a[1]<=4;a[1]++)
for(a[2]=1;a[2]<=4;a[2]++)
for(a[3]=1;a[3]<=4;a[3]++)
for(a[4]=1;a[4]<=4;a[4]++)
for(a[5]=1;a[5]<=4;a[5]++)
for(a[6]=1;a[6]<=4;a[6]++)
for(a[7]=1;a[7]<=4;a[7]++)
for(a[8]=1;a[8]<=4;a[8]++)
for(a[9]=1;a[9]<=4;a[9]++)
{ for(i=0;i<=9;i++)c[i]=a[i];

for(i=1;i<=9;i++)

{if(a[i]==1||a[i]==3)b[i]=8;

else if(a[i]==2){b[i]=-8;a[i]=1;}

else {b[i]=1/8.0; a[i]=3;}

}
for(i=0;i<=9;i++)

{ m=i;

while(a[i]==3)i++;

for(p=m;p<i;p++)

{b[m-1]=b[m-1]*b[p];

b[p]=0;

}

}
for(i=0;i<=9;i++)
sum=b[i]+sum;
printf("%0.1f",sum);
for(i=0;i<=9;i++)a[i]=c[i];
sum=0;
}
getch();
}
答案:你共犯了三点原则性的错误!
第一点:
你既然用了getch()函数,在前面就应有头文件#include<conio.h>。因为:
conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数等等。
包含的函数
cgets(char *);
cprintf(const char *, ...);
cputs(const char *);
cscanf(const char *, ...);
inp(unsigned short);
inpw(unsigned short);
getch(void);
getche(void);
kbhit(void);
outp(unsigned short, int);
outpw(unsigned short, unsigned short);
putch(int);
ungetch(int);
void _Cdecl clreol (void);
void _Cdecl clrscr (void);
void _Cdecl delline (void);
int _Cdecl gettext (int left, int top, int right, int bottom,
void *destin);
void _Cdecl gettextinfo (struct text_info *r);
void _Cdecl gotoxy (int x, int y);
void _Cdecl highvideo (void);
void _Cdecl insline (void);
void _Cdecl lowvideo (void);
int _Cdecl movetext (int left, int top, int right, int bottom,
int destleft, int desttop);
void _Cdecl normvideo (void);
int _Cdecl puttext (int left, int top, int right, int bottom,
void *source);
void _Cdecl textattr (int newattr);
void _Cdecl textbackground (int newcolor);
void _Cdecl textcolor (int newcolor);
void _Cdecl textmode (int newmode);
int _Cdecl wherex (void);
int _Cdecl wherey (void);
void _Cdecl window (int left, int top, int right, int bottom);
har *_Cdecl cgets (char *str);
int _Cdecl cprintf (const char *format, ...);
int _Cdecl cputs (const char *str);
int _Cdecl cscanf (const char *format, ...);
int _Cdecl getch (void);
int _Cdecl getche (void);
char *_Cdecl getpass (const char *prompt);
int _Cdecl kbhit (void);
int _Cdecl putch (int c);
int _Cdecl ungetch (int ch);

第二点:
你没弄清getch()的用法。谨记如下:
getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回,getch返回值是用户输入的ASCII码,出错返回-1.输入的字符不会回显在屏幕上.getch函数常用于程序调试中,在调试时,在关键位置显示有关的结果以待查看,然后用getch函数暂停程序运行,当按任意键后程序继续运行.
第三点:
本次提问没有说清楚你编的程序想要干什么?
getch()函数需要头文件#include<conio.h>

上一个:关于C语言数据结构中的图的一些问题
下一个:C语言_SDK 屏蔽任务管理器的函数??

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,