新手求助 printf没有输出

winchester丶 2018-06-20 11:36:51
#include <stdio.h>

int main(){
char t='0';
while(t!='.'){
int cnt=0;
while(t!=' '){
cnt++;
scanf("%s",t);
if(t=='.'){
printf("%d\n",cnt);
goto out;
}
}
printf("%d\n",cnt);
scanf("%s",&t);
}
out:
return 0;
} 不知道那里错了,但是cnt不能打印出来,求解答
...全文
272 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-06-21
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
#if 1
int cnt = 0;
char t;

while (scanf("%c", &t) == 1 && t != '.') {
cnt++;
if (t == ' ') {
printf("%d\n", cnt);
cnt = 0;
}
}
printf("%d\n", cnt);
#else
char t=' ';
while(t != '.'){
int cnt=0;
while(t != ' '){
cnt++;
scanf("%c", &t);
if(t=='.'){
printf("%d\n",cnt);
goto out;
}
}
printf("++%d\n",cnt);
scanf("%c", &t);
}
out:
#endif
return 0;
}

参考一下吧
棉猴 2018-06-21
  • 打赏
  • 举报
回复
上面笔误,将
scanf("%s",&t);

改为
scanf("%c",&t);
棉猴 2018-06-21
  • 打赏
  • 举报
回复
确实,正如楼上几位所说的那样,感觉问题主要处在对scanf()这个函数的使用上。这个函数的格式之一为
int scanf(const char* format, argument);

其中,第一个参数format是格式化控制字符串,指定了以什么样的格式读取输入的数据,格式化字符串的基本格式为“%type”,其中type是单个的字母,%s表示以字符串的方式读取数据,%c表示以字符的方式读取数据。由于t的类型是char,所以在读取t时需要将格式化字符串设置为%c而不是%s。
所以,需要修改的代码为
scanf("%s",&t);

改为
scanf("%c",&t);

因为scanf()函数的第二个参数必须是指针,所以还需要修改的代码为
scanf("%s",t);

改为
scanf("%s",&t);
Aist-memory 2018-06-21
  • 打赏
  • 举报
回复
两个scanf改为 scanf("%c", &t);
老马何以识途 2018-06-21
  • 打赏
  • 举报
回复
給t的定義只是一個char,卻以字符串進行輸入,這裏就溢出了。
起碼要定義成一個字符數組,比如


char t[32] = '0';


另外前後兩個scanf的寫法也不一樣,要糾正一下。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧