Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
using namespace std;
int main()
{
    int a[2][3];
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<3;j++)
            a[i][j]=0;
    }
    for(int i=0;i<2;i++)
    {
        for(int j=0;a[j]!=13 && j<3;j++)
            cin>>a[i][j];
    }
for(int i=0;i<2;i++)
    {
        for(int j=0;j<3;j++)
            cout<<a[i][j]<<"\t";
    }
    cout<<"\n";
}


What I have tried:

the program is entering into an infinite loop.Why so?
Posted
Updated 5-Sep-17 7:40am
v2
Comments
jeron1 5-Sep-17 13:31pm    
This would be the perfect time to set a break point in your code and step through it.
Also, what is this doing
 a[j]!=13
'a' is a 2 dimensional array.

Use the debugger, and find out exactly what is going on.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Generally speaking, setting default value is a good habit, but you can remove this:
C++
for(int i=0;i<2;i++)
{
    for(int j=0;j<3;j++)
        a[i][j]=0;
}

because the real default value is the user input.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900