Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I can not see output in cycle (C++ simple code)?

I would like to edit numbers in p2 according to the code in for cycle.
But If I try to write out actual number in p2, I don´t see anything in output.
What could I change to see it?

#include <iostream>

using namespace std;

int main()
{
        
    int p1[10]={-5,-8,0,5,0,-8,-11,-2,1,-7};
    int p2[10]={0,0,0,0,0,0,0,0,0,0};
    
    for(int i; i >0; i++){
        p2[i] = p2[i] - p1[i];
        cout << p2[i];
    }
    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As pointed out by Ilya, you need to change the condition in the for loop. Right now, at the beginning of the for loop, i = 0, so the for loop never starts. Change it to the following:

#include <iostream>

using namespace std;

int main()
{
        
    int p1[10]={-5,-8,0,5,0,-8,-11,-2,1,-7};
    int p2[10]={0,0,0,0,0,0,0,0,0,0};
    
    for(int i = 0; i < 10; i++){
        p2[i] = p2[i] - p1[i];
        cout << p2[i];
    }
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading