Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 12-25-2006, 11:46 PM   #1 (permalink)
Newbie
 
Join Date: Jan 2006
Posts: 34
Default I wrote a small program.

Technically speaking, it's just a program that will use two variables to find a third, based on a provided equation embedded in the code. I've thoroughly tested it (and had a hell of a lot of fun doing it) and it works.

The program itself can be used to find the torque or horsepower of a car. Here is the complete source code, if you care to compile it yourself and check it out:

Code:
// Spunkster's Carshop v1.0
#include <iostream>

using namespace std;

int HorsepowerEquation (int first, int second) // Horsepower equation function declaration.
{
    cout << "Received " << first << " and " << second << "\n";
    return (first * second / 5252);
}
int TorqueEquation (int first, int second) // Torque equation function declaration.
{
    cout << "Received " << first << " and " << second << "\n";
    return (first * 5252 / second);
}
int main()
{
    string answer;
    
    cout << "Welcome to Spunkster's Carshop.\n";
    cout << "Are you trying to find horsepower, or torque?: ";
    cin >> answer;
    
    if (answer == "Horsepower" || answer == "horsepower" || answer == "hp")
    {
               int horsepower;
               int torque;
               int rpm;
               
               cout << "Please enter the torque: ";
               cin >> torque;
               cout << "Please enter the RPM: ";
               cin >> rpm;
               
               horsepower = HorsepowerEquation(torque,rpm); // Calls the HorsepowerEquation function, using the input for torque and rpm.
               
               cout << "Horsepower is: " << horsepower << endl;
               cout << "Press enter to exit...";
               cin.get();
               cin.ignore();
    }
    else if (answer == "Torque" || answer == "torque")
    {
         int horsepower;
         int torque;
         int rpm;
         
         cout << "Please enter the horsepower: ";
         cin >> horsepower;
         cout << "Please enter the RPM: ";
         cin >> rpm;
         
         torque = TorqueEquation(horsepower,rpm); // Calls the TorqueEquation function, using the input for horsepower and rpm.
         
         cout << "Torque is: " << torque << endl;
         cout << "Press enter to exit...";
         cin.get();
         cin.ignore();
    }
    else
    {
        cout << "The program seems to have returned an error.\n";
        cout << "Please restart and try again.\n";
        cout << "Press enter to exit...";
        cin.get();
        cin.ignore();
    }
    return 0;
}
Crits and comments are welcome here, as long as they are constructive. Feel free to use the source for whatever you like, I don't really care. I wrote this because I have a passion for cars, incase you couldn't tell, and I release it here for anyone else who wishes to experiment with it, and maybe learn a thing or two from the source.

If you find any bugs, submit them here, and I'll check it out.
Spunky_Man is offline   Reply With Quote
Old 12-26-2006, 12:53 AM   #2 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default

instead of the conditional ors, you may want to convert the input strings to all upper case, and just do one comparison.
looks good otherwise.
Nochte is offline   Reply With Quote
Old 12-26-2006, 01:13 AM   #3 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by Nochte View Post
instead of the conditional ors, you may want to convert the input strings to all upper case, and just do one comparison.
looks good otherwise.
Well, cut down on one condition from each if (since just converting it to upper-case wouldn't catch "hp").

Also, as a matter of readability, constants like 5252 shouldn't be in the code verbatim, but should be used through a constant:

Code:
// C-style
#define HT_CONSTANT 5252

// C++-style
const int HT_CONSTANT = 5252;
(Note that I make the distinction out of popular opinion as to which should be used given which language).

Or go a step further and get a more exact value for horsepower or torque
Code:
const double PI = 3.14159;
const double HT_CONSTANT = 16500 / PI;
You could also include <cmath> for M_PI, but you also have to define _USE_MATH_DEFINES to get it, at least with the Dinkumware libraries included with VS 8.0.

Oh, and HT_CONSTANT is horsepower/torque constant, only because I couldn't think of a better name for it.

Also, you repeat cin.get() and cin.ignore() in each control path, it would be better practice to have it once after the if and else blocks, and, a better alternative to using both cin.get() and cin.ignore() is to use a single cin.ignore() but make it ignore the maximum amount of characters possible and stop on a newline, ala:

Code:
#include <iostream>  // For std::cin
#include <limits>  // For std::numeric_limits

int main()
{
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Last edited by Sep102; 12-26-2006 at 01:36 AM.
Sep102 is offline   Reply With Quote
Old 12-26-2006, 04:05 AM   #4 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

As far as cin.get() and cin.ignore, couldn't one just use _getch();

(yea, I'm bored)

Last edited by Axle; 12-26-2006 at 04:07 AM.
Axle is offline   Reply With Quote
Old 12-26-2006, 02:29 PM   #5 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Yeah, you could, it just isn't portable across compilers since _getch() (and getch(), and any other permutation including getch(), for that matter) isn't a standard library function, whereas istream::get() and istream::ignore() are.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5