473,508 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop Not Working

32 New Member
Hey Everyone. I have a program for school that is suppose to project how many seconds it takes a projectile to hit the ground. I have the formula, but the loop is not posting to my output file. Can someone tell me what I'm doing wrong?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. float const gravity = 9.8;
  9. double velocity;
  10. double seconds;
  11. double height;
  12. ofstream launch;
  13.  
  14. int main()
  15. {
  16. launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
  17. launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters)"<<'\n'<<'\n';
  18. cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
  19. cin>>velocity;
  20.  
  21. seconds = 1;
  22. while(height > 0)
  23. {
  24. height = velocity * seconds - 1/2 * gravity * pow(seconds,2);
  25. launch<<setw(13)<<seconds<<setw(8)<<" "<<setw(14)<<height<<'\n';
  26. seconds++;
  27. }
  28.  
  29. launch.close();
  30.       system("PAUSE");
  31.       return 0;
  32. }
Dec 7 '07 #1
18 2138
Ganon11
3,652 Recognized Expert Specialist
In your formula, you use 1/2. That's fine for real life, but not in C++ - When an integer is divided by another integer, the computer returns an integer, and if the actual answer is a decimal, it truncates the decimal part. So 1/2 is 0.5, which gets truncated down to 0, so the acceleration due to gravity is never considered, only initial velocity. Try 1.0/2.0 instead of 1/2.
Dec 7 '07 #2
Prosdo
32 New Member
Something weird is going on with the program. It compiles, but when I open the prompt and enter a velocity then hit enter it just skips to the next line and doesn't do anything? I have no clue what's going on.
Dec 7 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Check the contents of launch.dat and see if your output is being written. If not, the file may not be opening.
Dec 7 '07 #4
Prosdo
32 New Member
Okay I fixed the cin issue kinda. Now it is only writing my headers to the file. I don't get what is going on. Before I fixed the cin issue it was writing data to the output file even though it was wrong. I was suppose to use a while loop but it only works with a for loop and when I use the for loop it put the wrong data. Ugh!
Dec 8 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Well, your loop runs while height is greater than 0 - but you never initialize height. Perhaps you can initialize height to 0, and have the loop run while height is greater than or equal to zero?
Dec 8 '07 #6
Prosdo
32 New Member
Well, your loop runs while height is greater than 0 - but you never initialize height. Perhaps you can initialize height to 0, and have the loop run while height is greater than or equal to zero?
I did that. My professor said to use a while loop so it doesn't go past when the projectile hits the ground. When I do that it doesn't work, but as soon as I make it a for loop it runs except it goes one past when the projectile would hit the ground into the negative and I'll lose points for that.
Dec 8 '07 #7
Ganon11
3,652 Recognized Expert Specialist
OK, I took your code and made a few modifications:

1) Changed the 1/2 to (1.0/2.0) as we discussed.
2) Made the loop run while height >= 0
3) Set height = 0 before the loop.
4) Used const double gravity = 9.8; rather than float const gravity = 9.8

I then made the output cout << rather than launch << (which should not change the results, only where I see them), and your program works perfectly.
Dec 8 '07 #8
Prosdo
32 New Member
OK, I took your code and made a few modifications:

1) Changed the 1/2 to (1.0/2.0) as we discussed.
2) Made the loop run while height >= 0
3) Set height = 0 before the loop.
4) Used const double gravity = 9.8; rather than float const gravity = 9.8

I then made the output cout << rather than launch << (which should not change the results, only where I see them), and your program works perfectly.
When you run it does it go into the negatives in the results like:
Time(Seconds) Height(Meters)

1 20.1
2 30.4
3 30.9
4 21.6
5 2.5
6 -26.4

My assignment says it should end at 0 but no matter what way I change it, it keeps going to negatives. Thank you for the help I really appreciate it.
Dec 9 '07 #9
Ganon11
3,652 Recognized Expert Specialist
Yes, it does output one negative number. However, this is due to the very nature of your program. Maybe you can calculate the height for the first second, and then execute your loop; inside the loop, print first, and then re-calculate height. Or, you can change your loop to a do...while loop. This will get rid of the last, negative result.

However, you then have to manually calculate the time when height = 0 again. This will likely be an explicit formula, from the fact that 0 = vel * t - 9.8 * t^2. You can use the quadratic formula with a = -9.8, b = velocity, c = 0.
Dec 9 '07 #10
Prosdo
32 New Member
Yes, it does output one negative number. However, this is due to the very nature of your program. Maybe you can calculate the height for the first second, and then execute your loop; inside the loop, print first, and then re-calculate height. Or, you can change your loop to a do...while loop. This will get rid of the last, negative result.

However, you then have to manually calculate the time when height = 0 again. This will likely be an explicit formula, from the fact that 0 = vel * t - 9.8 * t^2. You can use the quadratic formula with a = -9.8, b = velocity, c = 0.
Thanks Ganon. I figured it out I added an if statement inside the loop. I appreciate all your help and feedback. It is so appreciated.
Dec 9 '07 #11
Prosdo
32 New Member
My last problem is the professor said I need to format the output with a fixed, showpoint and setprecision. But I am getting errors. We didn't cover these too much so I am kinda shaky on them.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. float const gravity = 9.8;
  9. float velocity;
  10. float seconds;
  11. float height;
  12. ofstream launch;
  13.  
  14. int main()
  15. {
  16. launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
  17. launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters/Seconds Squared)"<<'\n'<<'\n';
  18. cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
  19. cin>>velocity;
  20. seconds=1;
  21. height=0;
  22. launch.setf(ios||fixed||ios||showpoint);
  23. launch<<setprecision;
  24. for(seconds=1;height >= 0;seconds++)
  25. {
  26. height = velocity * seconds - (1.0/2.0 * gravity * pow(seconds,2));
  27. if (height<0)
  28. launch<<setw(13)<<seconds<<setw(8)<<" "<<setprecision(2)<<0<<'\n';
  29. else
  30. launch<<setw(13)<<seconds<<setw(8)<<" "<<setprecision(10)<<height<<'\n';
  31. }
  32. launch.unsetf(ios||fixed||ios||showpoint);
  33. launch<<unsetprecision;
  34. launch.close();
  35.       system("PAUSE");
  36.       return 0;
  37. }
Dec 9 '07 #12
Ganon11
3,652 Recognized Expert Specialist
What errors are you getting?
Dec 9 '07 #13
Prosdo
32 New Member
What errors are you getting?
c:\docume~1\stepha~1\desktop\progra~1.cpp: In function `int main()':
c:\docume~1\stepha~1\desktop\progra~1.cpp:22: parse error before `||'
c:\docume~1\stepha~1\desktop\progra~1.cpp:22: `fixed' undeclared (first use this function)
c:\docume~1\stepha~1\desktop\progra~1.cpp:22: (Each undeclared identifier is reported only once
c:\docume~1\stepha~1\desktop\progra~1.cpp:22: for each function it appears in.)
c:\docume~1\stepha~1\desktop\progra~1.cpp:22: parse error before `||'
c:\docume~1\stepha~1\desktop\progra~1.cpp:32: `unsetprecision' undeclared (first use this function)
c:\docume~1\stepha~1\desktop\progra~1.cpp:33: parse error before `||'
c:\docume~1\stepha~1\desktop\progra~1.cpp:33: parse error before `||'
Dec 9 '07 #14
Ganon11
3,652 Recognized Expert Specialist
It's ios::fixed and ios::showpoint, not ios||fixed and ios||showpoint. The || is used to sort of link the two flags together so that both are passed to the function.
Dec 9 '07 #15
Prosdo
32 New Member
It's ios::fixed and ios::showpoint, not ios||fixed and ios||showpoint. The || is used to sort of link the two flags together so that both are passed to the function.
I can't get it to just show one decimal point for the height. I don't know what I'm doing wrong.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. float const gravity = 9.8;
  9. float velocity;
  10. float seconds;
  11. float height;
  12. ofstream launch;
  13.  
  14. int main()
  15. {
  16. launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
  17. launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters/Seconds Squared)"<<'\n'<<'\n';
  18. cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
  19. cin>>velocity;
  20. seconds=1;
  21. height=0;
  22. launch.setf(ios::showpoint||ios::fixed);
  23. launch<<setprecision(2);
  24. for(seconds=1;height >= 0;seconds++)
  25. {
  26. height = velocity * seconds - (1.0/2.0 * gravity * pow(seconds,2));
  27. if (height<0)
  28. launch<<setw(2)<<seconds<<setw(8)<<" "<<setw(10)<<0<<'\n';
  29. else
  30. launch<<setw(2)<<seconds<<setw(8)<<" "<<setw(10)<<height<<'\n';
  31. }
  32. launch.unsetf(ios::showpoint||ios::fixed);
  33. launch.close();
  34.       system("PAUSE");
  35.       return 0;
  36. }
Dec 10 '07 #16
Prosdo
32 New Member
I tried changing the precision numbers but it made it just sent things crazy lol.
Dec 11 '07 #17
Ganon11
3,652 Recognized Expert Specialist
Whoops; instead of launch.setf(ios::showpoint||ios::fixed), try launch.setf(ios::showpoint|ios::fixed), then change setprecision's value to 1.
Dec 11 '07 #18
Prosdo
32 New Member
Whoops; instead of launch.setf(ios::showpoint||ios::fixed), try launch.setf(ios::showpoint|ios::fixed), then change setprecision's value to 1.
Thanks so much that was driving me crazy. You are a life saver!
Dec 11 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1983
by: Ken Fine | last post by:
I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop, and it ain't working -- I'm getting a syntax error....
8
1661
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having...
8
2845
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
5
9732
by: L. Oborne | last post by:
I have this code working fine in Classic ASP but I get compile errors when I try to run it as ASP.NET. Do While NOT RS.EOF If ... Then... Else... End If RS.MoveNext Loop
34
2649
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
52
6270
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
1853
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
2
2062
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
6
500
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
4
6812
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.