Ramos801 2022-06-05 23:52 采纳率: 85.7%
浏览 38
已结题

C语言while循环问题

#include <stdio.h>
#include <stdlib.h> // for atoi

#define SIZE 10
int length(char arr[])
{
int i=0;
while(arr[i]!='\0'){
i++;
}
return i;
}
int power(int base, int n){
int result =1;
while (n != 0){
result *= base;
--n;
}

return result;    
}

int my_atoi (char c[])
{

int i =length(c)-1;
int change =0;
int cti =0;
int count=length(c)-1;
while (i>=0){
change=c[i]-'0';

cti=cti+change*power(10,count);
count--;
i--;

}
return cti;

}
int isQuit (char arr[])
{
int i;
char x []="quit";
if (length(arr)<4||length(arr)>4){
return 0;
}
for (i=0;arr[i]!='\0';i++){
if (arr[i]==x[i])
return 1;
}

return 0;

}

int main(){
int a,b,c;

char arr [SIZE];

printf("Enter a word of positive number or 'quit': " );
scanf("%s", arr);
while( isQuit (arr)==0)
{
printf("%s\n", arr);

a = atoi(arr);
printf("atoi:    %d (%#o, %#X)\t%d\t%d\n", a,a,a, a*2, a*a);

b = my_atoi(arr);
printf("my_atoi: %d (%#o, %#X)\t%d\t%d\n", b,b,b, b*2, b*b);

break;

}

return 0;

}这个代码运行之后为什么只会运行一次,而不是第二张图片所示呀,请问如何达到第二张的效果

img

img

  • 写回答

2条回答 默认 最新

  • WaitIKnowYou 2022-06-06 00:21
    关注

    你的输入语句没写在循环里 还应该删除break

    do
    {
    scanf("%s", arr);
    printf("%s\n", arr);

    a = atoi(arr);

    printf("atoi: %d (%#o, %#X)\t%d\t%d\n", a,a,a, a2, aa);

    b = my_atoi(arr);

    printf("my_atoi: %d (%#o, %#X)\t%d\t%d\n", b,b,b, b2, bb);
    }while( isQuit (arr)==0);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 创建了问题 6月5日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵