1

我现在正在自学 C/C++,并且我得到了练习(来自我正在阅读的书)来编写一个可以产生如下输出的程序:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

使用结构。但我的输出是这样的:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: , 

这是代码。它相当简短,所以应该不难阅读:)

#include <iostream>
#include <cstring>

using namespace std;

struct Person {
    char* firstName;
    char* lastName;
};

char* getName(void);

int main() {
    Person* ps = new Person;
    cout << "Enter your first name: ";
    char* name;
    name = getName();
    ps->firstName = name;
    cout << "Enter your last name: ";
    char* lastname;
    lastname = getName();
    ps->lastName = lastname;
    cout << "Here's the information in a single string: "
            << ps->lastName << ", " << ps->firstName;
    delete ps;
    delete name;
    delete lastname;

    return 0;
}

char* getName() {
    char temp[100];
    cin >> temp;
    cin.getline(temp, 100);
    char* pn = new char[strlen(temp) + 1];
    strcpy(pn, temp);

    return pn;
}
4

4 回答 4

5

首先,没有 C/C++ 这样的东西。你正在混合它们,这是错误的。由于您使用的是 C++ headers/ new/ using,我假设您需要 C++,所以这里是您修复代码的方法:

  • 全部替换char*char[]std::string
  • 摆脱动态分配

因此,一些变化将是:

struct Person {
    std::string firstName;
    std::string lastName;
};

或者

Person ps;
于 2012-10-30T11:40:39.150 回答
3

您正在使用:

cin >> temp;
cin.getline(temp, 100);

您可能会在行尾用空字符串覆盖您已有的内容。

只使用其中之一。

如果您坚持使用,cin >>您可以考虑设置width()以防止缓冲区溢出。

于 2012-10-30T11:41:24.330 回答
2

首先,直接的问题是您从 阅读了两次std::cin:首先是 with operator>>,然后是 with getline。选择一个或另一个。

但是让我们稍微简化一下您的代码。错误的来源太多了。指针很棘手,因为它们可能指向错误的东西,或者您可能忘记删除对象,或者两次删除它们。C 风格的 char 数组作为字符串是不好的,因为它们不是字符串,而且它们的行为不像字符串。

所以让我们使用标准库的字符串类:

#include <iostream>
#include <string>

struct Person {
    std::string firstName;
    std::string lastName;
};

std::string getName(void);

int main() {
    Person ps;
    cout << "Enter your first name: ";
    std::string name = getName();
    ps.firstName = name;
    cout << "Enter your last name: ";
    std::string lastname = getName();
    ps.lastName = lastname;
    cout << "Here's the information in a single string: "
            << ps.lastName << ", " << ps.firstName;
}

std::string getName() {
    std::string temp;
    std::getline(cin, temp);
    return temp;
}

这是一个相当简单的,几乎是机械的替换,基本上只是用 替换char*std::string并删除不再需要的位。

当然,正如评论中所指出的,我省略了所有形式的错误检查,这是一个真正的程序绝对应该做的。

于 2012-10-30T11:56:43.470 回答
2

No no no, wayyyy too complicated. Use real C++ idioms. The program could be as simple as this:

#include <string>
#include <iostream>

int main()
{
    std::string firstName, lastName;

    if (!(std::cout << "Your first name: "  &&
          std::getline(std::cin, firstName) &&
          std::cout << "Your last name: "   &&
          std::getline(std::cin, lastName)     ))
    {
        std::cerr << "Error: unexpected end of input!\n";
        return 0;
    }

    std::cout << "You are " << firstName << " " << lastName << ".\n";
}

As a variation on the theme, you could put each getline in a loop until the user inputs a non-empty line:

std::cout >> "Your first name: ";
for ( ; ; )
{
    if (!(std::getline(std::cin, firstName))
    {
        std::cerr << "Error: unexpected end of input.\n"; 
        return 0;
    }
    if (!firstName.empty())
    {
        break;
    }
    std::cout << "Sorry, please repeat - your first name: ";
}
于 2012-10-30T11:44:06.500 回答