Program putting out my while statement wrong

So I'm doing a pretty simple program for class and I have a couple while statements in it but the issue is my while statement for my "tm" variable is coming out wrong. What happens is it actually comes out less than or equal to rather than greater than or equal to when I test it out so if I put in a negative number it goes through but if I put in a positive number it loops back. And if I change it to the opposite sign, "<=", it doesn't show up in the program at all. It's really confusing me because my next question about velocity works perfectly and is pretty much identical with the exception that it has an "and" statement in it as well. Help would be appreciated. I bolded the area that is having the problem if you're confused by how I asked my question. Thanks!

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

//globals, constants, function prototypes.....none

//our program
int main ()
{
	ofstream outdata;
	outdata.open("time.txt");
	cout << fixed << showpoint << setprecision(2);
	outdata << fixed << showpoint << setprecision(2);
	
	//declaraion of variables
	double ts;
	double tm;
	double v;
	double c;
	double v1;
	char doanother = 'y';
	
	//calculations
	v = (v1 / 100) * c;
	ts = tm/(sqrt(1-(v*v)/(c*c)));
	c = 299792458;
	
	//while statement
	while (doanother == 'y')
	{
	
	//prompt user for data
	while (tm >= 0){
		cout << "Please enter the time of travel (must be greater than 0): ";
		cin >> tm;
		}
	while (v1 >= 0 || v1 <= 100){
		cout << "Please enter the velocity (must be greater than 0 and less than 100): ";
	cin >> v1;
		}
	
	//print data
	cout << "Time passed on moving clock: " << tm << endl;
	cout << "Time passed on stationary clock: " << ts << endl;
	cout << "Velocity: " << v1 << endl;		
	
	
	}
	return 0;
}


It's not finished yet, I just decided to test it out and realized the issue.

EDIT: Now my velocity "while" statement isn't working whether I put in a positive or negative number. Just randomly started >.<
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

//globals, constants, function prototypes.....none

//our program
int main ()
{
	ofstream outdata;
	outdata.open("time.txt");
	cout << fixed << showpoint << setprecision(2);
	outdata << fixed << showpoint << setprecision(2);
	
	//declaraion of variables
	double ts;
	double tm;
	double v;
	double c;
	double v1;
	char doanother = 'y';
	
	//calculations
        c = 299792458;
	v = (v1 / 100) * c;
	ts = tm/(sqrt(1-(v*v)/(c*c)));
	
	
	//while statement
	while (doanother == 'y')
	{
	    promt1:cout << "Please enter the time of travel (must be greater than 0): ";
		cin >> tm;
		if(tm>0){
		    promt2:cout << "Please enter the velocity (must be greater than 0 and less than 100): ";
	        cin >> v1;
	            if(v1>0 && v1<100){ break; }
	            else goto promt2;
	       break;
	    }
	    else goto promt1;
		}
	
	//print data
	    std::cout << "Time passed on moving clock: " << tm << endl;
	    std::cout << "Time passed on stationary clock: " << ts << endl;
	    std::cout << "Velocity: " << v1 << endl;	
	
	}


this kind of works.
I am a beginner at this.
so I dont know if these are standard c++ loop (goto loop)
hopefully someone more experienced than me can help you better.
Last edited on
Although goto is pretty standard, you should not use it. It creates unmanageble code mess. There is several cases where it might be used, but until you are knowledgable enough to identify such places, you should not use it.

Cush, you are comparing tm with 0 on line 36. But when you first do so, tm is uninitialisated. You probably have random value here.
Also your loop condition is incorrect. Right now it says "repeat this loop while tm is equal or larger than 0". In other words "Terminate this loop and continue with the program only when tm less than 0".
To fix those (one of the ways, minimal changes to program):
1
2
3
4
5
6
double tm = -1; //initialisating variable with value
//...
while (tm < 0){
	cout << "Please enter the time of travel (must be greater than 0): ";
	cin >> tm;
}
Second loop has similar problems
Last edited on
@MiiNiPaa

Ya, I just figured that out before I checked back although I didn't realize I needed to give it a value prior. So I just did that with both variables and now I'm having a different issue. Although "tm" abides by having to be greater than 0 to proceed, it skips over my next loop entirely and doesn't ask the velocity question. Thanks for the help by the way. Here's what I have now:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

//globals, constants, function prototypes.....none

//our program
int main ()
{
	ofstream outdata;
	outdata.open("time.txt");
	cout << fixed << showpoint << setprecision(2);
	outdata << fixed << showpoint << setprecision(2);
	
	//declaraion of variables
	double ts;
	double tm = -1;
	double v;
	double c;
	double v1 = -1;
	char doanother = 'y';
	
	//calculations
	v = (v1 / 100) * c;
	ts = tm/(sqrt(1-(v*v)/(c*c)));
	c = 299792458;
		
	//while statement
	while (doanother == 'y')
	{
	
	//prompt user for data
	while (tm <= 0) 
		{
		cout << "Please enter the time of travel (must be greater than 0): " << endl;
		cin >> tm;
		}
	while (v1 <= 0 && v1 >= 100) 
		{
		cout << "Please enter the velocity (must be greater than 0 and less than 100): " << endl;
		cin >> v1;
		}
	
	//print data
	cout << "Time passed on moving clock: " << tm << endl;
	cout << "Time passed on stationary clock: " << ts << endl;
	cout << "Velocity: " << v1 << endl;		
	
	
	}
	return 0;
}
Your second loop reads "execute loop only if v1 less or equal to 0 and larger or equal than 100". As there is no number that both less than 0 and greater than 100, your loop is never executed.

You want to say "execute loop if v1 either <= 0 or >= 100"
OH! So I was right in my original one >.<
Buggers, getting myself confused by reading other people's problems on google over the while statement. But I figured out my other problem as well on my it was skipping my second loop entirely and not asking the question. It was because I didn't have the statement before the loop. I didn't realize the statement inside the loop is only for if the values come up false. Thanks so much for the help.
Topic archived. No new replies allowed.