简体   繁体   English

C ++-带有结构指针的字符串输入?

[英]C++ - String input with a pointer-to-structure?

How do you use a pointer-to-struct to get input that will be stored in a string variable? 如何使用结构指针获取输入,该输入将存储在字符串变量中? I thought simply passing pz->szCompany to getline() would behave the same as if I had used the . 我认为简单地将pz-> szCompany传递给getline()的行为就如同我使用过一样。 operator on a normal instance of Pizza (instead of a pointer-to), but when I run this program it skips over the company name prompt completely. 比萨的正常实例(而不是指向指针)上的运算符,但是当我运行此程序时,它将完全跳过公司名称提示。

// Parts of the program omitted.
struct Pizza {
    string szCompany;
    float diameter;
    float weight;
};
Pizza* pz = new Pizza;

cout << "Enter the weight: ";
cin >> pz->weight;

cout << "Enter the company name: ";
// use getline because the company name can have spaces in it.
getline(cin, pz->szCompany);

cout << "Enter the diameter: ";
cin >> pz->diameter;

cout << "\nCompany name: " << pz->szCompany;
cout << "\nWeight: " << pz->weight;
cout << "\nDiameter: " << pz->diameter;

// cannot forget this step.
delete pz;
return 0;

When you use >> to read input, it will leave unread characters in the stream (those, that couldn't be converted to integer, at least the return character you type to enter input), which the following getline will consume thinking it has already read an (empty) line. 当您使用>>读取输入时,它将在流中留下未读的字符(那些不能转换为整数的字符,至少是您键入要输入的返回字符),下面的getline会认为它具有已经读取(空)行。

#include <limits>

//...
cin >> pz->diameter;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

cout << "Enter the company name: ";
// use getline because the company name can have spaces in it.
getline(cin, pz->szCompany);

Your problem has nothing to do with the struct or pointers, just the normal behavior of input streams. 您的问题与结构或指针无关,而与输入流的正常行为无关。

You may also have to deal with incorrect input. 您可能还必须处理不正确的输入。 For example, entering a non-number where a number is expected would put the stream in an error state, so that all subsequent attempts to read would fail unless you deal with it. 例如,在期望数字的地方输入非数字将使流处于错误状态,因此除非您处理它,否则所有随后的读取尝试都将失败。 Better take Neil's advice, but for getting input from the user, it might also make sense to use a function for formatted input that prompts you until you get valid input: 最好采用Neil的建议,但是对于从用户那里获取输入,使用格式化输入功能提示您直到获得有效输入也可能有意义:

template <class T>
T input(const std::string& prompt, const std::string& error_prompt)
{
    std::cout << prompt;
    T result;
    while (!(std::cin >> result)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << error_prompt;
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return result;
}

//...
pz->weight = input<float>("Enter the weight: ", "Please enter a numeric value: ");

This has nothing to do with the pointer to the structure instance. 这与指向结构实例的指针无关。 It is simply not a good idea to mix line input with formatted input. 将行输入与格式化输入混合在一起根本不是一个好主意。 In fact, it's not a good idea to use formatted input from an interactive input stream at all. 实际上,根本不使用交互式输入流中的格式化输入。 You should instead read each input using getline() and then convert to the required type. 您应该改用getline()读取每个输入,然后转换为所需的类型。

Add either: 添加:

cout << "Enter the company name: " << std::endl;

or 要么

cout << "Enter the company name: ";
cout.flush();

You problem concerned with bufferization of stream 您担心与流缓冲有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM