Question

Create a C++ program that reads in from a file numbers.txt and stores each value of...

Create a C++ program that reads in from a file numbers.txt and stores each value of each line into a two dimensional array ( int array [4][4] )

The array should be filled row by row from left to right.

numbers.txt :

1,8,7,3

3,5,9,2

2,4,6,2

9,3,6,2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!

Note:

   char filename[] = "F:\\numbers.txt"; // update the file name and path here


===========================================================================

#include<iostream>
#include<fstream>

using namespace std;

int main(){
  
  
   char filename[] = "F:\\numbers.txt"; // update the file name and path here
  
   ifstream infile(filename);
  
   if(!infile.is_open() || infile.bad()){
       cout<<"Error: Unable to open file: " << filename << endl ;
       return 1;
   }
  
   int array[4][4]; int num; char comma;
   for(int row = 0; row < 4; row++){
       for(int col= 0; col < 4; col++){
          
           infile >> array[row][col];
           infile.ignore(1,'\n');
       }
   }
   infile.close();
  
  
   cout<<"Printing the array :\n";
   for(int row = 0; row < 4; row++){
       for(int col= 0; col < 4; col++){
          
           cout<< array[row][col] <<" ";
       }
       cout<<endl;
   }
  
   return 0;
}

=====================================================================

1 #include<iostream> #include<fstream> 2 3 4 5 using namespace std; 6 int main() { C:\Users\User\Documents\numbers.exe Printi

Add a comment
Know the answer?
Add Answer to:
Create a C++ program that reads in from a file numbers.txt and stores each value of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT