PDA

View Full Version : Am I doing this right (C++ question)



Artha
12-27-2004, 09:02 PM
So I'm finally doing my science project, I'm wondering before I get too involved if this looks about right.


#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int sel;
double kinener(int mass, int volume)

int main()
{
void menu();

return 0;
}

void menu(int sel)
{
cout <<"Formula List."<<endl;
cout <<"----"<<endl;
cout <<"1. Kinetic Energy."<</t<<"2. Speed"<</t<<"3. Average Acceleration."<<endl;
cout <<"4. Force of a Moving Object."<</t<<"5. Vector Resultant."<</t"<<"6. Specific Heat."<<endl;
cout <<"7. Pressure."<</t<<"8. Density"<</t<<"9. Mass."<<endl;
cout <<"10. Volume."<</t<<"11. Electrical Charge."<</t<<"12. Electrical Power."<<endl;
cout <<"13. Potential Difference."<</t<<"14. Percent Error."<</t<<endl;
cout <<"----"<<endl;
cout <<"Please input the number of the formula you wish to solve: ";
cin >>sel;
cout <<endl;

If (sel=1);
{
cout <<"Mass: ";
cin >>mass;
cout <<endl;
cout <<"Velocity: ";
cin >>velocity;
cout <<endl;
double kinener(x, y);
}
If (sel=2);
{
cout <<"Distance: ";
cin >>dist;
cout <<endl;
cout <<"Time: ";
cin >>time;
cout <<endl;
}

That last If block isn't done yet and there're going to be about 11 more. Thing is, I haven't learned how to do Ifs in class yet and the MSDN isn't super helpful. So if one of you uber programmers could provide guidance, I'd appreciate it.

imported_Kranar
12-27-2004, 09:26 PM
Sticking to standard C++ coding conventions, this is what your program should look like:



#include <iostream>
#include <iomanip>
#include <math.h>
#include <conio.h>

using namespace std;

double kinener(double mass, double velocity) {
return 0.5 * mass * velocity * velocity;
}

int main() {
int sel = 0;
cout << "Formula List.\n";
cout << "-------------\n";
cout << "1. Kinetic Energy.\n";
cout << "2. Speed\n";
cout << "3. Average Acceleration.\n";
cout << "4. Force of a Moving Object.\n";
cout << "5. Vector Resultant.\n";
cout << "6. Specific Heat.\n";
cout << "7. Pressure.\n";
cout << "8. Density\n";
cout << "9. Mass.\n";
cout << "10. Volume.\n";
cout << "11. Electrical Charge.\n";
cout << "12. Electrical Power.\n";
cout << "13. Potential Difference.\n";
cout << "14. Percent Error.\n";
cout << "Please input the number of the formula you wish to solve: ";
cin >> sel;
cout << endl;

if (sel == 1) {
double mass = 0., velocity = 0.;
cout << "Mass: ";
cin >> mass;
cout << "Velocity: ";
cin >> velocity;
cout << "The kinetic energy of the object is: ";
cout << kinener(mass, velocity) << " J\n";
} else if (sel == 2) {
double dist = 0., time = 0.;
cout << "Distance: ";
cin >> dist;
cout << endl;
cout << "Time: ";
cin >> time;
cout << endl;
}
getch();
}


[Edited on 12-28-2004 by Kranar]

imported_Kranar
12-27-2004, 09:37 PM
Some of the mistakes seem to stem from VisualBASIC coding practices, which will NOT work in C++. Going from VB to C++ is incredibly challenging since many habits need to be unlearned. If this may be the case, check if you're allowed to program this in VB.

Otherwise, here are the corrections that were made.

1) The function main should rarely ever return a value, infact as far as you're concerned, it should never return a value. It is declared as an int so that the operating system can return a value for it when the program terminates, not the programmer.

2) You must declare all your variables and initialize them before you make use of them. Don't have uninitialized variables... for numerical data types, it's best to initialize them to 0, for pointers, initialize them to NULL.

3) All keywords are lowercase, in your case, the keyword "if" needs to be lowercase, "if" not "If."

4) You confused the assignment operator "=" with the equality operator "==". If you want to check whether a variable x is logically equal to another variable y, you do "x == y", not "x = y". The latter will assign the value of y to the value of x.

5) Don't use global variables. There are very small exceptions to this rule, but they will come way later. For now, avoid global variables like the plague.

There were some other points I wanted to make but I forgot, the above should get you on the right track for now, though. They may not make too much sense for now, but in the long run breaking those rules leads to highly unmanagable, unmaintainable code.

[Edited on 12-28-2004 by Kranar]

Artha
12-28-2004, 10:25 AM
If this may be the case, check if you're allowed to program this in VB.

It is. I'm sure I could do it in VB, but I figured it'd be less hastle to do it in C++.


Thanks, Kranar.