2011-09-16 14 views
0

宿題については、ファイルを読み込んで文字列をソートすることになっています。このため、私は選択ソートを使用していますし、それは動作します。さて、selSort関数を呼び出すと直ちにクラッシュします。私は誰も私に手を差し伸べることができるこの問題を解決するためにアイデアが不足している?ESP呼び出し規約に問題があります。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行の場合、インデックスはからn-1までです。おそらく、あなたは意味 - 代わりにループを書くの

tempArray[x][y] = NULL; 

を、独自の要素を持つ配列要素を埋めるために、アルゴリズムのヘッダに存在するstd::fill関数を使用します。

関連する問題