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

标签 c++ function compiler-errors getline istream

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




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

最佳答案

怎么样简单:

std::getline(cin, input);

代替
cin.getline(cin, input);

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

关于c++ - 不知道出了什么问题(字符串输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59310904/

相关文章:

javascript - 横幅旋转图像。单击时每个图像都会发送到不同的 URL(vanilla javascript)

c - 如何修复我的代码以清除链接结构中的节点

java - Java错误: <identifier> expected unable to compile

ios - Swift 错误消息中的 '(String)' 和 'String' 有什么区别

c++ - 数组模板和内置数组类型之间有区别吗?

c++ - 如何提取Boost库中引用段的点

c++ - 跨平台 Flash Player 嵌入

sql-server - VARCHAR' 不是可识别的内置函数名称

c++ - C++ 中的可变参数方法和递归

C++/Objective-C - 如何分析一个大项目(Unix 方式)?