Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序总是产生相同的值吗?_C++_String_File_Io_G++ - Fatal编程技术网

C++ 程序总是产生相同的值吗?

C++ 程序总是产生相同的值吗?,c++,string,file,io,g++,C++,String,File,Io,G++,我的程序的目的是将数字1-1000000写入一个文本文件,生成一个介于1和1000000之间的随机数,在文本文件中搜索该行,获取值,并将其平方(这对我来说只是一个练习,没有实际应用)。问题是每当我运行它时,值都保持不变,但是rand()函数的种子是time(0)。我怀疑这是一个垃圾值,但我不知道它来自哪里(我没有使用GDB或任何其他独立调试器的经验)。以下是我的源代码: #include <fstream> #include <ctime> #include <io

我的程序的目的是将数字1-1000000写入一个文本文件,生成一个介于1和1000000之间的随机数,在文本文件中搜索该行,获取值,并将其平方(这对我来说只是一个练习,没有实际应用)。问题是每当我运行它时,值都保持不变,但是
rand()
函数的种子是
time(0)
。我怀疑这是一个垃圾值,但我不知道它来自哪里(我没有使用GDB或任何其他独立调试器的经验)。以下是我的源代码:

#include <fstream>
#include <ctime>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main(int argc, char** argv){
    ofstream file("log.txt", ios::app);
    ofstream programLog("programlog.dat", ios::app);
    cout << "Test Start" << endl;
    programLog << "Test Start" << endl;
    cout << "Log file created" << endl;
    programLog << "Log file created" << endl;
    ifstream readFile("log.txt");
    int foundNum;
    std::string line = "";
    unsigned int loopCount = 1000000;
    unsigned int numToSearch;
    const unsigned short min = 1;
    const int max = 1000000;
    unsigned int randomLine = 0;

    for(int i = 0; i <= loopCount; i++){
        file << i << endl;
    }

    //select random line
    srand((unsigned)time(0));

    while(!(randomLine > min) && !(randomLine < max)){
        randomLine = (unsigned)rand();
        programLog << randomLine;
        int newlines = 0;
        //size_t found;
        while(getline(readFile, line)){
            if(line.find("\n") != string::npos)
                newlines++;
            if(newlines == randomLine)
                numToSearch = atoi(line.c_str());
        }

    }

    programLog << "Random line selected" << endl;

    //read line
    while(std::getline(readFile,line)){
        if(atoi(line.c_str()) == numToSearch){
            foundNum = numToSearch;
            break;
        }
        else
            continue;
    }

    //square it
    const unsigned int squared = foundNum*foundNum;

    programLog << squared;
    readFile.close(); //end read
    file.close(); //end log
    programLog.close(); //end programlog
    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv){
流文件(“log.txt”,ios::app);
流程序日志(“programLog.dat”,ios::app);

cout
randomLine
被初始化为0,并且在到达while时仍然具有该值,因此循环体永远不会执行。

randomLine
被初始化为0,并且在到达while时仍然具有该值,因此循环体永远不会执行。

您永远不会在使用以下命令时进入while循环:

while(!(randomLine > min) && !(randomLine < max))
while(!(随机线>最小值)和&!(随机线<最大值))
而立即计算为false。您应使用:

while(randomLine < min || randomLine > max)
while(randomLinemax)

另外,为什么所有变量都有不同的类型?这可能会导致意外错误。您应该将它们更改为相同的类型。

在使用时,您永远不会进入while循环:

while(!(randomLine > min) && !(randomLine < max))
while(!(随机线>最小值)和&!(随机线<最大值))
而立即计算为false。您应使用:

while(randomLine < min || randomLine > max)
while(randomLinemax)
另外,为什么所有变量都有不同的类型?这可能会导致意外错误。您应该将它们更改为相同的类型。

file
file