Thread: if/break statement

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    29

    if/break statement

    Hi @all

    I have an if statement in my prog. I want my program ends if my if statement returns false.

    In order to end my prog. I used break but g++ compiler says I should put them in a loop or switch statement.

    for example, can I write a statement like without putting them in a loop;

    Code:
    if (isalpha(chr1)
       cout <<ch1<<" is a char "<<endl;
    else
    {
        cout <<ch1<<" is not a char "<<" quitting. "<<endl;
        break;
    }
    thanks

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    then you should listen to g++
    check out the link for more info.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    Quote Originally Posted by caroundw5h
    then you should listen to g++
    check out the link for more info.

    I knew that I had to use loop or switch. but I didn't want to put a loop or switch. I wrote here because I was looking another way but I guess I have to use loop or switch..

    Thank you

    edit;
    @mods
    I got it. Please send my threat to garbage..
    Last edited by Apropos; 02-21-2005 at 04:19 PM.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    In a battle with the compiler. the compiler will most likely always win.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Um... return 0;?
    I'd think that'd be the right answer.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    break can only be used to end a loop. Use exit() or return.

  7. #7
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Code:
    if (isalpha(chr1)
       cout <<ch1<<" is a char "<<endl;
    else
    {
        cout <<ch1<<" is not a char "<<" quitting. "<<endl;
        break;
    }
    It seems to me like you check chr1, but in your cout you write ch1. An accident?
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    Quote Originally Posted by MipZhaP
    An accident?
    @MipZhaP

    Yes. It was an accident but thank you for reminding me..

    @everybody that put a comment for my thread

    Thank you. I apricated for your help.
    Not to know, not to learn is shame

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM