Results 1 to 7 of 7

Thread: Newbie Who Needs C++ help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    2

    Question Newbie Who Needs C++ help

    Alright, this is only my 4th assignment in my C++ class, and I'm already stuck! Here's what I have to do:
    Create a program that uses a series of "if/else" statements that will test the age of a person. I have to use compound conditional statements to determine if a person is over 21 and a female (using the &&). Then, I have to use the || operator to test for males or over 21.

    Here's my basic coding so far (which is completely messed up) and thanks in advance for any help:
    #include <iostream>
    using namespace std;
    void main()
    {
    int age;// creates a variable called age
    char g;//creates a variable called mf


    cout<<"Are you m (for male) or f (for female)? ";//inputs a message on the terminal
    blah:
    cin>>g;
    if (g != 'm' && g != 'f')
    {cout<<"Are you an alien? Please enter again\n"; goto blah;}
    else cout<<"Hello, how old you are? ";//inputs a message on the terminal
    cin>>age;//waits for user input

    if (g == 'm')
    if (age > 21)
    {cout<<"You are a male over 21!\n"; }
    if (g = 'm' && age < 21 || age > 21)
    {cout<<"You are either a male or are under 21!\n";}
    if (g =='f')
    if (age > 21)
    {cout<<"You are a pretty woman over 21!\n" ;}
    else {cout<<"You are under 21, and a female!\n";}

    }

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    Canton, GA
    Posts
    487

    Wish I could help...

    Wish I could help you... but on that note one thing I have seen from poking around the boards is that people appear to REALLY HATE helping on homework assignments...

    Good luck finding your answer but if you don't get one that is probably why...

    Anjari

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Newbie Who Needs C++ help

    Please sort out the code before you post. There are some things that make it hard to read, and even though I enjoy helping newbies I don't enjoy deciphering code. So please sort out obvious inconcistencies (such as this line:
    char g;//creates a variable called mf
    which obviously doesn't create a variable called mf) and then repost the code. Then I will help you.
    Also are you sure that it is
    "Then, I have to use the || operator to test for males or over 21." and not "under 21" (or maybe under 21 already when it comes to women)?

    And use [code][/code] tags to preserve the formatting of your code and make sure that the formatting is right in the first place:
    {cout<< ...
    is bad formatting. Bad formatting has very many problems, your biggest of those is probably that few people on boards like this read such code and bother to answer your questions.

    Anjari: we don't like doing your homework for you, questions like
    I have to do x, how do I do that?
    are not welcome. But if you say:
    I have to do x. I used approach y and have this (properly formatted) code:
    Code:
    code code code
    this part doesn't do what it should
    more code code code
    Do you have an idea what the problem could be?
    then you can rest assured that you'll get an answer.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    A few pointers:

    1) Please do not use void main(). If you are doing C++, you really should use
    int main()
    {
    // do code stuff here
    return 0;
    }

    2) Do not use goto. It's bad programming style, and other than in extreme cases, it is not even considered.

    Therefore, revise

    blah:
    cin>>g;
    if (g != 'm' && g != 'f')
    {cout<<"Are you an alien? Please enter again\n"; goto blah;}

    to

    do
    {
    cin >> g;
    if (g != 'm' && g != 'f')
    cout<<"Are you an alien? Please enter again\n";
    } while (g != 'm' && g != 'f');

    That should help you a bit.

  5. #5
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I see lot's of people writing their code like this:
    Code:
    code;
    if(x == y) {
    	do this;
    	and this;
    	and this; }
    more code;
    Is that bad formating or is it ok? I write my code like this:
    Code:
    code;
    
    if(x == y)
    {
    	do this;
    	and this;
    	and this;
    }
    
    more code;
    Which way is preferable?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Of the two, I'd prefer the second. Although my preferred style is:
    Code:
    if(x == y) {
        somecode;
        blah();
        boing(thingie->ptr);
    }
    
    morecode();
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's a matter of taste. No more, no less (unless you work together with other programmers).

    This:
    Code:
    and this; }
    is probably a bad idea as it makes the ending brace hard to find.

    I use various naming and coding conventions depending on what I do, languages, environments etc. It's a funny mix.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width