2015-10-14 86 views
0

在我的C++學習複製構造函數的使用等等,我們得到了一個我們要完成的程序的模板,但是我的輸出在我的輸出流中引入了時髦的asci字符。C++,Odd Asci輸出與我的程序

繼承人我的主類:

#include <iostream> 
using namespace std; 
#include "Contact.h" 
using namespace sict; 

    int main(){ 
     Contact c("Empty Contact", 3); 
     c.display(); 
     cout << "Enter Contact information: " << endl; 
     c.read(); 
     c.display(); 
     cout << endl; 
     for (int i = 0; i < 1000000; i++){ 
     Contact temp("Testing the contact with a looooong " 
        "name that should be taken care of", 20); 
     if (!(i % 100000)){ 
      cout << i << ":" << endl; 
      temp.display(); 
     } 
     } 
     return 0; 
    } 

Contact.cpp:

#include <cstring> 
#include <iostream> 
#include "Contact.h" 
using namespace std; 

namespace sict{ 


    void Contact::display()const{ 
    //display the name and go to new line 
     cout<< _name << endl; 
    // loop through elements of _pn up to _noPN and display them one by one 
     for(int i = 0; i < _noPN ; i++){ 
     _pn[i].display(); 
     } 
    // draw a 40 char line using '-' and go to new line 
    cout<<"----------------------------------------"<<endl; 


    } 
    Contact::Contact(){ 
     _pn = nullptr; 
    } 
    Contact::Contact(const char* name, int number){ 
     strncpy(_name, name,40); 
     _pn = new PhoneNumber[number]; 
     _noPN = number; 

    } 
    Contact::~Contact(){ 
     delete[] _pn; 

    } 


    void Contact::read(){ 
    cout << "Contact Name: "; 
    cin.getline(_name, 41, '\n'); 
    cout << "Please enter " << _noPN << " phone numbers: " << endl; 
    for (int i = 0; i < _noPN; i++){ 
     cout << i + 1 << ": "; 
     _pn[i].read(); 
    } 
    } 



    bool Contact::isEmpty()const{ 
    return _pn == (PhoneNumber*)0; 
    } 
    void Contact::setEmpty(){ 
    _name[0] = 0; 
    _noPN = 0; 
    _pn = (PhoneNumber*)0;// same as _pn = nullptr; 
    } 
} 

輸出故障:

Empty Contact 
---------------------------------------- 
Enter Contact information: 
Contact Name: John Doe 
Please enter 3 phone numbers: 
1: Home, 123 1234567 
2: Cell, 234 2345678 
3: Work, 345 3456789 
John Doe 
Home..........., 123 123-4567 
Cell..........., 234 234-5678 
Work..........., 345 345-6789 
---------------------------------------- 
    0: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    100000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    200000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    300000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    400000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    500000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    600000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    700000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    800000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 
    900000: 
    Testing the contact with a looooong nameôoc·lÐ 
    ---------------------------------------- 

它是什麼想的樣子:

Empty Contact 
---------------------------------------- 
Enter Contact information: 
Contact Name: John Doe 
Please enter 3 phone numbers: 
1: Home, 123 1234567 
2: Cell, 234 2345678 
3: Work, 345 3456789 
John Doe 
Home..........., 123 123-4567 
Cell..........., 234 234-5678 
Work..........., 345 345-6789 
---------------------------------------- 

0: 
Testing the contact with a looooong name 
---------------------------------------- 
100000: 
Testing the contact with a looooong name 
---------------------------------------- 
200000: 
Testing the contact with a looooong name 
---------------------------------------- 
300000: 
Testing the contact with a looooong name 
---------------------------------------- 
400000: 
Testing the contact with a looooong name 
---------------------------------------- 
500000: 
Testing the contact with a looooong name 
---------------------------------------- 
600000: 
Testing the contact with a looooong name 
---------------------------------------- 
700000: 
Testing the contact with a looooong name 
---------------------------------------- 
800000: 
Testing the contact with a looooong name 
---------------------------------------- 
900000: 
Testing the contact with a looooong name 
---------------------------------------- 

正如你所看到的,那裏有在名稱末尾添加一些ASCII字符

我可以添加程序的其餘部分但它是安靜冗長的,如果要求它我會編輯這篇文章,將它們添加。

+0

沒有'Contact'類的定義,我們真的不能幫助。這顯然是問題所在。 – owacoder

+0

您需要顯示'Contact'構造函數 - 問題很可能存在(並且可能缺少字符串的null終結符)。 – Rostislav

+0

對不起,添加它。 – andirew

回答

1

strncpy(_name, name,40);不會在字符串末尾添加空終止符。要修復它,只需添加一行_name[40] = '\0'。對於短字符串,它只是恰好複製它。對於更長的琴絃 - 它停在第40個符號處。

cppreference

如果被複制整個陣列SRC之前達到計數,所得到的字符數組是不是空終止。

+0

這解決了它!謝謝! – andirew

+0

@andirew不要忘記接受答案;) – Rostislav