Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my variables keep resetting themselves?

Tags:

c

All the variables included in my Stats struct continuously reset themselves after I've called updateStats. Am I not referencing or passing my Stats variable correctly? Not sure what other info to give as this is the first question I've asked, but apparently I need to type more. If it helps this program is supposed to represent a very basic version of the Lunar Lander simulation game.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct _stats
{
    double altitude, velocity, mass, fuel, acceleration;
};
typedef struct _stats Stats;

void printStats(Stats a, double thrust)
{
    printf("Thrust: %f  Altitude: %f  Velocity: %f  Mass: %f  Fuel: %f\n", thrust, a.altitude, a.velocity, a.mass, a.fuel);
}
void updateStats(Stats a, double thrust, int time)
{
    a.acceleration += (thrust / a.mass -1.6) * time;
    a.mass -= thrust / 3000.0;
    printf("aaaaa%f", a.mass);
    a.fuel -= thrust / 3000.0;
    a.velocity += a.acceleration * time;
    a.altitude += a.velocity * time;
}
double thrustAllowed(Stats a, double thrust)
{
    if (thrust/1000 <= 45 && thrust <= (3000 * a.fuel))
    {
        return thrust;
    }
    else
        return (3000*a.fuel)/1000;
}

int main(void)
{
    FILE* inputFile = fopen("simulation.csv", "wt");
    if (!inputFile)
    {
        printf("Unable to open file.\n");
        return 1;
    }

    int time = 0;
    double thrust = 0;
    Stats rocket;
    rocket.altitude = 150000;
    rocket.velocity = -325;
    rocket.mass = 9000;
    rocket.fuel = 1800;
    rocket.acceleration = 0;
    while (time < 150)
    {
        time++;
        fprintf(inputFile, "Enter a Thrust in kN: ");
        scanf("%lf", &thrust);
        fprintf(inputFile, "%f", thrust);
        thrust = thrust * 1000;
        if (thrustAllowed(rocket, thrust) == thrust)
        {
            updateStats(rocket, thrust, time);
            fprintf(inputFile, "\nThrust: %.1f  Altitude : %.1f  Velocity : %.1f  Mass : %.1f  Fuel : %.1f\n", thrust, rocket.altitude, rocket.velocity, rocket.mass, rocket.fuel);
            if (rocket.altitude <= 0)
            {
                if (rocket.velocity <= 0 && rocket.velocity >= -1)
                {
                    fprintf(inputFile, "Soft Landing!!!");
                    break;
                }
                else
                {
                    fprintf(inputFile, "You broked it");
                    break;
                }
            }
        }
        else
            fprintf(inputFile, "Thrust must be <= %.1f\n", thrustAllowed(rocket, thrust));
    }
    fclose(inputFile);
}'''
like image 901
rizoch Avatar asked Mar 09 '26 04:03

rizoch


1 Answers

You need to use pointers for variables you want to change:

void updateStats(Stats* a, double thrust, int time) {
    a->acceleration += (thrust / a->mass -1.6) * time;
    a->mass -= thrust / 3000.0;
    printf("aaaaa%f", a->mass);
    a->fuel -= thrust / 3000.0;
    a->velocity += a->acceleration * time;
    a->altitude += a->velocity * time;

}

and then call it like updateStats(&rocket, thrust, time);.

like image 159
nik7 Avatar answered Mar 12 '26 02:03

nik7