Jump to content

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;
    
}
Link to post
Share on other sites
  • 4 weeks later...

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

Link to post
Share on other sites
  • 1 month later...

@Funce, It seems that you never got a thank you for your reply.

So, in the spirit of all that is good let me do it, as you are unlikely to get it from the OP.

 

THANK YOU VERY MUCH

Edited by Red_Carpet
re-size
Link to post
Share on other sites
  • 1 month later...

You will declare  <iostream.h> instead of <iostream> in your file . You have declared int i twice. but actually all languages are one time declare to variable. You can add only i=0 in if statement. if you have not another variable so i++ not working

#include<iostream.h>

using namespace std;

int main ()

{

int i,n=5;

for (i=0;i<n;i++)

{

cout<< i << endl;

}

return 0;

} 

 

Edited by Sherin
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...
×
×
  • Create New...