0

宿題のために、私はファイルを読んで文字列をソートすることになっています。このために私は選択ソートを使用していて、それは機能します。これで、selSort関数を呼び出した直後にクラッシュします。私はこの問題を解決するためのアイデアを使い果たしました。誰かが私に手を差し伸べることができますか?

// 2_8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <wchar.h>
#include <fstream>
#include <iostream>
using namespace std;

class convert
{
private:
    ifstream myFile;
    TCHAR charArray[1000][25];
    int size;

public:
    convert()
    {
        myFile.open("rand.txt");
        for(int x=0;x<1000;x++)
        {
            for(int y=0;y<25;y++)
            {
                charArray[x][y] = NULL;
            }
        }
        if(!myFile)
        {
            cout << ("File not open") << endl;
        }
        else
        {
            cout << ("File opened") << endl;
        }
    }

    void readFile()
    {
        int x = 0;
        int y = 0;
        int result =0;

        if(myFile.is_open())
        {
            TCHAR tempChar;
            while(!myFile.eof())
            {
                tempChar = myFile.get();
                if(tempChar != 32)
                {
                    charArray[x][y++] = tempChar;
                }
                else
                {
                    size=x++;
                    y = 0;
                }
            }
        }
        result = selSort(charArray,size);

        if(result)
        {
            cout << "We did it!!!!";
        }
    }

    void printString()
    {
        for(int x=0;x<1000;x++)
        {
            for(int y=0;y<25;y++)
            {
                cout << charArray[x][y];
            }
            cout << endl;
        }
    }

    int selSort(TCHAR thArray[][25], int length)
    {
        TCHAR tempArray[1][25];


        for(int x=0;x<1;x++)
        {
            for(int y=0;y<25;y++)
            {
                tempArray[1][25] = NULL;
            }
        }
        for(int x=0;x<length;x++)
        {
            int best = 0;

            for(int y=1;y<length;y++)
            {
                int result = _tcscmp(thArray[y],thArray[best]);
                if (result == 1)
                {
                    best = y;
                }
                for (int t=0;t < _tcslen(thArray[best]);t++)
                {
                    tempArray[0][t] = thArray[best][t];
                }
                for(int w=0;w < _tcslen(thArray[x]);w++)
                {
                    thArray[best][w]=thArray[x][w];

                }
                for(int e=0;e < _tcslen(thArray[x]);e++)
                {
                    thArray[x][e]=tempArray[0][e];

                }
            }
        }
        return 1;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    convert c1;

    c1.readFile();

    system("pause");
    return 0;
}
4

1 に答える 1

1
int selSort(TCHAR thArray[][25], int length)
{
    TCHAR tempArray[1][25];

    // ...

    tempArray[1][25] = NULL;   // In the for loop
 }

tempArray に 2 行目はありません。配列にn行がある場合、そのインデックスは0 から n-1 まで始まります。おそらくあなたが意味したのは-

tempArray[x][y] = NULL;

ループを記述する代わりに、アルゴリズム ヘッダーにあるstd::fill関数を使用して、配列要素を一意の要素で埋めます。

于 2011-09-16T21:52:34.933 に答える