另一个printf问题!! [英] another printf question!!

查看:87
本文介绍了另一个printf问题!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了常见问题并查询了档案,但似乎无法让b / b
能够让这个工作。这是通常的阶乘递归

函数,但这不是问题。用户输入

后,它会挂起。但是,正如我所指出的,如果在

数字之后添加了其他内容,则该功能会继续并成功完成。


我将非常感谢一些有用的提示。

提前感谢。

I have looked at the faq and queried the archives, but cannot seem to
be able to get this to work. It''s the usual factorial recursive
function, but that is not the problem. It hangs after the user enters a
number. However, as I indicate, if one adds something else after the
number, the function proceeds and finishes successfully.

I would appreciate some helpful hints.
thanks in advance.

>>>>>


#include< stdio.h>

#include< math.h>


int factorial (int);

main(){


int i,k;


printf(" Please输入一个数字); //<<<<<<似乎挂在这里如果

用户只输入数字,但

//如果用户输入数字,例如8加上其他东西,例如字母

,则有效" m"

scanf("%d",& i);


k = factorial(i);

printf(%d的阶乘是%d \ n,i,k);


return(0);

}

int factorial(int j){

if(j == 1)


返回1 ;


返回j * factorial(j-1);


}
>>>>>
>>>>>
#include <stdio.h>
#include <math.h>

int factorial( int);
main() {

int i, k;

printf ( "Please enter a number "); // <<<<< seems to hang here if
user enters number only, but
// works if user enters number eg 8 plus something else eg the letter
"m".
scanf (" %d ", &i);

k = factorial (i);
printf ( " the factorial of %d is %d\n ", i, k);

return (0);

}
int factorial ( int j){
if (j == 1)

return 1;

return j * factorial(j-1);

}
>>>>>





推荐答案

从scanf调用中删除空格:

scanf("%d",& i);


David Barkol
www.neudesic.com

Remove the spaces from your scanf call:
scanf("%d", &i);

David Barkol
www.neudesic.com


David,

你是天才!!!但是为什么......我在C空间中的数量不计算?

David,
You are a genius!!! But why...I thougth in C spaces did not count?


scanf从标准输入流中读取格式化数据。这意味着

如果您的格式如下:"%d%d"然后scanf将不会返回

,直到您输入两个以空格分隔的数字。例如:24

14.处理格式时,空格总是很重要。

scanf reads formatted data from the standard input stream. This means
that if you have a format like: "%d %d" then scanf will not return
until you enter in two numbers seperated by a space. For example: 24
14. When dealing with formatting, spaces always count.


这篇关于另一个printf问题!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆