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!
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
usingnamespace 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; }
elsegoto promt2;
break;
}
elsegoto 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.
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;
}
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:
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.