Newsgroup: it.comp.lang.c
Titolo: please help me with this problem

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];
 main()
 {
 introduction();
 getting_the_info();
 }
 /*The following are functions used in this program*/
/*-------------------------------------------------------------------------*
/
introduction()
{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;
 }
/*------------------------------------------------------------------------*/
getting_the_info()
{ index=1;
for (index=1; index<=5; index++)
{printf("\nPlease enter the marital status of person # ");
printf("%d ==>", index);
 scanf("%c", &marital_stat[index]);
printf("\nPlease enter income of person # ");
printf("%d ==>", index);
scanf("%f", &income[index]);
}
 return;
}
--
http://www.geocities.com/Pentagon/Barracks/5529/frame-controls.html
-- 
comp.lang.c.moderated - moderation address: 
cl...@plethora.net

}
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+


 
http://www.geocities.com/Pentagon/Barracks/5529/frame-controls.html


The reason is the scanf() function you use to get a character for the
martial status of a person. One way is to use getch() which returns only a
character. But getch() does not echo the entered character to the screen. So
you might also use printf() to put the character to the screen.
So instead of writing
scanf("%c", &marital_stat[index]);
write
martial_stat[index] = (char)getch();
printf("%c", martial_stat[index]);
With this you can not press enter because of the getch() function. Otherwise
the return character entered will also be appended to the martial_status
array.
Another way to write this code is to use the scanf() function and specify
the format of your input line. That is when you enter matrial status of a
person by entering a character followed by a return you have to give this
information to the scanf() function. That is
scanf("%c\n", &martial_stat[index]);
This will filter out the return character entered and get the correct
martial status information.
One of these might solve your problems.
Riza.


 you might also use printf() to put the character to the screen.
There is no getch() function in standard C.  Try getc(), fgetc(), and
getchar().  Even better is fgets().  If you must use responses to single-key
presses, which is not obvious, then you are using implementation-specific
functionality.  This should be addressed in a newsgroup for your
implementation.  That means one other than ANY of the 9 newsgroups to which this
has been posted. And try to avoid such massive crossposting in the future.  I
don't believe for a minute that you follow all these groups.  It is extremely
rude to post to groups you don't read.


Aside from the scanf is evil problem, you also have a problem with array
addressing.  Note that in C, the first
address starts at 0 not 1.  So the line
char marital_stat[5];
sets up an array with 5 elements that must be addressed as marital_stat[0]
through marital_stat[4].  You should check the loop in getting_the_info().
If you try to access marital_stat[5] bad things will happen to your helpless
program.
cyberian bear wrote in message ...

