Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C++ Help with force calculator

Herman09851

New Coder
Hi, I am a newbie to c++ and for college, I need to make a force calculator. I have done this so far but it won't calculate the Fx and Fy when I run it like this.
Does anybody know what I am doing wrong? ps. I need to use a void function in my code and I'm dutch that's why there are some lines in dutch in the code 🙂

Code:
using namespace std;
double Force = 1.0, Angle, AmountF, Quadrant, Fx, Fy;

void checkkwadrant()
{
    if (Quadrant == 2) {
        //Quadrant 2 dus ten op zichte van xas in Quadrant 1 is een AmountF in Quadrant twee 180 - hoek weg van de x-as in Quadrant 1
        Angle = 180 - Angle;
        Force == -Force;
        
    }

    else if (Quadrant == 3) {
        Angle = 180 + Angle;
        Force == -Force;
        
    }

    else if (Quadrant == 4) {
        Angle = 360 - Angle;
        Force == -Force;
        
    }

    else if (Quadrant != 1 && Quadrant != 2 && Quadrant != 3 && Quadrant != 4)
    {
        //stap 5
        cout << "Kwadrant " << Quadrant << " bestaat niet " << endl;
        cout << "Vul een bestaande waarde in! ";
        cout << "" << endl;
        Fx = 0;
        Fy = 0;
    }

    Fx = Force * cos(Angle);
    Fy = Force * sin(Angle);
}

int main()
{

    double Fx = 0.0, Fy = 0.0;
    //stap 1:
    cout << "Geef aantal krachten die berekend moeten worden: ";
    //stap 2
    cin >> AmountF;
    cout << "" << endl;
    //stap 3
    cout << "Voer grootte in (zonder eenheid), hoek (in graden, vanaf x-as) en richting (welk Quadrant) in van elke vector." << endl;

    for (int i = 0; i < AmountF; i++) {
        //stap 4

        cin >> Force;
        cin >> Angle;
        cin >> Quadrant;

        //stap 6
        //6a
        Angle *= 3.14159 / 180;

        //6b
        checkkwadrant();

        //6c
        cout << "Fx = " << Fx << " en Fy = " << Fy << endl;
    }

    //Force = sqrt(Fx * Fx + Fy * Fy);
    //Angle = atan2(Fy, Fx) * 180.0 / 3.14159;

    cout << "Het resultant van de vector heeft een grootte: " << Force << " met een hoek van: " << Angle << " graden vanaf de x-as " << endl;

}
 
The problem is likely to be that you write Force == -Force. This not an assignment, but a comparison. It's a boolean expression, which does not change Force (as you intended it to), but just returns false (unless Force was zero). Make that Force = -Force and things may work better. Remember to change it 3 times 😉 Personally I would use a switch (kwadrant) statement rather than this convoluted if-else tree. Much easier to follow and maintain.

Also, in addition to what BGB suggested, look at the assignments for Fx and Fy. These two
Fx = 0;
Fy = 0;

are useless, as they will be given values right after the if statement. Probably not really important, but not good coding.
 
Last edited by a moderator:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom