2016-11-30 151 views
0

無法使用strcpy正確地交換char數組。我使用了strncpy,memcpy,但仍然沒有得到正確的結果。使用strcpy時從'char'到'const char *'的轉換無效

這是我的程序的外觀。

class Carte 
{ 
protected: 
    char Denumire[50]; 
    char Autor[50]; 
    char Editura[50]; 
    int AnPublicare[50]; 
public: 
    void Citire(int n) 
    { 
     for(int i=0;i<n;i++) 
     { 
      cout<<"Denumire: "; cin>>Denumire[i]; cout<<endl; 
      cout<<"Autor: "; cin>>Autor[i]; cout<<endl; 
      cout<<"Editura: "; cin>>Editura[i]; cout<<endl; 
      cout<<"An Publicare: "; cin>>AnPublicare[i]; cout<<endl; 
     } 
    } 

    void Afisare(int n) 
{ 
    for(int i=0;i<n;i++) 
    { 
     cout<<"Denumire: "<<Denumire[i]<<endl; 
     cout<<"Autor: "<<Autor[i]<<endl; 
     cout<<"Editura: "<<Editura[i]<<endl; 
     cout<<"An Publicare: "<<AnPublicare[i]<<endl<<endl; 
    } 
} 

問題出在哪裏。

void Sortare(int n) 
{ 
    int ok,aux; 
    char *auxDen , *auxAt, *auxEd; 
    do{ 
     ok=1; 
     for(int i=0;i<n-1;i++) 
      if(AnPublicare[i]>AnPublicare[i+1]) 
      { 
       ok=0; 
       aux=AnPublicare[i]; 
       AnPublicare[i]=AnPublicare[i+1]; 
       AnPublicare[i+1]=aux; 

       strcpy(auxDen,Denumire[i]); 
       strcpy(Denumire[i],Denumire[i+1]); 
       strcpy(Denumire[i+1],auxDen); 

       strcpy(auxAt,Autor[i]); 
       strcpy(Autor[i],Autor[i+1]); 
       strcpy(Autor[i+1],auxAt); 

       strcpy(auxEd,Editura[i]); 
       strcpy(Editura[i],Editura[i+1]); 
       strcpy(Editura[i+1],auxEd); 
      } 
    }while(ok!=1); 
} 
}; 

如何使它工作?

+2

'Denumire [i]'是'char'。你需要'&Denumire [i]'。 –

+0

它不起作用。編譯器崩潰。 –

+0

我懷疑編譯器崩潰了,請發佈更新後的代碼,並且過去現在得到的確切錯誤 – pm100

回答

1

在C數組中的char是單個字符串,所以首先你的數據類型是錯誤的。你最好使用std::string這就像一個字符串對象:

class Carte 
{ 
protected: 
    std::string Denumire[50]; 
    std::string Autor[50]; 
    std::string Editura[50]; 
    int AnPublicare[50]; 

然後移動它們對子級是相同int

std::string tmp; 
tmp = Denumire[i]; 
Denumire[i] = Denumire[i+1]; 
Denumire[i+1] = tmp; 

甚至更​​好:

std::swap(Denumire[i], Denumire[i+1]); 

,如果你僅限於C類型,您必須創建一組char數組來保存50個字符串。

+0

謝謝!我一直在C編程一年半。我還是新來的C++; –

1

A char只是一個字符;你似乎把它當作完整的字符串來對待。如果可以,最好使用std::string而不是char*,因爲它們更容易正確使用。

此外,我會建議你將所有元素分組到一個struct Book。這樣一切都在一起,你可以使用現有的STL排序算法,這將更快。

#include <string> 
#include <vector> 
#include <algorithm> 

struct Book{ 
    std::string Denumire; 
    std::string Autor; 
    std::string Editura; 
    int AnPublicare; 
}; 

bool publisherLessThan(const Book& a, const Book& b) { 
    return a.AnPublicare < b.AnPublicare; 
} 

class Carte 
{ 
protected: 
    std::vector<Book> books; 
public: 
    void Citire(int n) 
    { 
     books.clear(); 
     for(int i=0;i<n;i++) 
     { 
      Book book 
      cout<<"Denumire: "; cin>>book.Denumire; cout<<endl; 
      cout<<"Autor: "; cin>>book.Autor; cout<<endl; 
      cout<<"Editura: "; cin>>book.Editura; cout<<endl; 
      cout<<"An Publicare: "; cin>>book.AnPublicare; cout<<endl; 
      books.push_back(book); 
     } 
    } 

    void Afisare() 
    { 
     for(const Book& book : books) 
     { 
      cout<<"Denumire: "<<book.Denumire<<endl; 
      cout<<"Autor: "<<book.Autor<<endl; 
      cout<<"Editura: "<<book.Editura<<endl; 
      cout<<"An Publicare: "<<book.AnPublicare<<endl<<endl; 
     } 
    } 

    void Sortare() { 
     std::sort(books.begin(), books.end(), publisherLessThan); 
    } 
}; 
相關問題