Thread: Newbie: Homework Help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Newbie: Homework Help

    Hi,

    I have a homework question that asks to

    "Write a C++ program that asks the user to enter a number that is larger than 10. If the user follows instructions, write "That is correct". This will require the use of an if statement, without an "else"

    Now how in the world are you supposed to do this without using an Else statement. I am using Dev C++

    Any Help Would Be Great,
    Soheel

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Forgot to Add Something

    FORGOT TO ADD SOMETHING TO ABOVE

    If the integer entered is not greater than 10, you have to write " Enter an Integer Greater than 10"

    I just need to know how to do that without using an else statement

    THanks

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    just read the post by the moderator, sorry about the post

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    just read the post by the moderator, sorry about the post
    It is ok to ask for help (if your teacher has no problem with it).
    It is not ok to ask people to do it for you.

    Basically you want to post your code and ask specific questions about what is not working, ect.

    In your case, how to do it with just an if statement and not an include an else.

    You can make your code so it assumes it is correct, and use the if to see if it is not correct (if it is not correct, prompt user, if it is correct it will just skip the if section).

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Instead of writing a thing explaining it I did it with comments on my code, since I'm going to bed(and the code is shorter than an explanation). Please, read the comments, that's how you learn.Attached:

    EDIT:
    Thanks for pointing that out, CornedBee. Here's the correct code.
    Last edited by jmd15; 10-04-2005 at 04:42 AM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    One error in jmd's code. "Greater than" means that the loop condition, and that of the second if, must be <=, not <.

    As for the lack of else, it works like this:
    Code:
    if( condition ) {
    } else {
    }
    is semantically equivalent to
    Code:
    if( condition ) {
    }
    if( inverse condition ) {
    }
    The logical NOT operator is !.

    Note that it's not a good idea to write real code this way. Unless the compiler can guess the parallel between condition and inverse condition (and why should it, when no proper programmer writes such code?) the latter will test the condition twice and thus be less efficient.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd use a flag. To do that, declare a variable to store the desired input and declare a variable to act as the flag. Set the flag to a default value. Then start a while loop with the conditional such that the flag will stop when the flag is not the default value. Within the body of the loop request the user enter a value into the desired input variable. if the input value is meets the desired criteria, then change the value of the flag variable to a non-default value. That way if the input satisfies the criteria the flag will no longer be the default and the next time through the loop the loop will stop.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. newbie homework help please
    By phnominon in forum C Programming
    Replies: 2
    Last Post: 11-11-2006, 09:32 PM
  3. Urgent homework help for NEWBIE
    By Kazasan in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 04:23 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM