Thread: Suggestions?

  1. #1
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68

    Suggestions?

    I am looking for advice on anything in the code below which could be beneficial in some way. Anything that could be added or changed etc:
    Code:
    /*A car and a train are both traveling toward a railroad crossing on a collision course.
    The train is 1200 feet from the crossing and traveling at a constant speed of 40 feet per second.
    The car is 1500 feet away and traveling at 55 feet per second.
    Write a program to calculate and print each vehicles distance from the crossing at one-second intervals.
    If the car gets to the crossing first, print "Made it."
    If the train gets to the crossing first, print "Crash".
    */
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
         int Train_Distance = 1200, Car_Distance = 1500, Train_Speed = 40, Car_Speed = 55, Seconds_Passed = 0;
         std::cout<< "The train and the car race to the cross the intersection. Which will win?"
              << endl << "Seconds Passed = " << Seconds_Passed
              << " Train Distance = " << Train_Distance << " feet"
              << " Car Distance = " << Car_Distance << " feet" << endl;
         while(Train_Distance >= 0 && Car_Distance >= 0)
         {
              std::cout<< "Seconds Passed = " << ++Seconds_Passed
                   << " Train Distance = " << (Train_Distance -= Train_Speed) << " feet"
                   << " Car Distance = " << (Car_Distance -= Car_Speed) << " feet" << endl;
         }
         string winner = "Made it.";
         if(Train_Distance < Car_Distance) winner = "Crash.";
         std::cout<< winner << endl;
         std::cin.get();
         return EXIT_SUCCESS;
    }
    Asking a question you already know the answer to might teach you something you did not know...

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see any major changes that you should make, but here are some things to think about:

    Since you don't appear to be using anything from <cstdlib> other than EXIT_SUCCESS, I would remove that #include and just return 0.

    You're already prefixing std:: to all your cout's, so you can either:
    - Leave the using namespace std; as it is and change nothing.
    - Change it to using std::endl;
    - Remove the std:: before the cout's.
    - Add std:: to the endl's and remove the using namespace std; line.

    I would also add more whitespace to make it easier to read.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm not sure exactly what you're looking for. The code appears to work (I didn't run it), so there's nothing much you can do to semantically improve your code. I can suggest some very minor things though:

    Code:
    using namespace std;
    It's a good idea to never use this line. While it doesn't matter for small test applications, it can cause you problems once you start dealing with bigger projects.

    Code:
         while(Train_Distance >= 0 && Car_Distance >= 0)
    If Train_Distance or Car_Distance is zero, why would you want to loop again?

    Code:
         string winner = "Made it.";
         if(Train_Distance < Car_Distance) winner = "Crash.";
    This is a good place to use the ternary operator.
    Code:
    string winner = (Train_Distance < Car_Distance) ? "Crash." : "Made it.";
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    - Add std:: to the endl's and remove the using namespace std; line.
    It's a good idea to never use this line. While it doesn't matter for small test applications, it can cause you problems once you start dealing with bigger projects.
    Understood and fixed.

    Code:
    while(Train_Distance >= 0 && Car_Distance >= 0)
    I used this because it give the desired results of printing the last passing second which shows the first vehicle has crossed the intersection. The first negative number. I am unaware of a better way to do so. Would be happy to hear suggestions.

    ...ternary operator
    Interesting concept, google here I come.

    Thank you both for your much appreciated feedback.
    Asking a question you already know the answer to might teach you something you did not know...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions?!
    By liljp617 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-20-2008, 11:12 AM
  2. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  3. Math Book Suggestions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-09-2003, 10:43 AM
  4. Need Suggestions
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 05:46 AM
  5. Learning C for OSX, tutorial suggestions please
    By Nexum in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 04:57 AM