C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Bartleby Related Questions Icon

Related questions

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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage