C++ 循环内部变量

C++ 循环内部变量,c++,for-loop,C++,For Loop,我很沮丧。我试图找到一种方法,让名为“line”的变量在代码底部的for循环中被访问 #include <ctime> #include <iostream> #include <sys/types.h> #include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <iostream> #inc

我很沮丧。我试图找到一种方法,让名为“line”的变量在代码底部的for循环中被访问

#include <ctime>
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <Lmcons.h>
#include <fstream>
#include <windows.h>

using namespace std;

string currentmonth;
string earlydays;

//**************************************
string a3;
string a1;

int getdir (string dir, vector<string> &files)
{
    DIR *dp;

    struct dirent *dirp;

    if((dp = opendir(dir.c_str())) == NULL)
    {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        std::string fname = dirp->d_name;
        if(fname.find("FIN804") != std::string::npos)
            files.push_back(fname);
    }
}

//***************************************
int main() {
    //Copy New date to be used from date database

    ifstream dailyfiledate;

    dailyfiledate.open("Databasedate.txt");

    string line;

    if (!dailyfiledate) //checks to see if file opens properly
    {
        cerr << "Error: Failed to copy the first string from Date Database.";
    }
    else
    {
        if (getline(dailyfiledate, line)) // Get line
            cout << line; // print the line.

        dailyfiledate.close(); // Remember to close the file.
    }

    string dir = string(a1);

    vector<string> files = vector<string>();

    getdir(dir,files);

    for (unsigned int i = 0; i < files.size();)
    {
        //cout << files[i] << endl;
        a3 = files[i];
        cout << a3 << endl;
        string b1 = a3 + line;
        cout << b1 << endl;
        remove(b1.c_str());
        i++;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串当前月份;
弦乐早期;
//**************************************
字符串a3;
字符串a1;
int getdir(字符串目录、向量和文件)
{
DIR*dp;
结构方向*dirp;
if((dp=opendir(dir.c_str())==NULL)
{

cout实际上,在for循环中成功地访问了变量。我想这是我的路径上的疏忽

上面的代码在编译时绝对没有错误


你们这些冒牌货真的需要更谦虚一些。给出答案,给出指导,或者根本不要评论。

先格式化你们的代码。@user4581301谢谢,这伤害了我的眼睛,必须完成
int-getdir(string-dir,vector&files)
承诺返回一个
int
,但当它成功时,它不会返回任何东西。这可能会导致各种奇妙和神秘的事情发生。我在那里看到的唯一问题是
a1
在使用时是空的。强烈建议给变量起一个有意义的名称,这样一来,冷眼旁观的人们对我有一些希望解释它不工作时应该做什么。@SketchFam
a1
从未初始化。当调用
getdir(dir,files)时,您将该值指定给
dir
opendir
将失败,因此
文件
为空。您甚至不会进入该循环。告诉我您在哪里初始化
a1
,我会改变主意。