My program doesn't gets stopped

Hi All,

I have displayed a code. In this code at the last part of program I need to stop the program once when the condition is satisfied. But My program keeps on running not stopping once the condition is satisfied. I have used break statement. But not working.

Needs to be fixed. Your help is needful to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 #include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

int main()
{
    
/*Grid Point declarations*/
    int x,y;
    double left,right,top,bottom;
    double phi[100][100],uphi[100][100];
        
    cout<<"Enter no. of grid points"<<endl<<"x:";
    cin>>x;
    cout<<"y:";
    cin>>y;
    
    
/*Read & Define Boundary Conditions*/ 
 
cout<<endl<<endl<<"Enter value for left:";
cin>>left;
cout<<endl<<"Enter value for right:";
cin>>right;
cout<<endl<<"Enter value for top:";
cin>>top;
cout<<endl<<"Enter value for right:";
cin>>bottom;

int nit;
cout<<"Enter required iterations: ";
cin>>nit;
cout<<'\n';

    for (int i=0;i<=x-1;i++)
    {
        phi[0][i]=left;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[x-1][i]=right;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[i][y-1]=top;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[i][0]=bottom;
    } 
  
cout<<'\n';
cout<<"First Iteration values"<<endl<<'\n';   
    
/*First Guess it=1*/   

for (int i=1;i<=x-2;i++)
{
    for(int j=1;j<=y-2;j++)
    {
            phi[i][j]=(left+right+top+bottom)/4;
            cout<<"1"<<'\t'<<phi[i][j]<<endl;
    }
} 
cout<<'\n';

/*    for (int i=0;i<=x-1;i++)
    {
        for (int j=0;j<=y-1;j++)
        {
            cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
        }
    }*/


/*Iteration from it=2*/   


                            
       for (int it=2;it<=nit;it++)
       {
           for (int i=1;i<=x-2;i++)
               {
                    for(int j=1;j<=y-2;j++)
                    {
                           uphi[i][j]=phi[i][j];
                           phi[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1])/4;
                           cout<<it<<'\t'<<phi[i][j]<<'\t'<<uphi[i][j]<<endl;     
                           if((uphi[i][j]-phi[i][j])<=0.001&&(uphi[i][j]-phi[i][j])>0)
                           {
                           cout<<"Solution Converged"<<'/n';
                           break;
                           }                                        
                    }
               }
               cout<<'\n';
       }  
          
                              
                  
    getch();
}
break; will only break out of the innermost loop.
What should I do to stop my whole program?
You can always stop the program by calling std::exit but that's probably not so nice, and not what you want.
http://en.cppreference.com/w/cpp/utility/program/exit

You could put the loops within a function, that way you can escape all loops by using return.
Could You please help me with this. Because I'm a Beginner in C++.

I could understand that I should use return but I'm stuck with how?
Thanks For your Help. I used goto statement to bring my process out of the loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

int main()
{
    
/*Grid Point declarations*/
    int x,y;
    double left,right,top,bottom;
    double phi[100][100],uphi[100][100];
        
    cout<<"Enter no. of grid points"<<endl<<"x:";
    cin>>x;
    cout<<"y:";
    cin>>y;
    
    
/*Read & Define Boundary Conditions*/ 
 
cout<<endl<<endl<<"Enter value for left:";
cin>>left;
cout<<endl<<"Enter value for right:";
cin>>right;
cout<<endl<<"Enter value for top:";
cin>>top;
cout<<endl<<"Enter value for right:";
cin>>bottom;

int nit;
cout<<"Enter required iterations: ";
cin>>nit;
cout<<'\n';

    for (int i=0;i<=x-1;i++)
    {
        phi[0][i]=left;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[x-1][i]=right;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[i][y-1]=top;
    }
    for (int i=0;i<=x-1;i++)
    {
        phi[i][0]=bottom;
    } 
  
cout<<'\n';
cout<<"First Iteration values"<<endl<<'\n';   
    
/*First Guess it=1*/   

for (int i=1;i<=x-2;i++)
{
    for(int j=1;j<=y-2;j++)
    {
            phi[i][j]=(left+right+top+bottom)/4;
            cout<<"1"<<'\t'<<phi[i][j]<<endl;
    }
} 
cout<<'\n';

/*    for (int i=0;i<=x-1;i++)
    {
        for (int j=0;j<=y-1;j++)
        {
            cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
        }
    }*/


/*Iteration from it=2*/   


                            
       for (int it=2;it<=nit;it++)
       {
           for (int i=1;i<=x-2;i++)
               {
                    for(int j=1;j<=y-2;j++)
                    {
                           uphi[i][j]=phi[i][j];
                           phi[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1])/4;
                           cout<<it<<'\t'<<phi[i][j]<<'\t'/*<<uphi[i][j]*/<<endl;     
                           if((uphi[i][j]-phi[i][j])<=0.001&&(uphi[i][j]-phi[i][j])>0)
                           {
                                                                                     
                           goto last;                                                           
                           }                                        
                    }
               }
               cout<<'\n';
       }  
   
   last:
        cout<<endl<<"Solution is Converged";                    
    getch();
}
Now it will always print "Solution is Converged". ;)
You were pointed to return and you somehow found goto?
Topic archived. No new replies allowed.