【问题标题】:Not sure whats going wrong (string input) [closed]不知道出了什么问题(字符串输入)[关闭]
【发布时间】:2020-04-06 05:13:38
【问题描述】:

为我们使用函数从输入 txt 文件复制数据列表的类编写程序。我的功能之一是获取他们想要搜索数据的文件名的输入。这是我的文件的预处理器方向以及函数代码,但是在 .getline 上,我的 .getline 行上不断出现相同的错误,我不知道如何解决这个问题。还想知道我的代码的一般流程是否有意义。谢谢!

 #include <iostream>
 #include <iomanip>
 #include <string>
 #include <time.h>
 #include <stdlib.h>
 #include <fstream>
using namespace std;
const char PROGRAMMER[29] = "Matt Napoli & Jeff Koucoulis";
const char CLASS[5] = "CS1A";
const char SECTION[25] = "MW: Mon/Wed 5-7:20 PM";
const int LAB_NUM = 13;
const char LAB_NAME[17] = "Arrays in C++";
string input;
const int AR_SIZE = 10;




string checkFile()
{
    cout << "What input file would you like to use? ";
    cin.getline(cin, input);
    return input;
}

void filetoArray()
{
    cout << "Reading records from input file " << input;
    cout << "Who do you want to search for (enter done to exit)? " << endl;
    ifstream file(input);
        if(file.is_open())
        {
            string whatsInFile[AR_SIZE];

            for(int i = 0; i < AR_SIZE; ++i)
            {
                file >> whatsInFile[AR_SIZE];
            }
        }
}

这也是我的 main.cpp 文件

 #include <iostream>
 #include <iomanip>
 #include <cstring>
 #include <string>
 #include <time.h>
 #include <stdlib.h>
 #include <fstream>
 using namespace std;
string input;
string inFile();
const int AR_SIZE = 10;
string whatsInFile[AR_SIZE];

int main() {
    int index = 0;
    void InFile();
    while (inFile && index < AR_SIZE)
    {
        void filetoArray();
    }
    return 0;
}

我的输入文件在这里

Joe
Sally
Joe
Sue
Sally
Adam
Joe
Adam
Adam
Joe

【问题讨论】:

  • 您没有发布minimal reproducible example。您发布了两个完全不相关的函数。
  • 你能分享文件的内容吗?
  • @RafalS 是的,我这样做了

标签: c++ function compiler-errors getline istream


【解决方案1】:

简单怎么样:

std::getline(cin, input);

而不是

cin.getline(cin, input);

这是错误的,因为std::basic_istream&lt;CharT,Traits&gt;::getline 的签名是: basic_istream&amp; getline( char_type* s, std::streamsize count );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2023-04-02
    相关资源
    最近更新 更多