Jump to content

simple c++ program


idsuni

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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...