0

I'm trying to calculate a list of closing price for SMA. My program was supposed to be calculating like this. And the value of first day of 5-day SMA will be stored in array with index (time_frame-1).

Daily Closing Prices:
1.4660, 1.4627, 1.4600, 1.4607, 1.4609, 1.4691, 1.4687
First day of 5-day SMA:
(1.4660 + 1.4627 + 1.4600 + 1.4607 + 1.4609) / 5 = 1.4621
Second day of 5-day SMA:
(1.4627 + 1.4600 + 1.4607 + 1.4609 + 1.4691) / 5 = 1.4627
Third day of 5-day SMA:
(1.4600 + 1.4607 + 1.4609 + 1.4691 + 1.4687) / 5 = 1.4639

This is my typedef struct

typedef struct
{
    char date[11];
    double price;
    double SMA;
    double EMA;
}DATA;

This is the main function.

int main(void)
{
    DATA forex[100];
    int time_frame, count;
    get_data(&time_frame, &count, forex);
    calculate_SMA(time_frame, count, forex);

    return 0;
}

This function is to read from my text file "forexPrice.txt" and get user input for time_frame

void get_data(int *time_frame, int *count, DATA forex[])
{
    int i = 0;
    bool cont;

    ifstream infile("forexPrice.txt", ios::in);
    if (!infile)
        cout << "Error opening input file!";
    else
    {
        *count = 0;
        do
        {
            infile >> forex[i].date >> forex[i].price;
            cout << forex[i].date << "\t" << fixed << setprecision(4) << forex[i].price << endl;
            i++;
            (*count)++;
        } while (!infile.eof());
        infile.close();
    }

    do
    {
        cont = true;
        cout << "Enter the number of days to calculate SMA and EMA: ";
        cin >> *time_frame;
        if (*time_frame < 1 || *time_frame >= *count)
        {
            cout << "Do not enter number smaller than 1 or larger than " << *count << ". Please enter again.\n";
            cont = false;
            //break;
        }
    } while (cont == false);
}

This is the function to calculate SMA and error has shown at line price_total += forex[i].price; when i use debugger.

void calculate_SMA(int time_frame, int count, DATA forex[])
{
    double sma, price_total;
    for (int j = time_frame - 1; j < count; j++)
    {
        price_total = 0.0;

        for (int i = 0; i < i + time_frame; i++)
            price_total += forex[i].price;

        forex[j].SMA = price_total / time_frame;
        cout << forex[j].SMA << endl;
    }
}
5
  • Please post a minimal reproducible example. What is the input? May 12, 2020 at 12:26
  • @idclev463035818 5
    – OYS
    May 12, 2020 at 12:30
  • 5 is the input from the user, but what is inside the file? (best would be to include the input in the code instead of reading from file, assuming the error is not from the file reading part). Also you posted to functions but who calls them? What is the arguments to that functions? The already pointed out mistake can be the cause of the runtime error, but without a complete example there is no way to be sure about that May 12, 2020 at 12:34
  • If your're building something from scratch here then think carefully about using a double to represent money values. Can you use a decimal type? Boost has one.
    – Bathsheba
    May 12, 2020 at 12:34
  • while (!infile.eof()); is a bug. There are multiple fundamental bugs in the shown code. This is what happens when you write a huge pile of code first, and only then try to figure out if it works. Mass confusion results, due to the plethora of bugs in play. You should start over from scratch. Write only a few lines of code, or a small function, first. Test it. Make sure it works. Write more code. And so on. May 12, 2020 at 12:37

1 Answer 1

1

Think about this

for (int i = 0; i < i + time_frame; i++)

when is that loop ever going to ever stop?

I guess you meant this

for (int i = 0; i < time_frame; i++)
3
  • but how can i make it increase accordingly everytime my i increase, for example, if my time_frame=5, i want the loop to add all numbers from array index 0 to 4, then for the second time, i want the loop to add all the numbers from array index 1 to 5
    – OYS
    May 12, 2020 at 12:33
  • @YingShuang Well I'm not exactly sure, because I don't know precisely what you are trying to do, but I guess something like for (int i = j; i < j + time_frame; ++i)
    – john
    May 12, 2020 at 12:34
  • thanks for giving me the idea! i've changed it to for (int j = time_frame - 1, k = 0; j < count && k < count; j++, k++) and for (int i = k; i < k + time_frame; ++i) and now it finally works!!
    – OYS
    May 12, 2020 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.