2016-03-30 1 views
-1
#include <string.h> 

#include "BubbleSort.h" 

void BubbleSort(char Str[]) 
{ 
    int  i; 
    int  NumElements; 
    bool Sorted; 
    char Temp; 

    NumElements = strlen(Str); 
    do { 
     Sorted = true; 
     NumElements--; 
     for (i = 0; i < NumElements; i++) 
      if (Str[i] > Str[i + 1]) 
      { 
       Temp = Str[i]; 
       Str[i] = Str[i + 1]; 
       Str[i + 1] = Temp; 
       Sorted = false; 
      } 
    } while (!Sorted); 
} 
///////////////////////////////////////////// 

#include <iostream> 
#include "Bubblesort.h" 

using namespace std; 



void main() { 
    int Num; 
    char Array[20]; 



    cout << "How many numbers would you like to enter?" << endl; 
    cin >> Num; 

    cout << "Enter your numbers:" << endl; 
    for (int i = 0; i < Num; i++) 
    { 
     cin >> Array[i]; 
    } 


    cout << "Here are the numbers you entered:" << endl; 
    for (int i = 0; i < Num; i++) 
    { 
     cout << Array[i] << " "; 
    } 
    cout << endl << endl; 

    BubbleSort (Array); 


     cout << "Here are your sorted numbers:" << endl; 
     for (int i = 0; i < Num; i++) 
     { 
     cout << Array[i] << " "; 
    } 
    cout << endl; 
} 

//////////////////////////////////////////////////////// 

#ifndef BUBBLE_SORT_H 
#define BUBBLE_SORT_H 

void BubbleSort(char[]); 

#endif 

実行時エラーが発生し、Numが破損しています。誰かが私のコードの問題を特定するのを助けることができますか?ランタイムチェックエラー#2

おかげ

+0

入力内容は何ですか? –

+1

ようこそ。コードを正しくフォーマットし、[このガイド](http://stackoverflow.com/help/how-to-ask)を読んで質問する方法をお読みください。 – robsn

+0

'main'を' void'にすることはできません –

答えて

0

char Array[20]Numご入力が20より大きくなる一方で、それは破損します。

NumElements = strlen(Str);

したがってNumElementsは未定値を持っています

より良い使用vectorpush_back

0

1つのミスは、あなたがNULL終了することが保証されていないchar配列にstrlenを呼んでいるということです。

あなたがいずれかを行う必要があります。

BubbleSort(Array, Num); 
//... 
void BubbleSort(char Str[], int NumElements) 

1)は、配列とstrlenにコールを取り払うとともにパラメータとしてソートされる実際の文字数を渡しますまたは

2)渡す文字配列がヌルであることを確認してください。