【问题标题】:C++, Odd Asci output with my ProgramC++,奇怪的 Asci 输出与我的程序
【发布时间】:2015-10-14 17:18:08
【问题描述】:

在我的 c++ 中学习使用复制构造函数等。我们得到了一个要完成的程序模板,但是我的输出在我的输出流中抛出了时髦的 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;
    }

联系人.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 字符

我可以添加程序的其余部分,但是它很长,如果需要,我会编辑这篇文章并添加它们。

【问题讨论】:

  • 没有Contact 类定义,我们真的无能为力。这显然是问题所在。
  • 您需要显示Contact 构造函数 - 问题很可能存在(并且可能缺少字符串的空终止符)。
  • 看起来像一个未终止的字符串。
  • 您的教授似乎明确使用了超过 40 个字符的名称。
  • 恶魔!鉴于分配的重点是复制构造函数,我假设使用 std::string 不可行?另外两个注意事项:我在代码中看到 40 和 41。您可能希望更严格地执行缓冲区大小。如果_name的大小改变了,40不会改变,但sizeof(_name)会。在复制构造函数的主题上阅读What is The Rule of Three?。它可能会为您节省一些未来的问题。

标签: c++ output ascii


【解决方案1】:

strncpy(_name, name,40); 不在字符串末尾添加空终止符。要修复它,只需添加一行_name[40] = '\0'。对于短字符串,它只是碰巧复制它。对于较长的字符串 - 它在第 40 个符号处停止。

来自cppreference

如果在复制整个数组 src 之前达到 count,则生成的字符数组不是以 null 结尾的。

【讨论】:

  • 这修复了它!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 2014-02-04
  • 2020-05-14
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多