|
||
|
|
#1 (permalink) |
|
Newbie
Join Date: Jan 2006
Posts: 34
|
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;
}
If you find any bugs, submit them here, and I'll check it out. |
|
|
|
|
|
#3 (permalink) | |
|
Forum Expert
|
Quote:
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; 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; 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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|