开发者

ESP calling convention issues C++

For homework I'm supposed to read a file and the sort the strings. For this I'm using a selection sort and it works. Now, right after it makes the call to selSort function it crashes. I have ran out of ideas to solve this issue can anyone give me a hand?

// 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],thArr开发者_StackOverflow社区ay[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;
}


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

    // ...

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

There is no second row in the tempArray. If an array has n rows then it's index starts from 0 to n-1. Probably you meant -

tempArray[x][y] = NULL;

Instead of writing a loop, use a std::fill function residing in the algorithm header to fill the array elements with a unique element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜