首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++程序不打印字符串,但输出双精度值

C++程序不打印字符串,但输出双精度值
EN

Stack Overflow用户
提问于 2013-06-15 05:07:13
回答 1查看 383关注 0票数 0

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

Molecule.h

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
 const int MAX_MOLECULES = 10;

w4x.cpp

代码语言:javascript
复制
 #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文件,这也是我一直在修改的。这是我目前收到的输出。

代码语言:javascript
复制
Molecular Information

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

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

Enter structure : 0

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

回答 1

Stack Overflow用户

发布于 2013-06-15 05:25:04

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

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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();
   }
 }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17117289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档