不知道出了什么问题(字符串输入)

时间:2019-12-12 18:39:51

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

为一个类编写程序,在该类中我们使用函数从输入的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

1 个答案:

答案 0 :(得分:1)

简单吗?

std::getline(cin, input);

代替

cin.getline(cin, input);

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

相关问题