清霜一梦

while执行两次的问题,已经解决

0
阅读(889)

//2013 5 11

//designer :pengxiaoen

// function change the number 'd' to 'b'

#include "stdio.h"

unsigned long int in_un_number;

char in_c_choice; int s[33];

int i;

void output_function ()

{

printf ("\nSUCCESS\n") ;

printf ("%d = (%d)",in_un_number,i);

for (i=i-1;i>=0;i--) printf ("%d",s[i]); printf ("\n\n\n");

}

void choice_function ()

{

printf ("please choice next operation:\n");

printf ("'c' for contiue\n");

}

void transform_function(unsigned long int temp)

{

i = 0;

temp = in_un_number;

while (temp != 0)

{

s[i] = temp % 2;

temp = temp /2;

i++;

}

}

int main ()

{

in_c_choice = 'c';

for (i=33;i>=0;i--) s[i] = 0;

printf ("The function is change 'd' to 'b'\n");

//-------------------------------------------------------------

while (in_c_choice == 'c')

{

printf ("Please enter your positive number : ");

scanf("%d",&in_un_number);

transform_function(in_un_number);

output_function();

choice_function();

scanf("%d",&in_c_choice);

}

system ("pause");

}

第一次键入值正确

可是当我再键入 c 的时候就出现了死循环

不知道为什么?????????????????????????????????????????????????????????

修改:

将scanf("%d",&in_c_choice);和choice_functon注释掉,正常

问题解决办法:

因为第二次执行scanf的时候,读取内存数据,由于上一次的残留,程序以外其中有数,输入完毕

解决办法 在scanf之前 清除缓存数据

fflush(stdin);


Baidu
map