1

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?

  • 2
    `int mainKinematics();` -- Read up on how to call functions. This does not call a function. -- *The code compiles without error,* -- This is the danger of C++, and that is having code that does something that is totally different than what you expect just happens to compile. – PaulMcKenzie Mar 30 '18 at 19:11
  • 3
    Your title is kind of misleading, since you don't have nested function, which isn't allowed in C++ anyway. – Some programmer dude Mar 30 '18 at 19:11
  • 1
    As for your problems, I suggest you get [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read them. – Some programmer dude Mar 30 '18 at 19:11
  • @Some programmer dude Unfortunately GCC actually allows nested functions as an extension by default :-/ – Jesper Juhl Mar 30 '18 at 19:13
  • @JesperJuhl True, unfortunately. But it's still not the issue here. – Some programmer dude Mar 30 '18 at 19:14
  • @JesperJuhl -- *Unfortunately GCC actually allows nested funcrions as an extension by default* -- So now we have that to deal with too? The VLA stuff is difficult enough. – PaulMcKenzie Mar 30 '18 at 19:14
  • @Some programmer dude - agreed. Not the issue here. Just wanted to mention it. – Jesper Juhl Mar 30 '18 at 19:15
  • @PaulMcKenzie Not my doing. Just pointing out an unpleasant fact. – Jesper Juhl Mar 30 '18 at 19:16
  • Thank you for all the prompt responses! I'll read up on how to call a function in C++. Does the formatting for my header files look wonky? – mosstheinterrupter Mar 30 '18 at 19:22
  • A question very similar to this one was asked just the other day, but I can't find it now. You should always search for existing questions before asking new ones – Remy Lebeau Mar 30 '18 at 19:25
  • @JesperJuhl GCC Does NOT allow nested functions by default, you can have structs inside a function (which may contain functions) but that is standard-legal. – Joseph Franciscus Mar 30 '18 at 19:31
  • @Joseph Franciscus I'm sorry, but you are wrong. Read [the documentation](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html) if you don't believe me. You could also just try it out and you'll see that nested functions compile and work just fine with gcc unless you use `-std=`some-non-gnu-version (but the gnu variant is the *default*). G++ however does *not* allow them when compiling C++ code - just for C. – Jesper Juhl Mar 30 '18 at 19:35
  • You may want to simplify your life and not use Precompiled Headers. If you change a header, add a header or remove a header, they need to be recompiled. Also Precompile Headers usually have significant build performance for large or huge projects. – Thomas Matthews Mar 30 '18 at 19:36
  • @JesperJuhl That is true C not for C++, so you are right but I assumed we were talking within the C++ context. (Given that this is C++ program, that statement really isn't relevant) – Joseph Franciscus Mar 30 '18 at 19:39
  • (after edit) For this question to be useful (to others), it needs major editing for terminology (_e.g._, “nested” and “structure” don’t have those meanings) and brevity. Unlike many questions, you can and should omit all the code except about 1/3 of the main file and your proposed fix. – Davis Herring Apr 03 '18 at 03:26

0 Answers0