Jump to content
Sign in to follow this  
idsuni

simple c++ program

Recommended Posts

So i started programming a few days ago and wanted to do a little C++ program. Can anyone explain me why it prints 0 instead of 1? What do I need to change? Best regards.

#include <iostream>

using namespace std;

int main ()
{
    int i;
    i = 0;

    if (int i = 0)
    {
        i++;
    }
    else 
    {

    }

    cout << i << endl;

    return 0;
    
}

Share this post


Link to post
Share on other sites

You've redeclared your i variable inside your if statement. The i++ is incrementing that i rather than the i that's being outputted.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...