www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/13/20:58:14

From: frolikcc AT indy DOT net (Chris Frolik)
Newsgroups: comp.os.msdos.djgpp
Subject: newbie needs help
Date: Wed, 13 Nov 1996 20:03:13 +0600
Organization: IndyNet - Indys Internet Gateway (info AT indy DOT net)
Lines: 38
Message-ID: <frolikcc-1311962003140001@indynet.indy.net>
NNTP-Posting-Host: indy2.indy.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I am a newbie C programmer, and I just downloaded djgpp as my first
compiler.  I tried to run a simple program, but had some problems.  I am
not sure if there is something wrong with my code, or I am just going
about it the wrong way.

The following is a simplified example of what I compiled:

#include <stdio.h>

void main()
{
   long n=0, x=0, result=0;
   printf("\n Enter a number between 1 and 100: "); 
   scanf("%ld", &x);
   printf("\n Enter another number between 1 and 100: ");
   scanf("%ld", &n);

   result = x - n;
   printf("%ld - %ld = %ld", x, n, result);
}

So, I compiled it and ran it. It worked fine, unless I entered a decimal
(i.e. .6) or a letter at the first prompt.  If I did, it skipped the
second scanf() entirely.  Is this supposed to happen?  I have no
experience with this, so I am not sure.  If so, what can I do to prevent
it?

Also - when printing a float, how can I drop off the trailing zeros?  For
example, if i have:

   float x=2.5;
   printf("%f", x);

It prints out 2.500000, while I only want it to print 2.5.

Thanks,

-Chris

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019