Thread: Help needed with a simple programm!!

1. Help needed with a simple programm!!

lease write a program in C for this and mail it back
as soon as possible!!!!

The problem goes lke this

There is an insurance company.They insure their
members on the following basis:

1>)He shud b a male and between 25 and 35 years of age
who resides in a town and with excellent health
condition.Then he is insured and gets and incentive of
4 per thousand.

2.)If all the above conditions are agreed and the
person is a female then then she gets
and incentive of 3 per thousand

3.)IF he is 25 to 35 years of age and a male with poor
health condition and resides in a
village then he gets an incentive of 2 per thousand

4.) if not he gets no incentive

Get the age , city , sex , health from the member and
print if he is insured or not and print
his/her incentive respectively.

Use only int and char. no functions to be used.
USE C - FOR CITY,V - FOR VILLAGE,FOR HEALTH USE E FOR EXCELLENT AND P FOR POOR FOR SEX USE M FOR MALE AND F FOR FEMALE

THIS IS THE PROGRAM THAT I TRIED

#include <stdio.h>
#include <conio.h>
void main()
{
int age;
char sex,health,stay;
scanf("%d%c%c%c",&age,&stay,&health,&sex);
if((sex=='M'||sex=='F')&&(age>=25&&age<=35)&&(stay =='C'||stay=='V')&&(health=='E'||health=='P')
{
printf("you are eligible for insurance");
if(sex=='M' && (age>=25&&age<=35) && stay=='C' && health=='E')
printf("your insurance incentive is 4 per thousand");
if(sex=='F' && (age>=25&&age<=35) && stay=='C' && health=='E')
printf("your insurance incentive is 2 per thousand");
if(sex=='M' && (age>=25&&age<=35) && stay=='V' && health=='P')
printf("your insurance incentive is 2 per thousand");
}
else
printf("you are not eligible for insurance");
}

the error i get is whenever i run the program and enter the values irrespectively i get the output as you are not eligible.

2. >Please write a program in C for this and mail it back as soon as possible!!!!
Wow, what an awful start to this question.

>the error i get is whenever i run the program and enter the
>values irrespectively i get the output as you are not eligible.
That's funny, I get a syntax error because you forgot the closing paren for the outer if statement. Post input that should work but doesn't. My test worked just fine.

>#include <conio.h>
There's no need for this.

>void main()
main doesn't return void, ever. main returns int, and the common return value is 0.

3. I am sorry about the way i started.I am sorry about that.I have closed the parenthesis and checked it again but still i get the same result.I believe there is something wrong with the logic!!

4. >checked it again but still i get the same result.
Okay, what input are you givng the program? My first test gave me the desired output, so either you're not giving the program what it expects, or you stumbled on an oddball edge case. Either way it would be helpful to know what you're doing so I don't waste time debugging a program where the P.E.B.K.A.C.

5. Originally Posted by jabrouni1
I am sorry about the way i started.I am sorry about that.I have closed the parenthesis and checked it again but still i get the same result.I believe there is something wrong with the logic!!

Another possibility is that you are not getting user input correctly.

You can do something like this to see what is happening. That way you won't have to wait for someone else to figure it out.

Code:
```  scanf("%d%c%c%c",&age,&stay,&health,&sex);

printf("age = %d, stay = %c, health = %c, sex = %c\n",
age, stay, health, sex);```
Regards,

Dave

6. I THNK THAT IT MUST BE BECAUSE HE IS PUTTIN SPACES BETWEEN THE INPUTS.....COZ THE ENDIN 3 ARE CHAR,THE SPACES MIGHT BE COUNTED.....I SAY THIS AS I TRIED IT IN MY COMPILER WITH THE SPACES AND WITHOUT THE SPACES AS I WAS DOUBTFUL BOUT THE TWO THINGS AND I GOT DIFFERENT RESULTS...ALSO MAKE SURE THAT U ENTER THE LETTERS IN CAPS....AND ONE MORE THING......THIS IS NOT THE RITE WAY TO ACCEPT INPUT WHERE THE USER DOES NOT KNOW WAT HE HAS TO PUT IN

SOEMTHING LIKE
Code:
``` printf("please enter gender as M or F");
fflush(stdin);
scanf("%c",&sex);```
wud be much better......
hope ya find the soln .....
ciao

7. >I THNK THAT IT MUST BE BECAUSE HE IS PUTTIN SPACES BETWEEN THE INPUTS
Good job Sherlock, now would you mind pressing the Caps Lock key before typing again?

>fflush(stdin);
This is undefined. fflush is only defined for output and update streams.

>wud be much better......
>hope ya find the soln .....
Your punctuation to content ratio is far too high. N it wud B bttr f u lurnd 2 typ w/o abbrv.

8. Originally Posted by Prelude
>I THNK THAT IT MUST BE BECAUSE HE IS PUTTIN SPACES BETWEEN THE INPUTS
Good job Sherlock, now would you mind pressing the Caps Lock key before typing again?

>fflush(stdin);
This is undefined. fflush is only defined for output and update streams.

>wud be much better......
>hope ya find the soln .....
Your punctuation to content ratio is far too high. N it wud B bttr f u lurnd 2 typ w/o abbrv.

sorry bout the dots prelude.
1st point:i tried the input with spaces and without spaces.
when i put the spaces,it says that i am not eligible for insurance.
when i dont put the spaces,it says that i am eligible for insurance.
2nd point:fflush(stdin) can be used to clear the keyboard buffer.
if u see.try this experiment with an ansi c compiler.
Code:
```  #include<stdio.h>
#include<conio.h>
int main()
{
getch();
return 0;
}```
now when the screen appears,press one of the arrow keys.
that provides the character for getch();then try to run the program again.
u see.when u try for the second time just after pressing the arrow key,it does not wait for getch();this is because of the various things going on with the keyboard buffer.thats y fflush is useful so that it does not happen while accepting input with scanf();

please correct me if i am wrong
take care
ciao

9. >1st point:
I'm well aware of what the problem is. The whole point was to get the OP to spell it out.

>2nd point:
It may work for you, that's fine. But just because it works for you doesn't mean it's right. Also, while you can prove that it works on your compiler, I can prove that the language definition explicitly states that it's undefined:
Code:
```ISO C Standard Section 7.19.5.2, Paragraphs 1 and 2:

#include <stdio.h>
int fflush(FILE *stream);

If stream points to an output stream or an update stream in
which the most recent operation was not input, the fflush
function causes any unwritten data for that stream to be
delivered to the host environment to be written to the file;
otherwise, the behavior is undefined.```
And because the language definition dictates what's correct, fflush(stdin) is not correct.

10. Originally Posted by Prelude
>1st point:
I'm well aware of what the problem is. The whole point was to get the OP to spell it out.

>2nd point:
It may work for you, that's fine. But just because it works for you doesn't mean it's right. Also, while you can prove that it works on your compiler, I can prove that the language definition explicitly states that it's undefined:
Code:
```ISO C Standard Section 7.19.5.2, Paragraphs 1 and 2:

#include <stdio.h>
int fflush(FILE *stream);

If stream points to an output stream or an update stream in
which the most recent operation was not input, the fflush
function causes any unwritten data for that stream to be
delivered to the host environment to be written to the file;
otherwise, the behavior is undefined.```
And because the language definition dictates what's correct, fflush(stdin) is not correct.

okk.if u say frm the standard that it is undefined.bt well,its not that it works only for me.its a tried and tested thing.
as for the reply to the 1st point,i dont get wat u mean by getting the op to spell it out??

11. >its a tried and tested thing
Not everyone uses a compiler that supports fflush on input streams. If you want to be stubborn and use it then go ahead, but you'll get corrected on a regular basis around here. And because it gets old after a while, those corrections will turn to flames.

>i dont get wat u mean by getting the op to spell it out??
Both to verify my assumption about the problem and to get the OP to think about what he's doing. The first rule of debugging is to turn off the autopilot and start thinking about what's going on.

12. Originally Posted by PING
its a tried and tested thing.
So is void main().

13. Originally Posted by Prelude
>its a tried and tested thing
Not everyone uses a compiler that supports fflush on input streams. If you want to be stubborn and use it then go ahead, but you'll get corrected on a regular basis around here. And because it gets old after a while, those corrections will turn to flames.

>i dont get wat u mean by getting the op to spell it out??
Both to verify my assumption about the problem and to get the OP to think about what he's doing. The first rule of debugging is to turn off the autopilot and start thinking about what's going on.

i dont like to be stubborn prelude.i tried the fflush thing on the borland and the ansi c compiler and it worked.also on some other compiler i dont recount.thats y i posted it.sorry ,i didnt know that it does not work the same on all compilers.and one more thing->wats OP??

14. > its not that it works only for me.its a tried and tested thing.
The problem with "works for my compiler" is what happens when you upgrade your compiler / OS and all of a sudden it stops working for you.
Then you have to unlearn a whole bunch of stuff and learn something else.

So we try and stick to things which are pretty much guaranteed to work on any compiler which has an ANSI badge on it. That way you don't have to worry about "my compiler does...." and you can get on with life knowing that your program will do the right thing without having to care about which compiler you're using.

15. The thing about undefined results is that it's not compiler dependent. It might even work fine for you if the input buffer contains certain characters, but it might send an email to your boss saying she's a jerk with different characters in the buffer. Just don't do things that have undefined results even if you think they work on your compiler.