CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: for loop issue

Threaded View

  1. #1
    Join Date
    Jul 2009
    Posts
    22

    for loop issue

    Hi all...I'm new to programming and working on a project to determine velocity and position. the problem is the for loop I'm using doesn't follow the rules as I'm trying to implement them. That is, if the velocity (finalVeloc) is equal to or less than -500, the position (finalPos) should be determined by subtracting 500 and the velocity should be set to -500.

    Here's what I have so far:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    double positionCalc(double, double, double);
    double velocityCalc(double, double);
    
    int main(){
        
        double time = 0;
        double initialPos, finalPos, finalVeloc, initialVeloc; 
        
        cout << setw(60) << setfill('*') << " " << endl;
        cout << setw(53) << setfill(' ') << "Sir Isaac Newton's Position/Velocity Calculator" << endl;
        cout << setw(60) << setfill('*') << " " << endl;
        cout << endl;
        cout << endl;
        cout << "This program will calculate the position and velocity of a falling object." << endl;
        cout << endl;
        cout << "Enter the initial height in feet (above 1000): ";
        cin >> initialPos;
            while (initialPos < 1000){
                  cout << "Invalid answer. Must be height over 1000." << endl;
                  cout << "Enter the initial height in feet (above 1000): ";
                  cin >> initialPos;
                  cout << endl;
                  }
        
        cout << endl;          
        cout << "Enter the initial velocity in ft/sec: ";
        cin >> initialVeloc;
            while (initialVeloc < -500){
                 cout << "Invalid answer. Must be speed less than -500 ft/sec." << endl <<endl;
                 cout << "Enter the initial velocity in ft/sec: ";
                 cin >> initialVeloc;
                 cout << endl;
                 }
        
        finalPos = 1000;
        finalVeloc = 0;
        cout << setfill(' ') << "Time" << setw(15) << "Position" << setw(20) << "Velocity" << endl;
        cout << "----" << setw(15) << "--------" << setw(20) << "--------" << endl;
        
        for (time = 0; finalPos >= 1000; time++){
            if (finalVeloc <= -500){
                           finalPos -= 500;
                           finalVeloc = -500;
                           }
            else{
                 finalVeloc = velocityCalc(initialVeloc, time);
                 finalPos = positionCalc(initialPos, initialVeloc, time);
                 }
            cout << setw(4) << setprecision(2) << fixed << time << setw(15) << finalPos << setw(20) << finalVeloc << endl;
            }
        
        system("PAUSE");
        return 0;
    }
    
    //calculates the position of the object
    double positionCalc(double initialPos, double initialVeloc, double time){
           double finalPos;
           finalPos = -16 * pow(time, 2) + initialVeloc * time + initialPos;
           return finalPos;
           }
    //calculates the velocity of the object
    double velocityCalc(double initialVeloc, double time){
           double finalVeloc;
           finalVeloc = -32 * time + initialVeloc;
           return finalVeloc;
           }
    Last edited by reubenpatterson; July 22nd, 2009 at 10:22 AM. Reason: added code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured