Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:58:12+00:00 2026-05-25T16:58:12+00:00

the point of my program is to write the numbers 1 – 1,000,000 to

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:58:13+00:00Added an answer on May 25, 2026 at 4:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to write a program that would display the prime numbers between
Here is my code, the point of which is to write a program that
My goal is to write a program that handles an arbitrary number of tasks
How can I write an algorithm that given a floating point number, and attempts
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that reads 2 numbers from the user and
I've been trying to write a program to read columns of text-formatted numbers into
Write a program that calculates Euler’s number e. To do this, first write a
I want to write a program that would print every combination of a set
I want to write a small program that should print something like testing CPU...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.