1

I am learning c++ and decided to make a program that estimates the value of sine and cosine using the Taylor series method.

There are no compiler errors, however when I run the program the moment the user enters a value the program just closes. I tried to use a hard value and the program shuts down right away as well. How can I fix this?

#include <iostream>

float ConvertToRad(float x)
{
    const float Pi = 3.14159265358979323;
    return x * (Pi / 180);
}

float sine(float x)
{
    // Function to estimate the sine of a real value x
    return x - (x * x * x) / 6 + (x * x * x * x * x) / 120;
}

float cosine(float x)
{
    // Function to estimate the cosine of a real value x
    return 1 - (x * x) / 2 + (x * x * x * x) / 24;
}

int main()
{

    float val;
    float ValInRads;
    float SineValue;
    float CosineValue;

    val = 22;
    std::cout << "Input a real number\n";
    std::cin >> val;

    ValInRads = ConvertToRad(val);

    SineValue = sine(ValInRads);
    CosineValue = cosine(ValInRads);

    std::cout << "Sine of ( " << val << " ) = " << SineValue << "\n";
    std::cout << "cosine of( " << val << " ) = " << CosineValue << "\n";
}
cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

0

The program is not crashing it is just reaching the end of its code. You can add

system("pause");

to the end of you int main to keep the app from closing

or If you are using visual studio 2019 hit f5 to run your program. It will not exit when It is done

  • This is a pretty hack way of doing it. It's better to set up the IDE so that it doesn't immediately close the app, or run it like you suggest. – tadman Oct 28 '20 at 02:24
  • An app like this is simple enough where it will not affect it much, and I think they ran the compiled executable, not using a debugging in an IDE. – Colin Sutter Oct 28 '20 at 02:27
  • 1
    [Why is system("pause") wrong?](https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – PaulMcKenzie Oct 28 '20 at 02:30