473,585 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

another printf question!!

drM
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 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);

}
>>>>>


Nov 14 '05 #1
12 1804
Remove the spaces from your scanf call:
scanf("%d", &i);

David Barkol
www.neudesic.com

Nov 14 '05 #2
drM
David,
You are a genius!!! But why...I thougth in C spaces did not count?

Nov 14 '05 #3
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.

Nov 14 '05 #4
drM
thank you,

Nov 14 '05 #5
On 2 Feb 2005 14:24:08 -0800, "drM" <md**@comcast.n et> wrote:
David,
You are a genius!!! But why...I thougth in C spaces did not count?


Spaces in quotations are significant. In your scanf call, you could
write
scanf ( "%d " , &i );

without making a difference (except to the unfortunate person reading
it),.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #6
drM
It makes sense now...all part of the learning curve. Thanks for your
input.

Nov 14 '05 #7
drM wrote:
thank you,


Please learn to use the correct reply method when using the Google
interface. Click "show options" to expand the header of the message you
are replying to, then the Reply shown there. Don't use the one at the
bottom of the message. You'll then have proper attributions and quotes,
so people can understand what you are refering to.


Brian

Nov 14 '05 #8
On 2 Feb 2005 14:45:11 -0800, in comp.lang.c , "drM" <md**@comcast.n et>
wrote:
It makes sense now...all part of the learning curve. Thanks for your
input.


You may also want to consider not using scanf for user input - its not very
robust or secure. Its often better to use fgets and sscanf. As you've
found scanf can't handle unexpected input very well.

What does this programme do, if the user types in "one" as the response?

#include <stdio.h>

int main(void)
{
int x=1;

printf("think of a number\n");
while(x > 0)
scanf("%d", &x);

return 0;
}

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #9
drM wrote:
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 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);

}
>>>>>>>


I faced the same problems when I was new to C. Learned the hard way
though. Have been paranoid about scanf ever since. :)
A book on C by Schaum's Series was helpful regarding the nuances on
scanf. Sorry don't excatly remember the name. I rather you go thru it
once.

HTH

Regards,
Taran

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15372
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an abstracct superclass, with an abstract method such that all subclasses that implement this method make that method static. basically, the abstract class...
9
3059
by: Wenjie | last post by:
Hello, Is printf("%d", var); OK for the unsigned long var = SOME_VAR? Or should some other print format be specified? Thanks and best regards, Wenjie
11
1822
by: anonymous | last post by:
Hi CLCers, The program below prints some number and i dont understand why it is behaving like this. I am not supplying the necessary arguments to the printf function and according to my understanding the program shiould throw some error. Please clear my doubts. #include<stdio.h> int main() {
11
1442
by: Sensei | last post by:
Hi again! I have ``yet another silly question'', about arrays this time. I've looked through the FAQs in the array & memory sections without finding an answer. I surely didn't look deep enough. What does the standards (plural! C99 and pre-C99 standards) say about variable-initialized arrays like in the following code? int main(void) {
1
1094
by: Gunawan | last post by:
Hi All, I have populate data in gridview manually. SqlCommand cmd = new SqlCommand(strSQL, cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "hzFileUpload"); gvFile.Visible = true; gvFile.DataSource = ds; gvFile.DataBind(); cn.Close();
4
2182
by: Mandragon03 | last post by:
I am looking for a way to take a floating point number and get rid of any extraneous 0's at the end. For instance if I have myFloat = 9999; printf("%f", myFloat); I get 9999.0000. I want to get rid of these 0's. Here is one catch. I also need to be able to do this:
11
1577
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
3
1831
by: Bint | last post by:
Hi, I have a giant string buffer, and I want to print out small chunks of it at a time. How do I print out, say 20 characters of a string? Is it like this? printf("%20s",mystring); I can change the start point of the string, I just don't know how to tell it
2
1501
by: NickiWood | last post by:
Here we go again... My next assignment is the following: Modify the Payroll Program so that it uses a class to store and retrieve the employee’s name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.