473,667 Members | 2,749 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 2163
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

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

Similar topics

4
1988
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. Any ideas why the following snippet isn't working? <% Dim ConfirmedSpeakerList2 ConfirmedSpeakerList2=(While ((Repeat1__numRows <> 0) AND (NOT rsConfirmedSpeakers.EOF)) (rsConfirmedSpeakers.Fields.Item("Per_GivenName").Value)
8
1666
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 a problem with a conditional in my loop not working correctly, I am sure that I have been overlooking something, but cannot pin it down. I have pared the code down quite a bit just so that I could resolve my issues with it. I have a page...
8
2850
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++) For(i=0; i<=K; i++)
5
9736
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
2671
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 <limits.h> #include <stdlib.h> int main(void) {
52
6313
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 variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
2
1861
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 duplicate so many time, also i'm using a removeloop if the client want to remove the frame, in the html of the page i'm creating a table and i'm calling this loop, the problem now is that when i'm calling the removeloop it removes the frame but not the...
2
2067
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 mysql. Same problem in Fx 2.0 and IE 7. I have the query sort of spread out across three pages to make it easier for me to generate it; I have a pagination script which checks what is being accessed from the query string (is it the whole blog or an...
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 output(unsigned char ret_buffer, int curr_buffer_size, bool& endoffile) { int index2=0; int addr=0; unsigned char outBuff;
4
6826
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 System.IO.FileSystemEventArgs) Dim strFilename As String = "c:\web\example.mdb"
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2779
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 we have to send another system

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.