I can't read and display the file properly

Jun 15, 2017 at 2:56pm
Hi,
I am having issue with one of my programs. My program is supposed to read from command from the simulation text and find values form sample text. So, far I have been able to read almost everything except the Array! I tried to cout my array and it gives me weird character or nothing. I don't know how to use the code function for the forum post...

Header File:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

using std:: cin;
using std:: cout;
using std:: endl;
using std:: string;
using std:: ifstream;
using std:: istream;
using std:: ofstream;
using std:: ostream;

const int ROWS=30;
const int COLS=30;

void openSimulatorfile(string input, string output);

Body:


#include "fire.h"

void openSimulatorfile(string input, string output)
{
ifstream in (input);

string command1, command2;
string command3, text;
int com1,com2;

in>>command1>>com1>>command2>>com2>>command3>>text;
while (!in.fail() )
{
cout<<command1<<com1<<endl<<command2<<com2<<endl<<command3<<text<<endl;
///////////////////////try2//////////////////
char numbers [ROWS][COLS];
ifstream in2(text);
string junk1,junk2;
int ncols,nrows;
in2>>junk1>>ncols>>junk2>>nrows;

while (!in2.fail())
{
cout<<junk1<<ncols<<endl<<junk2<<nrows<<endl<<endl;
in2>>junk1>>ncols>>junk2>>nrows;
}
//////////////// For Reading the Array//////////////////
for (int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{
in2>>numbers[i][j];

}

}

in>>command1>>command2>>command3;
}


}

Simulation text:

Moisture: 3
Burn: 5
Grid: sample.txt



Sample text:
Rows: 5
Cols: 5

n n n n n
e n n n e
n n b n n
e n n n e
n n n n n


My Output so far:

Moisture:3
Burn:5
Grid:sample.txt
Rows:5
Cols:5
Last edited on Jun 15, 2017 at 2:57pm
Jun 15, 2017 at 3:37pm
what are you expecting to see?
you call it numbers, its reading characters. It appears to have spaces in the file text. I can't recall if >> reads spaces for characters or skips them, but that might be part of it. Its been a while since I read char by char, usually do lines or whole files at once.

What exactly did you get? Your loop looks more or less correct, but it would be really helpful to see what you got into "numbers" from the exact file that you showed here.

Jun 15, 2017 at 5:34pm
Moisture:3
Burn:5
Grid:sample.txt
Rows:5
Cols:5
n
Jun 15, 2017 at 5:35pm
That's what I am getting each time I run the code. @Jonnin
I am expecting it to print the Array (nn etc)
Last edited on Jun 15, 2017 at 5:35pm
Topic archived. No new replies allowed.