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

  • SEARCH
  • Home
  • 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 7748315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:45:10+00:00 2026-06-01T10:45:10+00:00

I am working on a program that reads a series of ints from a

  • 0

I am working on a program that reads a series of ints from a text file into a 2D array.

The file contains 40 lines of 81 numbers that have no spaces between them.

The problem is that when I cout the array after the loop has finished, it is outputting 2 random numbers in array[0][0] and array[0][1] before the expected output. I think it has to do with the newline/carriage return characters. The first iteration of the loop runs perfectly. Here is the code:

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  int array[9][9];

  //Open file:

  fstream ifile;

  ifile.open("numbers.txt", ios::in);
  if (ifile.fail())
    {
      cout << "Could not open numbers.txt" << endl;
      return -1;
    }

   while (!ifile.eof())
    {
      for(int i=0; i<9; i++)
      {   
    for(int j=0; j<9; j++)
    {
       int n = ifile.get();
              if(isdigit(n)) 
            {
                  array[i][j] = n - '0';
        }

          cout<<"array ["<<i<<"]["<<j<<"] is "<<array[i][j]<<endl;
      } 
      } cout<<"This is a test"<<endl;
    }

  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-06-01T10:45:11+00:00Added an answer on June 1, 2026 at 10:45 am

    The random numbers appear because you increment j regardless of whether you write to grid[i][j].

    Try replacing your inner loop with:

    for(int j=0; j<9; )
    {
      int n = file.get();
      if(!file) break;
      if(isdigit(n))
      {
        array[i][j] = n - '0';
        cout<<"array ["<<i<<"]["<<j<<"] is "<<array[i][j]<<endl;
        j++;
      }
      cout<<"grid ["<<i<<"]["<<j<<"] is "<<grid[i][j]<<endl;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that reads arbitrary data from a file system and outputs
I'm working on a program for class that reads data from a file, the
i am working on a program that reads in a text file that the
I am working on a program that reads a text file and stores parts
Background I have a Spring batch program that reads a file (example file I
I have a program that accepts two file names as arguments: it reads the
I am working on a program that reads in from a serial port, then
I have a program that simulates a text editor. It lets users add lines
I am working on a program that (among other things) reads a CSV file
I'm working on a program that reads in users input as a string, which

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.