C ++程序不打印字符串但输出双值

时间:2013-06-14 21:07:13

标签: c++ string double output

所以这是我正在研究的程序。到目前为止它只是打印出我的CPP文件中的double值。请注意我设置的两个字符数组。这是为什么?

Molecule.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;
};        

Molecule.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

1 个答案:

答案 0 :(得分: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进行更彻底的代码修改会产生:

Molecule.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

Molecule.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();
   }
 }