Error ?

This is a discussion on Error ? within the C++ Programming forums, part of the General Programming Boards category; I am making this program but It doesnt works properly if different values are entered Write program which will take ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Error ?

    I am making this program but It doesnt works properly if different values are entered

    Write program which will take personís gender, material status(in characters) and age as input and will tell that the person is eligible to get job in Departement of Defense or not.
    Creiteria:

    FOR MALE:
    Age should be between 26-39
    He can be married or single
    Gender should be male

    FOR FEMALE
    Age should be between 20-28
    She should be single
    Gender should be female

    INPUT:
    ENTER MATERIAL STATUS:S
    ENTER AGE: 28
    ENTER GENDER: M

    OUTPUT:
    CRITERIA MET

    NOTE:
    1. if user enter a character for material status other of S or M and for gender other than M OR F program should give a error and teriminate.

    Code:
    void main()
    {
    char g,ms;
    int age;
    clrscr();
    if(g=='M' && g!='F' && ms!='M' || ms!='S')
    	{
    	cout<<"\n Gender : ";
    	cin>>g;
    	cout<<"\n Age : ";
    	cin>>age;
    	cout<<"\n Marital status : ";
    	cin>>ms;
    	if(g=='M' && age>=26 && age<=39)
    	cout<<"\n Criteria Met ";
    	else if(g=='F' && age>=20 && age<=28 && ms=='S')
    	cout<<"\n Criteria Met ";
    	else
    	cout<<"\n Invalid Info ";
    	}
    else if(g!='M' || g!='F' && ms!='M' || ms!='S')
    cout<<"\Error";
    exit(0);
    }
    getch();

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if(g=='M' && g!='F' && ms!='M' || ms!='S')
    This test is completely meaningless since the variables g and ms are unintialized at that point - they could contain any random value.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    anon can you please correct it , I have tried this program from different steps still cant make it write

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ENTER MATERIAL STATUS
    Are you sure this isn't supposed to be MARITAL STATUS?

    http://en.wikipedia.org/wiki/Marital_status


    anon is right, where your if-statement sits, the variables are uninitialized, so could have any values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    hehe i just copied the question sorry its marital status

    check this code , i just want to know how to do "if user enter a character for material status other then S or M and for gender other than M OR F program should give an error and teriminate."
    Code:
    void main()
    {
    int age;
    char g,m;
    clrscr();
    cout<<"\n Gender : ";
    cin>>g;
    cout<<"\n Age : ";
    cin>>age;
    cout<<"\n Marital Status : ";
    cin>>m;
    if(g=='M'&& age>=26 && age<=39 && m=='S' || m=='M')
    cout<<"\n criteria met ";
    else if(g=='F' && age>=20 && age<=28 && m=='S'&& m!='M')
    cout<<"\n Criteria met ";
    else
    cout<<"\n ERROR!! ";
    exit(0);
    getch();
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    if(g=='M'&& age>=26 && age<=39 && m=='S' || m=='M')
    
    else if(g=='F' && age>=20 && age<=28 && m=='S'&& m!='M')
    Some slight modification to those tests are needed:
    Code:
    if(g=='M'&& age>=26 && age<=39 && (m=='S' || m=='M'))
    
    else if(g=='F' && age>=20 && age<=28 && m=='S')
    Also, main should return int.

    [edit]
    Code:
    exit(0);
    getch();
    And the getch is pointless since the exit is placed before it meaning that the code will never be reached... not to mention that a call or two to std::cin.get() would work just as well as a nonstandard getch call.
    [/edit]
    Last edited by hk_mp5kpdw; 10-31-2008 at 06:00 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    You need to specify priority as hk_mp5kpdw kindly points out, because
    if (g == 'M' && age >= 26 && age <= 39 && m == 'S' || m == 'M')
    Can mean:
    - If gender is male and age is 26 or more, but 39 or below, and either single or married.
    if (g == 'M' && age >= 26 && age <= 39 && (m == 'S' || m == 'M'))
    - If gender is male and age is 26 or more, but 39 or below and single
    OR
    - If the individual in mind is simply married.
    Which is not what you want and this formula looks like:
    if ((g == 'M' && age >= 26 && age <= 39 && m == 'S') || m == 'M')

    So when you combine and and or, use () to show what you actually mean! Even if it makes sense to you and the compiler and you KNOW it is right, others will most likely not and might either be confused or think you have made an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21