www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/16/22:45:26

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: <NEWBIE> why does this not work?
Date: Thu, 16 Jul 1998 22:28:28 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 48
Message-ID: <35AEB6CC.D2F98298@cs.com>
References: <6olca3$8ps$1 AT mendelevium DOT btinternet DOT com>
NNTP-Posting-Host: ppp113.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

WillB wrote:
> 
> I can't get this program to work. It is supposed to ask for name, age, and
> month of birth, then print the name and age, then if else the different
> months to choose the correct description of their horoscope. DJGPP tells me
> january is undeclared and febuary is undeclared. What am I doing wrong. I'm
> sorry if I have done something stupid- but I am new to this. Program is
> below (in C)

Nowhere in your code do you tell the compiler what 'january' and
'february' are; they aren't reserved words, variables, constants, or
functions.

You must define something like this (there are at least four ways):

#define january		1
#define february	2
#define march		3

Alternatively:

const int january=1;
const int february=2;
const int march=3;

Another way:

enum {january, february, march, /* ... */};

A fourth way is to use an array:

const char * month_names[] = { "january", "february", "march", /* ... */
};

And then use just the numeric values or write a lookup function.

BTW, an array lookup or a switch...case block is much more efficient
than nested if's for the kind of application you're writing.

-- 
John M. Aldrich, aka Fighteer I <fighteer AT cs DOT com>  UIN# 7406319

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s+:- a-->? C++>$ U@>++$ p>+ L>++ E>++ W++ N++ o+>++ K? w(---)
O- M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+(++) tv+() b+++ DI++ D++ G>++
e(*)>++++ h!() !r !y+()
------END GEEK CODE BLOCK------

- Raw text -


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