0

这是我正在开发的程序。到目前为止,它只是在我的 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
4

1 回答 1

2

更改标题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();
   }
 }
于 2013-06-14T21:25:03.727 回答