else loop

This is a discussion on else loop within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int yep; cout<<"how old are you?"; ...

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

    else loop

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int yep;
        
        
        cout<<"how old are you?";
        cin>>yep;
        
        
        if (yep==12);{
        cout<<"your name must be darrell";
        }
        else;{
        cout<<"you suck";
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I was bored and started playing around and came up with this, it says syntax error before else. Another time I changed something it displayed both the second and third cout when I typed in 12.
    My computer is awesome.

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Code:
    else;{
    Get rid of that semicolon.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    a. if and else are not loop constructs, so avoid calling them that (nitpicking on my part).
    b. Remove the semicolons after the if( ) and after the else... else must be immediately prededed by and if() statement, and the semicolon terminates the if block, so the code enclosed in brackets is always executed (not part of the if statement), and the else has no immediate predecessor (an if).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Scribbler
    else;{
    Get rid of that semicolon.
    That one is no problem (syntactically -- though it is wrong from the programming logic point of view). It just means that in the 'else' case, nothing will be executed specially for that case. It is the one after the if that causes the error.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    alright I got it, but I don't even know what a loop construct is.
    My computer is awesome.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Yeah, I just skimmed through the code glancing only at the else statement based on what he said the error was, didn't even look at the if(), and was about to edit my post but I saw you already hit it

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    cin.ignore;
    cin.get;
    Most likely another stupid question but when I add this before return EXIT_SUCCESS it says statement cannot resolve address of overloaded function.
    My computer is awesome.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Loop constructs are things such as for, while, and do while that allow to repeat a certain section of code multiple times based on certain conditions.

    And you should probably consider including the parentheses to your function calls.

    edit: You may ignore the following paragraph, since it appears you already figured this part out - otherwise you wouldn't be trying the cin function calls!

    I noticed you using system("PAUSE"). If you're just learning the language and testing programs, I guess this is okay, but you should be aware of the dangers of using this method. Have a look at the "programming tips" section of the site for the explanation I wrote.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    should say
    Code:
    cin.ignore();
    cin.get()
    I think you need the parenthesis, I'm a little drunk right now though so I might be wrong lol

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    return EXIT_SUCCESS;
    adding to the end of Sean's post, and at the danger of starting a holy war here, I recommend not returning EXIT_SUCCESS. even the creator of the language (not saying he's the C++ god, although he kinda is if you think about it long enough), suggested that using macros may not be the best way to go about doing things. I suggest returning the value 0 instead.

    While we're on this topic, according to the standard, you don't need to actually return anything from main at all, but I like to explicitly close, delete, return, etc. everything I can, just as good practice. I suggest you do the same when it comes to file I/O and pointers and such.

    both of these subjects seem to become sore spots every once in a while, but that's how I see it, so that's how it is. kinda like the void main vs. int main thing... I think we've strung up every void mainer out there already, so the resistance has crumbled and dozens of high schoolers were taught the right way.

    TLDR version: return 0 instead of EXIT_SUCCESS, explicitly close/delete/return everything, and don't ever use void main.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    according to the standard, you don't need to actually return anything from main at all
    uhahlargtuipvbairgbkjabdgs!!!

    *sniff*

    Excuse me... *wipes major_small's shoulder off*

    No, the standard defines main as returning an integer value.

    edit: Unless of course you're posting in the C++ board... well you still return an integer, it's just that if nothing else is stated, the compiler will assume an int.
    Last edited by sean; 01-24-2005 at 03:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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