I really hope this falls within guidelines, but I have a potential silly question: I'm trying to get a good feel of how multiple header and source files fit together in a C++ program. To do so, I'm making a calculator for simple physical formulae.
#include "stdafx.h"
#include "rotationalKinematics.h"
#include "Kinematics.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//declare local variables:
int fieldChoice = 0;
//query field of equations:
cout << "What physical properties would you like to solve for?\n (1) kinematics\n (2) rotational kinematics\n Please enter number: ";
cin >> fieldChoice;
if (fieldChoice == 1) {
int mainKinematics();
return 0;
}
if (fieldChoice == 2) {
int mainRotationalKinematics();
return 0;
}
return 0;
}
The program compiles without error, and the two functions mainKinematics() and mainRotationalKinematics() are defined and declared in separate .cpp and .h files, respectively. For example, I've included the a header and a source file for mainKinematics() below:
#include "stdafx.h"
#include "Kinematics.h"
#include <iostream>
#include <cmath>
using namespace std;
//function declaration:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration);
int mainKinematics()
{
//local variable declaration:
double velocityInitial = 0;
double velocityFinal = 0;
double velocityFinalSquared = 0;
double acceleration = 0;
double deltaX = 0;
double deltaTime = 0;
int solveFor = 0;
//query variable to solve for:
cout << "What variable would you like to solve for?\n (1) DeltaX w.r.t. acceleration\n (2) DeltaX w.r.t. velocity\n (3) Final velocity\n (4) Final velocity squared\n Please enter number: ";
cin >> solveFor;
//query for values based on solveFor choice:
if (solveFor == 1) {
cout << "Please enter value for velocityInitial: ";
cin >> velocityInitial;
cout << "Please enter value for deltaTime: ";
cin >> deltaTime;
cout << "Please enter value for acceleration: ";
cin >> acceleration;
//function call:
deltaX = kinDeltaX_acceleration(velocityInitial, deltaTime, acceleration);
cout << deltaX;
}
if (solveFor != 1 && solveFor != 2 && solveFor != 3 && solveFor != 4)
{
return 0;
}
return 0;
}
//functions:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration)
{
//local variable declaration:
double result;
result = velocityInitial*deltaTime + .5 * acceleration * pow(deltaTime, 2);
cin.clear();
cin.sync();
cin.get();
return result;
}
and the related Kinematics.h header file:
#ifndef Kinematics
#define Kinematics
#pragma once
//function declarations:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration);
double kinDeltaX_velocity(double velocityInitial, double velocityFinal, double deltaTime);
double kinVelocityFinal(double velocityInitial, double acceleration, double deltaTime);
double kinVelocityFinalSquared(double velocityInitial, double acceleration, double deltaX);
int mainKinematics();
#endif
When I solely compiled the Kinematics.cpp file (above) it worked fine. However, when I tried to tie the Kinematics.cpp file and the rotKinematics.cpp files together into one overarching main file (shown in the first code example) I run into the following problem: the code compiles without error, it takes user input for which function to access, and then immediately closes without seeming to execute the called function from a separate source file. Am I doing something wrong in the way I'm calling the functions?
Any help is appreciated immensely and thank you for your time!!
P.S. I realize that I declare the functions in both the .cpp and .h files; when I do not include the declaration in the .cpp file the compiler returns errors. This suggests that I'm doing something wrong, though I haven't been able to figure out what.
UPDATE
Hello! Thank you all so much for weighing in on this! With the resources provided I've found a workaround as such:
if (fieldChoice == 1) {
int val = -1;
val = mainKinematics();
return 0;
Referencing the function in my original script wasn't actually returning anything; by creating an intermediate object (val) to hold the final value the script now seems to work as intended. What do you folks think? Is this an okay solution?