[英]C++, Odd Asci output with my Program
learning in my c++ the use of copy constructors etc. we were given a template of a program we were to complete, however my output is throwing funky asci characters in my output stream. 在我的C ++中学习了复制构造函数的使用等。我们得到了要完成的程序的模板,但是我的输出在输出流中抛出了时髦的asci字符。
Heres my main class: 这是我的主要课程:
#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: 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;
}
}
Output malfunction: 输出故障:
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Ð
----------------------------------------
What it is suppose to look like: 看起来像什么:
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
----------------------------------------
As you can see, theres a few ascii characters being added at the end of name 如您所见,名称末尾添加了几个ascii字符
I can add the rest of the program however it is quiet lengthy, if it is requested i will edit this post and add them. 我可以添加程序的其余部分,但是它很冗长,如果需要,我将编辑此帖子并添加它们。
strncpy(_name, name,40);
doesn't add a null-terminator at the end of string. 不在字符串末尾添加空终止符。 To fix it, just add a line
_name[40] = '\\0'
. 要解决此问题,只需添加
_name[40] = '\\0'
。 For short strings it just happens to copy it. 对于短字符串,只需将其复制即可。 For longer strings - it stops at 40'th symbol.
对于更长的字符串-停在第40个符号处。
From cppreference 来自cppreference
If count is reached before the entire array src was copied, the resulting character array is not null-terminated.
如果在复制整个数组src之前已达到count,则所得的字符数组不会以null结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.