delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/06/12:47:24

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie Question
Date: Fri, 06 Jun 1997 00:23:22 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 141
Message-ID: <3397587A.6CC@cs.com>
References: <01bc71c5$0db9aee0$75629986 AT doh>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Cory Fahey wrote:
> 
> Ok guys I am just trying tolearn some of the basics of C, I looked at a few
> tutorials online but none were really helpful. So I decided to just try a

Try getting a good book; net tutorials are not always perfect.  DJ
Delorie has made an attempt at a DJGPP-specific tutorial; visit his web
pages at <http://www.delorie.com/djgpp/> and follow the links for online
help.  Or try a book such as _The New C Primer Plus, 2nd Edition_, by
the Waite Group.  It's a fantastic reference.

> sample program, The program calculates the amount owed on my house monthly
> bills. An explanation of what my problems are is welcome. Also can someone
> explain to me when blocks should be opened and closed. Here is the basic
> code I was trying. Aslo what is the cc1plus command to make an exe from a
> source called first.c ?

1) cc1plus is used to compile C++ programs, not C programs.
2) You should not be calling any of the subprograms directly.  The 'gcc'
driver controls all compilation stages and it is the only program you
should use unless you know _exactly_ what you are doing.

Simple compilation is performed as follows:

   gcc -o first.exe first.c

This compiles first.c into an executable.

For more info on compilation, read the 'readme.1st' file that
accompanies the DJGPP distribution.  It's there for a reason... and pay
special attention to the extra flags, such as '-Wall' and '-O', which
can be extremely helpful for catching mistakes.

I'll take a crack at debugging your program as you wrote it, but I can't
easily help you on your design.  There are about a hundred ways to do
this program better, although it suffices for a beginner.  ;)

First, you forgot to include the necessary header files.  The very first
line of every C program should be:

#include <stdio.h>

This prototypes all the standard input/output functions, variables, and
macros.  Without it, the compiler doesn't know what printf() and scanf()
are.

>    float rob, cory, tommy, terry, heat, robphone,
>          terryphone, cable, cablem, total

You are missing a terminating semicolon on this line.  And why are you
defining the variables globally?  Since you only have one function, they
can be used locally with no problems.

>    main()

This is okay, but I prefer saying "int main( void )" explicitly. 
Nothing says you have to, though.

> {
>    printf("Enter the amount of the heat bill:");
>    scanf( "%f", &heat);
>    total = heat + 800.0;
>    printf("/n Enter the amount of the cable bill:");

The newline character is '\n', not '/n'.  The backslash is the C escape
character.  There's also no reason to put spaces after it, unless you
like your output to look strange.  You need to fix this in all your
printf()'s.

>    scanf("%f", &cable);
>    total += cable;
>    printf("/n Enter the amount of the cable modem bill:");
>    scanf("%f", &cablem);
>    total += cable;

You probably mean cablem on this line.

>    rob=total /4.0;
>    cory=rob;
>    tommy=rob;
>    terry=rob;

To be somewhat more concise, you could use
"rob=cory=tommy=terry=total/4.0;".  C is the language of shortcuts,
after all.  Please be careful using this syntax; it's not for the weak
of heart.  ;)

>    printf("/n Enter the service charge of the house phone bill:");
>    scanf("%f", &robphone);
>    robphone = robphone * 1.15;
>    rob=rob + robphone/3.0;
>    cory=cory + robphone/3.0;
>    tommy=tommy + tommy/3.0;
>    printf("/n Enter Cory's long distance:");
>    scanf("%f", &robphone);
>    cory=cory + robphone*1.15;
>    printf("/n Enter Rob's long distance:");
>    scanf("%f", &robphone);
>    rob=rob + robphone*1.15;
>    printf("/n Enter Tommy's long distance:");
>    scanf("%f", &robphone);
>    tommy= tommy + robphone*1.15;
>    printf("/n Enter Terry's total phone bill:");
>    scanf("%f", &terryphone);
>    terry=terry + terryphone;
>    printf

C isn't Pascal.  What's this extra prinf doing here all by itself?

>    printf("/n Cory: %f Tommy: %f Rob: %f Terry: %f", cory, tommy, rob,
> terry)

First, you must end this line with a semicolon.  Second, using the %f
specifier by itself will cause your numbers to be output in an extremely
lengthy notation.  Third, you should put a newline on the end of the
line as well as the beginning.  Try this:

printf( "\nCory: %.2f  Tommy: %.2f  Rob: %.2f  Terry:  %.2f\n", cory,
tommy, rob, terry );

The ".2" betwwen '%' and 'f' means to format the floating point number
with two decimal places.

You must also put a return statement at the end of main().  "return 0;"
will do just fine.
> }

Your blocks look just fine, considering that you only have one.  ;)

For further information and advice, please email me privately to avoid
cluttering the list with off-topic conversation.

Hope this helps!

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| <<< This tagline under >>> |
|                      - Douglas Adams | <<<    construction    >>> |
---------------------------------------------------------------------

- Raw text -


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