Loop Not Working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prosdo
    New Member
    • Nov 2007
    • 32

    #1

    Loop Not Working

    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?

    Code:
    #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;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • Prosdo
      New Member
      • Nov 2007
      • 32

      #3
      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.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Check the contents of launch.dat and see if your output is being written. If not, the file may not be opening.

        Comment

        • Prosdo
          New Member
          • Nov 2007
          • 32

          #5
          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!

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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?

            Comment

            • Prosdo
              New Member
              • Nov 2007
              • 32

              #7
              Originally posted by Ganon11
              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.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                • Prosdo
                  New Member
                  • Nov 2007
                  • 32

                  #9
                  Originally posted by Ganon11
                  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.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    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.

                    Comment

                    • Prosdo
                      New Member
                      • Nov 2007
                      • 32

                      #11
                      Originally posted by Ganon11
                      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.

                      Comment

                      • Prosdo
                        New Member
                        • Nov 2007
                        • 32

                        #12
                        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.

                        Code:
                        #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;
                        }

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          What errors are you getting?

                          Comment

                          • Prosdo
                            New Member
                            • Nov 2007
                            • 32

                            #14
                            Originally posted by Ganon11
                            What errors are you getting?
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp: In function `int main()':
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:22: parse error before `||'
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:22: `fixed' undeclared (first use this function)
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:22: (Each undeclared identifier is reported only once
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:22: for each function it appears in.)
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:22: parse error before `||'
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:32: `unsetprecision ' undeclared (first use this function)
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:33: parse error before `||'
                            c:\docume~1\ste pha~1\desktop\p rogra~1.cpp:33: parse error before `||'

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              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.

                              Comment

                              Working...