Thread: A simple 'else' without a previous 'if'

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    A simple 'else' without a previous 'if'

    Hi all hope everyone is well I just started to learn c++ so I'm sorry this has been asked a million times before and I've been through half of google but it seems I'm too stupid to work this out! Can someone tell me where I'm going wrong?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    
    using namespace std;
    
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    
    
    {
        int nAge;
    
    
        cout << "Enter Age:";
        cin >> nAge;
    
    
    {
    if (nAge < 0)
    
    
    
    
    
    
        cout << "Age can't be negative; using 0." << endl;
        nAge = 0;
    }
    
    
    else
    {
        cout << "Age of " << nAge << " entered" << endl;
    }
    
    
    
    
    }
    
    
    return 0;
    
    
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    1.Line 13 and 42 are not needed. (Though not wrong.)
    2.Interchange Lines 21 and 22.
    3.Indent better.

    Btw, a little simple problem with your logic..the output isn't printed when the input is < 0.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Do u know the purpose of using curly braces?
    Code:
    {
    }
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    Wow that was simple many thanks! Hmm there to start and end/or wrap statements and functions right?

    I'm just working through this "beginning C++ for dummies book" so I really am a super beginner

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Rule of thumb:
    Whenever a function begins, use a {.
    After an if statement, use a {.
    After a while or for statement, use {.
    Whenever you use a {, you must tell the compiler where it ends with a }. For example, it tells what lines belong to a function, if statement or loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    100
    Think of something inside of curly braces as like a smaller program. It's generally roughly referred to by names such as "block" and "scope" (roughly), but code inside a pair of curly braces is kind of like it's own little box that stuff gets run inside. As such, there's what's inside the box, what's outside the box, and what's being moved in and out of the box. Sometimes the box is itself inside of a larger box, and there's the matter of who has a key to the lock on the box.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. previous definition is here
    By AaronP in forum C Programming
    Replies: 4
    Last Post: 04-02-2012, 08:21 AM
  2. How to go to a previous line
    By Megamanenm in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 06:49 PM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. My Previous Project...
    By thecrazycoerian in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2004, 10:42 AM
  5. First Last Next and Previous
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-17-2002, 10:05 AM