0

I have a .txt file that is formatted as follows:

906 25 25
1997 25 25
900 25 25
1990 50 1.75
1981 50 50
925 25 25

I have written the following code to read the numbers into an array:

float numbers[1029][2];
std::ifstream fin;
fin.open("GiftCardFinal.txt");
if (!fin.is_open())
{
    return 0;
}
for (int i = 0; i < 1029; i++)
{
    for (int j = 0; j < 3; j++)
    {
        fin >> numbers[i][j];
    }
    
}
fin.close();

However, when I run this code to test if it read the numbers correctly...

std::cout << numbers[0][0] << " ";
std::cout << numbers[0][1] << " ";
std::cout << numbers[0][2] << " ";
std::cout << std::endl << std::endl;
std::cout << numbers[1][0] << " ";
std::cout << numbers[1][1] << " ";
std::cout << numbers[1][2] << " ";

It outputs:

906 25 25

25 1997 25

Can anyone help with this or offer advice on what I can improve?

6
  • Increasing the 2nd dimension to 3 can be a good start. Commented Feb 7, 2021 at 6:10
  • std::cout << numbers[0][2] goes outside the boundaries of the 2nd array dimension. Commented Feb 7, 2021 at 6:10
  • I did originally set it to 3. I changed it to 2 when I didn’t understand why it wasn’t working Commented Feb 7, 2021 at 6:14
  • Setting it to 3 results in the following output: 906 25 1997 1997 25 900 Commented Feb 7, 2021 at 6:16
  • When you set the 2nd dimension to 3, your inner loop is not filling the 2nd dimension. Try j < 3 Commented Feb 7, 2021 at 6:20

1 Answer 1

1

Try to run this code:

#include <iostream>
#include <fstream>  
using namespace std;

#define NUM_LINE 10000

int main()
{
    float numbers[NUM_LINE][3];
    std::ifstream fin;
    fin.open("GiftCardFinal.txt");
    if (!fin.is_open())
    {
        return 0;
    }
    for (int i = 0; i < NUM_LINE; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            fin >> numbers[i][j];
        }
        
    }
    fin.close();
    
    for (int i = 0; i < NUM_LINE; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << numbers[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

906 25 25
1997 25 25
900 25 25
1990 50 1.75
1981 50 50
925 25 25
Sign up to request clarification or add additional context in comments.

6 Comments

It has to read ~1000 rows and 3 columns each
I updated my code. You can change the value of NUM_LINE if you already know the number of lines in your input file. Check this post if you do not know the number of lines in the input file beforehand.
How are the for loops different from the original ones? Thank you for the help
I moved the point of code change to one line instead of three. Check your numbers array declaration and the outer for loops. Right now you need to only change the value of number of lines in #define NUM_LINE 10000.
Your code did not work because you declared the numbers array with wrong dimension. It should be 1029 x 3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.