April 15, 2021, 04:42:13 AM

Author Topic: C Programming AssignmentHi,  (Read 10018 times)

0 Members and 1 Guest are viewing this topic.

Jude

  • Guest
C Programming AssignmentHi,
« on: May 25, 2002, 09:20:10 AM »
Hi,
I'm working with the Miracle C (unregistered version) compiler on the following assignment:-
"Construct a program that will input the name of a person and decide whether he/she is suspect to a crime. It has been reported that a person between 20 and 25 years old and between 66 and 70 inches tall committed the crime. The program should display the name of the suspect if they fit this description".

Inputting the following code the program compiles, builds and executes the program .. except that it only reads the first "if" statement i.e. "Is a suspect..." no matter what age or height I input. This is my code:

#include <stdio.h>
main()
{
char name;
int age;
int height;
printf ("Name of suspect:");
scanf ("%s",&name);
printf ("Age of suspect:");
scanf ("%d",&age);
printf ("Height of suspect:");
scanf ("%d",&height);

if (age >= 20,age)
if (age <=25,age)
if (height >= 66,height)
if (height <= 70,height)

printf ("Is a suspect and should be detained for questioning");

else
if (age <20,age)
if (age >25,age)
if (height <66,height)
if (height >70,height)

printf ("Is not a suspect and should be released");
}
If someone can put me on the right path I'd be very grateful,
Jude

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C Programming AssignmentHi,
« Reply #1 on: May 26, 2002, 12:03:00 AM »
(bare with me since I haven't touched C in a couple years :( and haven't touched assembly
in nearly a year.. damnit..  oh well here goes)

What you have here is kind of confusing.. uhm...

For one thing, with C you can do:

if ( <first criteria> && <second criteria> )  
{

                 [code here]

}

Or you can use || for OR while && is AND.. you can use a combination of them..
and of course stuff like & and | and other stuff like ^ (for XOR if I remember correctly)
for other things which I honestly can't remember after all this time.. ugh... just ask
anyone here what I mean heh


So you could just use && ...

what else...

well actually while there probably is more to say, that alone right there should make it
read all the statements....

if you need anymore help let me know
"My Terminal is my Soul"

Offline wilnix

  • mv user /dev/null
  • Administrator
  • Forum Hero
  • *****
  • Posts: 690
  • Country:
  • Gender: Male
  • You're not the fastest packet in the subnet...
    • Wilnix - The NetAdmin's Resource
Re:C Programming AssignmentHi,
« Reply #2 on: May 31, 2002, 06:43:40 PM »
To add to Metty's comments:

If you consolidate your first if statement so that it fits the parameters correctly, you can drop the rest of the if statements because EVERYTHING ELSE doesn't fit the criteria you are looking for. For example:

if (age > 19 && age < 26 && height > 65 && height < 71) { printf ("Is a suspect and should be detained for questioning" }
  else printf ("Is not a suspect and should be released");


That should point you in the right direction...


Wilnix
alt email address: wilnix@hackphreak.org

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C Programming AssignmentHi,
« Reply #3 on: May 31, 2002, 11:00:58 PM »
Exactly what I meant, my friend...

Thanks Will.. yeah I was giving the format HEH...
eitherway, it should solve the problem.. If you don't understand read up on it
or ask one of us or someone else...

I'm always willing to help.. heh..

I remain,

Metty

"My Terminal is my Soul"

Offline wilnix

  • mv user /dev/null
  • Administrator
  • Forum Hero
  • *****
  • Posts: 690
  • Country:
  • Gender: Male
  • You're not the fastest packet in the subnet...
    • Wilnix - The NetAdmin's Resource
Re:C Programming AssignmentHi,
« Reply #4 on: June 04, 2002, 01:48:48 AM »
Hi,
      I have just entered your if statement :  if (age > 19 && age < 26 && height > 65 && height < 71) { printf ("Is a suspect and should be detained for questioning" }
  else printf ("Is not a suspect and should be released"); and it's spot on!  Good work fella and many thanks, I greatly appreciate your help,
                                                                            Regards,
                                                                                              Jude    


No problem Jude...
   
alt email address: wilnix@hackphreak.org

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C Programming AssignmentHi,
« Reply #5 on: June 04, 2002, 04:40:04 PM »
Yeah, I guess I should've been more clear about how to do it, but oh well,
problem solved. Jude, you're more than welcome. Anyhow, one comment:

When coding, you should always come up with your own style but a good
way to form if statements that have more than one check is:

if ( (check 1) && (check 2) )
{

         
Code: [Select]

}

rather than:

if ( check1 && check2 )
{

          [code]

}

This is because it can actually get VERY messy. I've done it myself and finally decided
to come up with a solution. It looks much cleaner and is much easier to read this way.

Also:

If parenthesis are needed in the check you can nest even more parentheses as follows:

if ( (((int1*int2)/2) == 5) )
or even

if ( ((int1*int2)/2 == 5) )


Personally, I prefer the former with more parentheses. Yes it may look messier but it is a
hell of a lot easier to read when you really get into some complex checks. It also MAKES
SURE that the logic is correct and the math is correct.

Okay, enough about style...

Damnit, this makes me want to relearn C it's just a matter of time and will I be as good
as I once was.. know all the cool tricks I knew.. etc...
[/code]
"My Terminal is my Soul"

Offline wilnix

  • mv user /dev/null
  • Administrator
  • Forum Hero
  • *****
  • Posts: 690
  • Country:
  • Gender: Male
  • You're not the fastest packet in the subnet...
    • Wilnix - The NetAdmin's Resource
Re:C Programming AssignmentHi,
« Reply #6 on: June 11, 2002, 11:09:55 PM »
Good point Metgod  :D

Wilnix
alt email address: wilnix@hackphreak.org

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C Programming AssignmentHi,
« Reply #7 on: June 12, 2002, 02:34:43 PM »
hehe *smile*
"My Terminal is my Soul"

Offline wilnix

  • mv user /dev/null
  • Administrator
  • Forum Hero
  • *****
  • Posts: 690
  • Country:
  • Gender: Male
  • You're not the fastest packet in the subnet...
    • Wilnix - The NetAdmin's Resource
Re:C Programming AssignmentHi,
« Reply #8 on: June 12, 2002, 09:04:24 PM »
I guess this thread has been closed. Who's next???

[shadow=red,left,300]Wilnix[/shadow]
alt email address: wilnix@hackphreak.org