Thread: End program through If statement?.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    27

    End program through If statement?.

    Hey guys i'm wondering how I would end a program prematurely through an if statement if an invalid number is input.

    I thought something like this:
    Code:
    int end=0;
    
    if((number[n]>9999999)||(number[n]<-9999999))
    {
        printf("All values must be between 9999999 and -9999999");
        return end;
    }

  2. #2
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by MstrKurt View Post
    Hey guys i'm wondering how I would end a program prematurely through an if statement if an invalid number is input.

    I thought something like this:
    Code:
    int end=0;
    
    if((number[n]>9999999)||(number[n]<-9999999))
    {
        printf("All values must be between 9999999 and -9999999");
        return end;
    }
    in place of
    Code:
    return end;
    you can use
    Code:
     exit(1);

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    That's perfect, Thanks a lot .

    ____

    I've hit a snag by whatever i'm inputting, it's saying it has to be between the 2 values and exits, even if the numbers are between them. Code is:

    Code:
    int end=0;
     
    if((number[n]>9999999)||(number[n]<-9999999))
    {
        printf("All values must be between 9999999 and -9999999");
        exit(1);
    
    }
    Last edited by MstrKurt; 01-09-2012 at 01:29 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What is the type of array number? Show the code for the input steps.
    "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

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If number is unsigned that would cause the problem. Also if you didn't read the number into number[n] properly.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Just a question...
    Exactly what datatype do you think your compiler will assume 9999999 is?

    And what datatypes should it fit in?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rdrast View Post
    Just a question...
    Exactly what datatype do you think your compiler will assume 9999999 is?

    And what datatypes should it fit in?
    Just a question...
    You aren't using an outdated obsolete piece of junk compiler like Turbid C that causes you to ask such an irrelevent question are you?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    Sorry I haven't replied. I have it sorted now. Thanks for all the help .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement that will tell the program to end?
    By PYROMANIAC702 in forum C Programming
    Replies: 24
    Last Post: 07-19-2011, 07:22 PM
  2. Help with if statement program plz
    By allen9190 in forum C Programming
    Replies: 3
    Last Post: 12-28-2009, 06:18 PM
  3. help with c++ program statement
    By robasc in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2009, 08:57 AM
  4. Program not seeing the if statement
    By adjac in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2004, 08:04 PM
  5. help with program using switch statement
    By Aysha in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 04:43 PM