【问题标题】:What is wrong with my use of "cin.get" here?我在这里使用“cin.get”有什么问题?
【发布时间】:2014-01-16 04:16:58
【问题描述】:

只要我注释掉 cin.get 行,我的代码似乎就可以编译,但我看不出我是如何滥用它的。作为第二个问题,一旦我弄清楚我在那里做错了什么,我是否可以使用 getlength 来计算该字符串的长度,然后使用 prtintf 将该长度的 cstring 打印到我的输出文件中计划加入?顺便说一句,我不允许使用字符串。

#include<iostream>
#include<fstream>
#include<cstring>
#include<string>

using namespace std;

struct thisType
{
    char peiceOne[100];
    char peiceTwo[100];
    char peiceThree[100];
};

void requestData();

void storeData(char *charArray[]);

int main(){
    thisType tempStruct[100];

    requestData();

    return 0;

}

    void requestData(){
        cout << endl << "Please enter data as follows, without brackets: [peiceOne];"
            << "[peiceTwo];[peiceThree] " << endl;

    }

    void storeData(char *tempStruct[100]){
        //cin.get(tempStruct[0], 100, ";");
    }

【问题讨论】:

  • 请考虑使用std::string 和提取运算符operator&gt;&gt; 在c++ 中读取std::istream
  • 这是一个类,我不能使用字符串,但我可以使用 cstrings。
  • 那你为什么要包含&lt;string&gt;?对于您的代码,仅需要 &lt;iostream&gt;。 C 字符串不需要“包含”它们是内置类型。 &lt;cstring&gt; 包含你只需要进行字符串操作的东西。
  • 我不知道,我会摆脱它。
  • 你到底想做什么?你的任务是什么?

标签: c++ arrays get char typedef


【解决方案1】:

cin.get() 中,第三个参数应为char,但您传递的是const char* 类型的字符串。

您应该将分隔符放在单引号中,这样可以解决错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2011-01-22
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多