程序总是产生相同的值?

发布于 2024-12-05 12:24:36 字数 2044 浏览 1 评论 0原文

我的程序的要点是将数字 1 - 1,000,000 写入文本文件,生成 1 到 1,000,000 之间的随机数,在文本文件中搜索该行,获取该值,然后对其进行平方(这只是一个练习)对我来说,它没有实际应用)。问题是,每当我运行它时,该值保持不变,但 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;
}

the point of my program is to write the numbers 1 - 1,000,000 to a text file, generate a random number between 1 and 1,000,000, search for that line in the text file, take the value, and square it (this is just an exercise for me, it has no practical application). The problem is that whenever I run it, the value remains the same, but the rand() function is seeded by time(0). I suspect that it's a garbage value but I don't know where it's coming from (I have no experience with GDB or any other standalone debuggers). Here's my source code:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-12-12 12:24:36

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

while(!(randomLine > min) && !(randomLine < max))

while 立即计算为 false。您应该使用:

while(randomLine < min || randomLine > max)

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

You never enter the while loop as you are using:

while(!(randomLine > min) && !(randomLine < max))

while immediately evaluates to false. You should use:

while(randomLine < min || randomLine > max)

Also, why do all your variables have different types? This could lead to unintended errors. You should change them to have the same type.

仙气飘飘 2024-12-12 12:24:36

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

randomLineis initialized to 0, and still has that value once it reaches the while, so the loop body never executes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文