0

VS and CODEBLOCKS compile and run fine . When I try Dev c++ or Online GDB I get error.

// Ex7.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;

//ASKISI 1
int getMedian(int arr[]);
//ASKISI 2

int main()
{
    int arr[9];

    for (int i = 0; i < 9; i++)
    {
        std::cout << "Enter num " << i << ": ";
        std::cin >> arr[i];
    }

    std::cout << "\nMedian of array is: " << getMedian(arr) << "\n\n";

    string line;
    int len = 0;

    ifstream myfile("myfile2.txt");
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            cout << line << '\n';
            len += line.length();
        }

        myfile.close();
    }
    else
        cout << "Unable to open file";

    getchar();

    int c;
    cout << "\nEnter char: ";

    c = getchar();
    if (c == 101)
    {
        FILE* f;
        fopen_s(&f, "myfile2.txt", "rb");

        fseek(f, 0, SEEK_END);
        long fsize = ftell(f);
        fseek(f, 0, SEEK_SET);

        char* string = (char*)malloc(fsize + 1);
        fread(string, fsize, 1, f);

        fclose(f);

        string[fsize] = 0;
        int sum = 0;
        int cnt = 0;;
        int len = sizeof(string) / sizeof(*string);

        for (int i = 0; i < len - 1; i++)
        {
            sum += string[i] + string[i + 1];
            cnt++;
        }
        cout << "\nAverage 1+2nd: " << sum / cnt << "\n" << endl;

        cnt = 0;
        for (int i = 0; i < len - 2; i++)
        {
            sum += string[i] + string[i + 2];
            cnt++;
        }
        cout << "\nAverage 1+3nd: " << sum / cnt << "\n" << endl;

        cnt = 0;
        for (int i = 0; i < len - 3; i++)
        {
            sum += string[i] + string[i + 3];
            cnt++;
        }
        cout << "\nAverage 1+3nd: " << sum / cnt << "\n" << endl;

        int i = 0, alphabet[26] = { 0 }, j;
        while (string[i] != '\0')
        {
            if (string[i] >= 'a' && string[i] <= 'z')
            {
                j = string[i] - 'a';
                ++alphabet[j];
            }
            ++i;
        }

        cout << "Frequency of all alphabets in the string is:" << endl;

        for (i = 0; i < 26; i++)
            cout << char(i + 'a') << " : " << alphabet[i] << endl;

        ofstream fout("ex3.txt");
        if (!fout)
        {
            cerr << "Could not open file." << endl;
            return 1;
        }

        for (i = 0; i < 26; i++)
        {

            fout << char(i + 'a') << " : " << alphabet[i] << endl;
        }

        fout.close();
    }
}

int getMedian(int arr[])
{
    return arr[5];
}
D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 1
    What error? Providing information is an important part of getting problems solved. Your `getMedian()` is bad. It only works if an array is of a certain size, but not the size you created. You would want your fifth element, which is at index 4. But just pass the size and figure out what element it should be instead. The program itself is way too scattershot as well. – sweenish Feb 09 '21 at 15:16
  • 1
    Does this answer your question? [Is there a way to use fopen\_s() with GCC or at least create a #define about it?](https://stackoverflow.com/questions/1513209/is-there-a-way-to-use-fopen-s-with-gcc-or-at-least-create-a-define-about-it) – drescherjm Feb 09 '21 at 15:25
  • @sweenish why dont you suggest a solution instead of judging the code? write something better that works and post it so you can really help. – gijiwe5846 Feb 09 '21 at 21:34
  • What solution? You don't describe your problem adequately enough. Your question has been marked as a duplicate, meaning you didn't put enough research in before asking. I'd love to help, but it's on you to provide enough information first. We can't read your mind. Have you read [ask]? Have you read [mre]? Have you taken the [tour]? – sweenish Feb 09 '21 at 22:00

1 Answers1

0

Change fopen, to fopen_s.

Like this: errno_t err = fopen_s(&f, "myfile2.txt", "rb");