🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #720

Re: please help me with this problem

#720
From: Gergo Barany
Date: Thu, 15 Jul 1999 00:00
121 lines
5438 bytes
In article <clcm-19990714-0004@plethora.net>, cyberian bear wrote:

We're glad to help, but please don't crosspost into every group with C
in its name, and try to write clean code because that's easier to read.

>i have written program which collects marital status and income of 5 people
>and computes their tax. in this prog i had to use a for loop and array.
>basically i got most of it but when i execute it asks me for marital status
>of first person than his income tehn it skips over marital status of second
>person and goes straight to his income, the same happend with 3rd 4th 5th
>person. if any1 could please tell me wehre my mistake is i would be very
>greatefull to You
>Paul
>
>/*Filename:tax604.c
>program which calculates taxes paid for 5 people */
> #include<stdio.h>
> int index;
> char marital_stat[5];
> float income[5];

You should only use global variables when absolutely necessary. Also,
the above declarations give you two arrays indexed 0--4 (see below).

> main()

This will probably be illegal soon. Use int main(void)

> {
> introduction();
> getting_the_info();

The functions above have not been declared at this point.
main() returns int, so:
return 0;

> }
>
>
> /*The following are functions used in this program*/
>/*-------------------------------------------------------------------------*
>/
>introduction()

Use int introduction(void)

>{printf("\t\t\t\tTAX PROGRAM\n\t\t\t   BY \n\n");
>printf("\t\t\t\tINSTRUCTIONS:\n");
>printf("\t\t     1.ENTER ANNUAL SALARY FOR A PERSON.\n");
>printf("\t\t     2.ENTER MARITAL STATUS OF A PERSON.\n");
>printf("\t\t\t   3.REPEAT WHEN PROMPTED.\n\n");
> return;

Make that return 0;

> }
>
>/*------------------------------------------------------------------------*/
>getting_the_info()

int getting_the_info(void)

>{ index=1;

That's redundant; it happens again on the next line.

>for (index=1; index<=5; index++)

You probably meant index<5 (see below)

>{printf("\nPlease enter the marital status of person # ");
>printf("%d ==>", index);
> scanf("%c", &marital_stat[index]);

By the last iteration, this will be &marital_stat[5], which is a
problem. Your arrays are indexed 0--4, so using an index of 5 will write
to memory you quite possibly don't have a right to write to. This will
probably cause problems; if you're lucky, only your program crashes
while the rest of your system remains running.
Also, scanf() is quite a bad function to use for live user input;
ever tried to input more than one character here? Use fgets() for any
kind of input from stdin.
Common scenario: the user enters 'f' here (or any other single
character) and then completes his input with a newline ('\n') character.
The 'f' lands in marital_stat[index], the '\n' remains in stdin.

>printf("\nPlease enter income of person # ");
>printf("%d ==>", index);
>scanf("%f", &income[index]);

The user enters 23 and terminates his input with a '\n'. scanf() ignores
the first newline in stdin, puts the 23 into income[index], then leaves
another '\n' in stdin. During the next iteration, scanf("%c",
&marital_stat[index]); (above) encounters a character in stdin and
fetches it; since that character is a newline, it looks like your
program skips the question, but in fact it behaves absolutely correcty.

> return;

return 0;

>}

What we learn from all this is the following: if you must use scanf(),
do a lot of error checking. It's better to use fgets() and then parse
the string you got with sscanf(), however.
And get an indentation style; your code is hard to read and thus hard to
debug.

Gergo

-- 
Brain, v. [as in "to brain"]:
	To rebuke bluntly, but not pointedly; to dispel a source of
error in an opponent.
		-- Ambrose Bierce, "The Devil's Dictionary"

GU d- s:+ a--- C++>$ UL+++ 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+
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net
Technical Details
Message-ID: <clcm-19990715-0002@plethora.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <clcm-19990714-0004@plethora.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10f67c,488b53d56236ec72
X-Google-Attributes: gid10f67c,public
X-Google-Thread: 1014db,488b53d56236ec72
X-Google-Attributes: gid1014db,public
X-Google-Thread: 114592,488b53d56236ec72
X-Google-Attributes: gid114592,public
X-Google-Thread: 114a87,488b53d56236ec72
X-Google-Attributes: gid114a87,public
X-Google-Thread: 1111a3,488b53d56236ec72
X-Google-Attributes: gid1111a3,public
X-Google-Thread: f4614,488b53d56236ec72
X-Google-Attributes: gidf4614,public
X-Google-Thread: 100850,488b53d56236ec72
X-Google-Attributes: gid100850,public
X-Google-Thread: 106434,488b53d56236ec72
X-Google-Attributes: gid106434,public
From: gergo.barany@gmx.net (Gergo Barany)
Subject: Re: please help me with this problem
Date: 1999/07/15
Message-ID: <clcm-19990715-0002@plethora.net>
X-Deja-AN: 501464722
Approved: clc@plethora.net
References: <clcm-19990714-0004@plethora.net>
X-Complaints-To: abuse@plethora.net
X-Trace: ptah.visi.com 932070907 205.166.146.1 (Thu, 15 Jul 1999 15:35:07 CDT)
Organization: None
User-Agent: slrn/0.9.5.5 (UNIX)
NNTP-Posting-Date: Thu, 15 Jul 1999 15:35:07 CDT
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c,comp.lang.c.moderated,comp.lang.objective-c,fj.comp.lang.c,fj.lang.c,it.comp.lang.c,pl.comp.lang.c,relcom.comp.lang.c-c++
Originator: clcm@plethora.net (Comp Lang C'Moderated)