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? - #include <iostream.h>
-
#include <stdlib.h>
-
#include <cmath>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
float const gravity = 9.8;
-
double velocity;
-
double seconds;
-
double height;
-
ofstream launch;
-
-
int main()
-
{
-
launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
-
launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters)"<<'\n'<<'\n';
-
cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
-
cin>>velocity;
-
-
seconds = 1;
-
while(height > 0)
-
{
-
height = velocity * seconds - 1/2 * gravity * pow(seconds,2);
-
launch<<setw(13)<<seconds<<setw(8)<<" "<<setw(14)<<height<<'\n';
-
seconds++;
-
}
-
-
launch.close();
-
system("PAUSE");
-
return 0;
-
}
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.
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.
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.
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!
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?
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.
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.
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.
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.
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.
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. - #include <iostream.h>
-
#include <stdlib.h>
-
#include <cmath>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
float const gravity = 9.8;
-
float velocity;
-
float seconds;
-
float height;
-
ofstream launch;
-
-
int main()
-
{
-
launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
-
launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters/Seconds Squared)"<<'\n'<<'\n';
-
cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
-
cin>>velocity;
-
seconds=1;
-
height=0;
-
launch.setf(ios||fixed||ios||showpoint);
-
launch<<setprecision;
-
for(seconds=1;height >= 0;seconds++)
-
{
-
height = velocity * seconds - (1.0/2.0 * gravity * pow(seconds,2));
-
if (height<0)
-
launch<<setw(13)<<seconds<<setw(8)<<" "<<setprecision(2)<<0<<'\n';
-
else
-
launch<<setw(13)<<seconds<<setw(8)<<" "<<setprecision(10)<<height<<'\n';
-
}
-
launch.unsetf(ios||fixed||ios||showpoint);
-
launch<<unsetprecision;
-
launch.close();
-
system("PAUSE");
-
return 0;
-
}
Ganon11 3,652
Recognized Expert Specialist
What errors are you getting?
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 `||'
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.
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. -
#include <iostream.h>
-
#include <stdlib.h>
-
#include <cmath>
-
#include <iomanip>
-
#include <fstream>
-
-
using namespace std;
-
float const gravity = 9.8;
-
float velocity;
-
float seconds;
-
float height;
-
ofstream launch;
-
-
int main()
-
{
-
launch.open("C:\\Documents and Settings\\All Users\\Desktop\\launch.dat");
-
launch<<setw(13)<<"Time(Seconds)"<<setw(8)<<" "<<setw(14)<<"Height(Meters/Seconds Squared)"<<'\n'<<'\n';
-
cout<<"Please enter the the launch velocity in meters/seconds squared."<<'\n';
-
cin>>velocity;
-
seconds=1;
-
height=0;
-
launch.setf(ios::showpoint||ios::fixed);
-
launch<<setprecision(2);
-
for(seconds=1;height >= 0;seconds++)
-
{
-
height = velocity * seconds - (1.0/2.0 * gravity * pow(seconds,2));
-
if (height<0)
-
launch<<setw(2)<<seconds<<setw(8)<<" "<<setw(10)<<0<<'\n';
-
else
-
launch<<setw(2)<<seconds<<setw(8)<<" "<<setw(10)<<height<<'\n';
-
}
-
launch.unsetf(ios::showpoint||ios::fixed);
-
launch.close();
-
system("PAUSE");
-
return 0;
-
}
I tried changing the precision numbers but it made it just sent things crazy lol.
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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....
|
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...
|
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++)...
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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 ...
|
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...
| |