my  c++ code isn't working, i want to transfer numbers 1-9 in my file to an 2D array but the array only gets 9 being put into it. the file is also returning an extra 9. Below is the code: #include #include using namespace std; int main() { int numbers; const int rows = 3; const int columns = 3; int array [rows] [columns]; ofstream write; write.open("file.txt"); write << 1 << endl << 2 << endl << 3 << endl << 4 << endl << 5 << endl << 6 << endl << 7 << endl << 8 << endl << 9 << endl; write.close(); ifstream read ("file.txt"); while (!read.eof()) { if (read.eof()) break; read >> numbers; cout << numbers << endl; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { array [r] [c] = numbers; } } } for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { cout << array [r] [c] <<" "; } cout << endl; } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 3PE
Question
100%

my  c++ code isn't working, i want to transfer numbers 1-9 in my file to an 2D array but the array only gets 9 being put into it. the file is also returning an extra 9. Below is the code:

#include <iostream>
#include <fstream>


using namespace std;
int main() {
int numbers;
const int rows = 3;
const int columns = 3;
int array [rows] [columns];

ofstream write;
write.open("file.txt");
write << 1 << endl << 2 << endl << 3 << endl << 4 << endl << 5 << endl << 6 << endl << 7 << endl << 8 << endl << 9 << endl;
write.close();

ifstream read ("file.txt");
while (!read.eof())
{
if (read.eof())
break;
read >> numbers;
cout << numbers << endl;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
array [r] [c] = numbers;
}
}
}

for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
cout << array [r] [c] <<" ";
}
cout << endl;
}
}

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage