breaking a loop

hey guys i have a slight problem..i knw its not gud programming practice to use a break to exit a loop but i would lyk to break my loop when hdistance reaches a negative value...i dont want it to display the negative value...how do i use a break statement to do dis....any help wuld be highly appriciated......also please ignore my comments in my code...thanx

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
  #include <iostream>
#include <vector>
#include<fstream>
#include<cmath>
#include<iomanip>

using namespace std;
void getValuesFromFile(vector<double>&VO,vector<double>&Theta,vector<double>&DT)
{
    double vo=0;
    double theta=0;
    double dt=0;

    ifstream infile("indata.txt");

    while (infile>>vo>>theta>>dt)
    {
        VO.push_back(vo);
        Theta.push_back(theta);
        DT.push_back(dt);


    }

}

void calculate(vector<double>VO,vector<double>Theta,vector<double>DT)
{
    double pie;
    pie=4*atan(1);
    double radians;
    radians=(Theta[0]*pie)/180;

    double hdistance=0.0;
    double vdistance=0.0;
    double sum=0;

    //dt=sum;


    while(hdistance>=0)
    {


        hdistance=(VO[0]*sum*sin(radians))-(4.905*(sum*sum));
        vdistance=VO[0]*sum*cos(radians);

        //if(hdistance==0)
           // break;

        cout<<fixed<<setprecision(3)<<sum<<"   "<<hdistance<<"   "<<vdistance<<endl;




        sum=sum+DT[0];

    }
    

}


int main()
{
    vector<double> VO;
    vector<double> Theta;
    vector<double> DT;

    getValuesFromFile(VO,Theta,DT);
    calculate(VO,Theta,DT);
    return 0;

}
Last edited on
It's perfectly fine to use an occasional break statement when it can't be avoided.

change your if statement to: if (hdistance <= 0)
I don't see a problem. I use break to exit a loop all the time and it works. I just do this:

1
2
3
4
5
6
while(cin>>var){
      if(var == "end"){
              break;
      }
}


I just looked through three of my programs and they all had something like that and it worked. try removing the comment in the if statement.
i did but nw am nt displaying anything

here is wat i dd

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
  #include <iostream>
#include <vector>
#include<fstream>
#include<cmath>
#include<iomanip>

using namespace std;
void getValuesFromFile(vector<double>&VO,vector<double>&Theta,vector<double>&DT)
{
    double vo=0;
    double theta=0;
    double dt=0;

    ifstream infile("indata.txt");

    while (infile>>vo>>theta>>dt)
    {
        VO.push_back(vo);
        Theta.push_back(theta);
        DT.push_back(dt);


    }

}

void calculate(vector<double>VO,vector<double>Theta,vector<double>DT)
{
    double pie;
    pie=4*atan(1);
    double radians;
    radians=(Theta[0]*pie)/180;

    double hdistance=0.0;
    double vdistance=0.0;
    double sum=0;




    while(hdistance>=0)
    {


        hdistance=(VO[0]*sum*sin(radians))-(4.905*(sum*sum));
        vdistance=VO[0]*sum*cos(radians);

        if(hdistance<=0)
            break;


        cout<<fixed<<setprecision(3)<<sum<<"   "<<hdistance<<"   "<<vdistance<<endl;




        sum=sum+DT[0];


    }


}


int main()
{
    vector<double> VO;
    vector<double> Theta;
    vector<double> DT;

    getValuesFromFile(VO,Theta,DT);
    calculate(VO,Theta,DT);
    return 0;

}
Really? I didn't get any errors when I ran it through.
Topic archived. No new replies allowed.