idsuni Posted April 20, 2020 Share Posted April 20, 2020 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 More sharing options...
Funce Posted May 18, 2020 Share Posted May 18, 2020 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 More sharing options...
Red_Carpet Posted June 26, 2020 Share Posted June 26, 2020 (edited) @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 June 26, 2020 by Red_Carpet re-size Link to comment Share on other sites More sharing options...
Sherin Posted August 19, 2020 Share Posted August 19, 2020 (edited) 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 August 19, 2020 by Sherin Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now