0

숙제의 경우 파일을 읽고 문자열을 정렬해야합니다. 이를 위해 저는 선택 정렬을 사용하고 있습니다. 이제 selSort 함수를 호출 한 직후에 충돌이 발생합니다. 나는이 문제를 해결하기 위해 아이디어를 다 썼다.ESP calling convention issues C++

// 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; 
} 
+0

이 프로그램에서 tmain과 TCHAR를 사용해야하는 이유가 있습니까? –

답변

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

    // ... 

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

는 tempArray에는 두 번째 행 없다. 배열에 n 행이있는 경우 인덱스는 0에서 n-1까지입니다. 아마 당신은 의미 -

tempArray[x][y] = NULL; 

을 대신 루프를 작성하는 고유 한 요소와 배열 요소를 채우기 위해 알고리즘 헤더에 존재하는 std::fill 기능을 사용하십시오.