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 7755331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:32:28+00:00 2026-06-01T12:32:28+00:00

I am working on a program that requires reading in integers from a file

  • 0

I am working on a program that requires reading in integers from a file into a 2D array. The concept is easy and I’m generally ok with file I/O. My problem is that the file contains 20 rows of 18 numbers. The numbers are not seperated by white space. An example is:

123456789987654321
192837465564738291

I have to read each individual number into the 2D array. I have created a for loop but I’m not getting the required output from the file I/O part of the loop. Is there a way to do this or do I need to use a work around such as reading the line into a string/array and dividing it? It’s driving me mad. In the code, infile has been opened and tested. GRIDSIZE has a size of 9 and grid is the 2D array

int n;

for(int i=0; i<GRIDSIZE; i++)
      {   
    for(int j=0; j<GRIDSIZE; j++)
      {
        infile.get()>>grid[i][j];//This is causing the problem
         // infile >> n //Also tried this, not working
         // grid[i][j] = n;
        cout<<grid[i][j]<<endl;
      }
      }
  • 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-01T12:32:30+00:00Added an answer on June 1, 2026 at 12:32 pm

    Calling get() on an ifstream returns a single character, casted to an int. So try changing

    infile.get()>>grid[i][j];
    

    to

    grid[i][j] = infile.get();
    

    That will give you the ASCII value of the digit. You can then use isdigit() (as noted by stefaanv) to make sure you actually have a digit, and then just subtract 0x30 (= 48 or ‘0’) from them to get the integer values (as evident from an ASCII chart, the digits go from 0x30 to 0x39).
    So for instance:

    int n = infile.get();
    if(isdigit(n)) {
      grid[i][j] = n - '0';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a program for class that reads data from a file, the
I have a script/program I am working on that requires a configuration file (I
I'm working on an a GPU accelerated program that requires the reading of an
I am working on a php program that requires javascript on every form control.
Greetings all, I’m working on a C# program that requires being able to get
I have a program I am working on that requires us to overload the
I am working on a program that requires the date of an event to
I am working on a small program that requires the user to insert a
I've been working on a program that converts numbers into binary. As you can
ID: QTCreator 2.1, QT 4.7. Language: C++ I'm working on a program that requires

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.