【问题标题】:C++ program doesn't print out strings but does output double valueC++ 程序不打印字符串但输出双精度值
【发布时间】:2013-06-14 21:07:13
【问题描述】:

这就是我正在开发的程序。到目前为止,它只是在我的 CPP 文件中打印出双精度值。注意我设置的两个字符数组。这是为什么呢?

分子.h

const int MAX_STRUCT = 10;
const int MAX_NAME = 20;

class Molecule {
    char molecule_structure[];
    char molecule_name[];
    double molecule_mass;
 public: 
    Molecule();
    bool read();
    void display() const;
};        

分子.cpp

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

Molecule::Molecule() {

molecule_structure[0] = '\0';
molecule_name[0] = '\0';
molecule_mass = 0;

}

bool Molecule::read(){

bool complete = false;

    cout << "Enter structure : ";
    cin.getline (molecule_structure, 10);

    if (strcmp (molecule_structure, "0") != 0){

    cout << "Enter full name : ";
    cin.getline (molecule_name, 20);

    cout << "Enter weight : ";
    cin >> molecule_mass;

    cin.ignore();
    complete = true;       
}

else {

molecule_structure[0] = '\0';
molecule_name[0] = '\0';
molecule_mass = 0;

}

return complete;

}


void Molecule::display() const
{       
cout << molecule_structure << "       " << molecule_name  << "       " << molecule_mass     << endl;
}

w4x.h

 const int MAX_MOLECULES = 10;

w4x.cpp

 #include <iostream>
 using namespace std;
 #include "w4x.h"
 #include "Molecule.h"

 int main() {
 int n = MAX_MOLECULES;
 Molecule molecule[MAX_MOLECULES];

 cout << "Molecular Information\n";
 cout << "=====================" << endl;

 for (int i = 0; i < MAX_MOLECULES; i++) {
     if (!molecule[i].read()) {
         n = i;
         i = MAX_MOLECULES;
     }
     cout << endl;
 }

 cout << "Structure            Name                     Mass\n";
 cout << "==================================================" << endl;

 for (int i = 0; i < n; i++)
     molecule[i].display();
 }

我认为这些错误来自我的 Molecule.cpp 文件,这是我一直在改变的。 这是我目前收到的输出。

Molecular Information

=====================

Enter structure : Super 
Enter full name : Man
Enter weight : 57

Enter structure : 0

Structure            Name                     Mass
==================================================
              57

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快阅读FAQ。你不应该在课堂上使用char molecule_structure[];;您可能应该使用字符串,否则,您应该使用大小合适的字符数组,例如char molecule_structure[64];。这是否是你麻烦的根源是另一回事,但应该认真看待。
  • 问:你为 char 数组分配了多少内存? A:None 如果你不懂 char 数组和指针,那么 std::string 会让你的生活变得更轻松。

标签: c++ string double output


【解决方案1】:

更改标题 Molecule.h 使其使用:

const int MAX_STRUCT = 10;
const int MAX_NAME = 20;

class Molecule {
    char molecule_structure[MAX_STRUCT];
    char molecule_name[MAX_NAME];
    double molecule_mass;
 public: 
    Molecule();
    bool read();
    void display() const;
}; 

使代码运行良好。


使用std::string 对代码进行更彻底的修改:

分子.h

#ifndef MOLECULE_H_INCLUDED
#define MOLECULE_H_INCLUDED

#include <string>
class Molecule
{
  std::string molecule_structure;
  std::string molecule_name;
  double molecule_mass;
public:
  Molecule();
  bool read();
  void display() const;
};

#endif // MOLECULE_H_INCLUDED

分子.cpp

#include <iostream>
#include <iomanip>
#include <limits>
#include <cstring>
using namespace std;
#include "Molecule.h"

Molecule::Molecule() : molecule_structure(""), molecule_name(""), molecule_mass(0) { }

bool Molecule::read()
{
  Molecule m;

  cout << "Enter structure : ";
  if (!getline(cin, m.molecule_structure) || m.molecule_structure == "")
    return false;

  cout << "Enter full name : ";
  if (!getline(cin, m.molecule_name))
    return false;

  cout << "Enter weight    : ";
  if (!(cin >> m.molecule_mass))
    return false;

  cin.ignore(numeric_limits<streamsize>::max(), '\n');

  swap(*this, m);

  return true;
}

void Molecule::display() const
{
  cout << left << setw(15) << molecule_structure << "      ";
  cout << left << setw(20) << molecule_name      << "      ";
  cout << setprecision(5)  << molecule_mass      << endl;
}

read() 函数不会修改它给出的变量,除非读取成功。可能有更好的方法来处理输入,但显示的是合理的。响应“Enter structure:”提示,您可以用空行终止输入。与 C++ I/O 流所必需的相比,printf() 格式符号的优点是简洁。

w4x.cpp

不再包括w4x.h。

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

const int MAX_MOLECULES = 10;

int main()
{
   int n = MAX_MOLECULES;
   Molecule molecule[MAX_MOLECULES];

   cout << "Molecular Information\n";
   cout << "=====================" << endl;

   for (int i = 0; i < MAX_MOLECULES; i++) {
     if (!molecule[i].read()) {
       n = i;
       break;
     }
     cout << endl;
   }

   if (n > 0)
   {
     cout << "Structure            Name                      Mass\n";
     cout << "===================================================" << endl;

     for (int i = 0; i < n; i++)
       molecule[i].display();
   }
 }

【讨论】:

  • 这行得通。但您也希望将这些常量与用户输入一起使用,否则将来的更改可能会引入缓冲区溢出条件。
  • 不,当有人决定输入一个超过 20 个字符的名称时,它会使代码像以前一样爆炸。这样的代码是最可靠的灾难!使用 C++ 字符串或根据所需大小手动分配动态内存,但不要求用户知道您在程序中编写了哪些任意限制!
  • @Sqeaky:呃;你的意思是你要我读所有的代码?是的,你是对的,Molecule::read() 函数中的 10 和 20 应该是 sizeof(molecule_structure) 和 sizeof(molecule_name),否则,常量 MAX_STRUCT 和 MAX_NAME。这只是“最低限度的必要改变”。使用std::string 来存储名称仍然会更好。
  • @JonathanLeffler sizeof 确实更有意义,我只是在提倡一致性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多