Yes and change it down where you display the entry after entering it in.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
bill_ 0 Newbie Poster
I have worked on it a little more since my last upload and this is wat i have now.
i want to input the text file, character by character, in the array but i seem to be getting, problems, also the writing to the file isnt working right either, but i will worry about that when i get the array storage right
#include <iostream>
#include <fstream>
using std::ofstream;
using namespace std;
int main()
{
char cabin[3][4][13]={' '};
char ch, res;
int lvl=0, row=0, col=0, l=0, r=0, c=0;
ifstream indata;
indata.open("littleswandb.txt");
if(!indata)
{
cout << "File could not be opened" << endl;
system("pause");
return 0;
}
while (indata != NULL)// keep reading until end-of-file
{
indata >> ch;
//cabin[l][c][r]= ch;
//cout << cabin[l][c][r];
if(c>3)
{
c=0; r++;
cout << endl;
}
if(r>12)
{
r=0; l++;
cout << endl;
}
if(l>2)
{
cout << "Error: level beyond 2" << endl;
}
cout << "L" << l << " R" << r << " C" << c << endl;
c++;
}
cout << endl;
indata.close();
system("pause");
cout << "Would you like to make a reservation? Y/N" << endl;
cin >> res;
while ((res == 'Y') || (res == 'y'))
{
cout << "Which Level Would you like to reserve? 1-3" << endl;
cin >> lvl;
cout << "Which Colunm Would you like to reserve? 1-13" << endl;
cin >> col;
cout << "Which Row Would you like to reserve? 1-4" << endl;
cin >> row;
lvl = lvl -1; //Minus 1 for array
row = row -1; //Minus 1 for array
col = col -1; //Minus 1 for array
if (cabin[lvl][col][row] != 'X')
{
cabin[lvl][col][row] = 'X';
lvl = lvl + 1; //Plus 1 after array use
row = row + 1; //Plus 1 after array use
col = col + 1; //Plus 1 after array use
cout << "You have booked the Cabin on Level: " << lvl
<< " Row: " << row
<< " Colunm: " << col << endl;
for(int x=0; x<3; x++)//Levels
{
for(int y=0; y<13; y++)//Colunms
{
for(int z=0; z<4; z++)//Rows
{
cout << cabin[x][z][y]; //output the array onto the screen
}
cout << endl;
}
cout << endl;
}
ofstream outdata("littleswandb.txt");
for(int d=0; d<3; d++) // Levels
{
for(int e=0; e<13; e++) //Rows
{
for(int f=0; f<4; f++) // Colunms
{
outdata<<cabin[d][f][e]; // store array into file
}
}
}
outdata.close();
system("pause");
}
else
cout << "That cabin is already booked" << endl;
cout << "Would you like to try another cabin? Y/N" << endl;
cin >> res;
}
cout << "Thank you. Good bye." << endl;
system("pause");
return 0;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
We need to get some better information.
You've been bouncing all over this program for 4 pages and still don't have your file input working right yet? In your first post you say
The main problem i am having is the storing the file character by character into the 3D array.
Your last post (#32) says:
i want to input the text file, character by character, in the array but i seem to be getting, problems...
After all this help, why is your input not working yet? Haven't we been helpful at all? Ignore everything else and get your input working!
Another problem is this -- you say (again)
i want to input the text file, character by character, in the array but i seem to be getting, problems...
Do you need more help with your problems? If so, do you think describing the problems might be beneficial?
bill_ 0 Newbie Poster
when i run the program. the out put i get is starts with:
error: level beyond 2
L4 R12 C3
Although i want it to start with
L0R0C0
L0R0C1
L0R0C2
And end with
L2R12C3 // as in l =2 , r=12, c=3
the error beyond 2 is displayed when "l" becomes larger then 2 which i dont want as "l" is wat i want to use as the "level" of the 3D array or the first dimension cabin"[3]"[4][13]
i dont know why "l" starts at '4' when it is displayed.
i am trying to get the the right numbers, so that i can then put the characters into the array
Edited by bill_ because: n/a
bill_ 0 Newbie Poster
i am getting closer to wat i wanted now
the following is the section that i have changed is below:
while (indata != NULL)// keep reading until end-of-file
{
while(l<3)
{
indata >> ch;
cabin[l][c][r]= ch;
cout << cabin[l][c][r];
//cout << l << r << c << endl;
if(c>2)
{
c = -1;
r++;
cout << endl;
}
if(r>12)
{
r = 0;
l++;
}
c++;
}
}
i now want to put the file character by character into the array, and display it as it goes in but i am getting a blank spaces first then a small square and then the first character in my file. Also i am getting spaces in between each character outputted onto the screen
Edited by bill_ because: n/a
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
I think you should start over and concentrate on only the input. Think about how you need to loop and display your values at every point. Once you get the the end of the the file, output your matrix and make sure it's right.
Then, and only then, move to the next task.
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Will your instructor vary up the input file any? Since you are hard-coding the array dimensions anyway you could potentially (though with loss of flexibility) get your input via 3 nested for loops stepping through the array.
This
if(c>3)
{
c=0;
r++;
cout << endl;
}
if(r>12)
{
r=0;
l++;
}
c++;
may or may not work but it's kinda kludgy either way.
Edited by jonsca because: n/a
bill_ 0 Newbie Poster
Will your instructor vary up the input file any?
im not sure wat ur asking me here
and i thought about something like this for the for loop instead of the if statements
im testing out now
cout << ch;
indata >> ch;
for(int l =0;l>3;l++)
{
//cabin[l][c][r] = ch;
for(int r=0;r>13;r++)
{
//cabin[l][r][c] = ch;
for(int c=0;c>4;c++)
{
cabin[l][c][r] = ch;
cout << cabin[l][c][r];
}
cout << endl;
}
cout << endl;
}
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
im not sure wat ur asking me here
Was asking if he/she would change the number of rows or what each row contained or really anything that would require the loops to change based on the data. If you've used the for loops below I'll take that as a no.
and i thought about something like this for the for loop instead of the if statements
im testing out nowcout << ch; indata >> ch; for(int l =0;l>3;l++) { //cabin[l][c][r] = ch; for(int r=0;r>13;r++) { //cabin[l][r][c] = ch; for(int c=0;c>4;c++) { cabin[l][c][r] = ch; cout << cabin[l][c][r]; } cout << endl; } cout << endl; }
There's a couple of gotchas in the above code. You put the greater than signs by mistake. Also you are inconsistent as to the order of l r c (some of the lines were commented out) Make sure you keep those in the proper oder What you have there is along the lines of my thoughts -- you should just eliminate the indata>>ch
and use indata >>cabin[l][c][r];
(or [l][r][c] whatever you decide) in the middle there(line 11, unlabeled ). Changing the variables back to that [L][R][C] order is more in line with how your datafile is laid out -- just be sure to keep consistent!
Edited by jonsca because: n/a
bill_ 0 Newbie Poster
i have worked some more on the for loop approach. I have got rid of the spaces in between each character but there are still extra spaces every 5th character that i am having trouble getting out of the way.
the way i have gotten rid of the extra spaces so far was putting them in the array "rubbish"
the following is my code so far:
#include <iostream>
#include <fstream>
using std::ofstream;
using namespace std;
int main()
{
char cabin[3][4][13]={' '};
char rubbish[160]={' '};
char ch, rub;
int d=0,e=0;
ifstream indata;
indata.open("littleswandb.txt");
if(!indata)
{
cout << "File could not be opened" << endl;
system("pause");
return 0;
}
indata >> ch;//rubbish char
indata >> ch;//rubbish char
for(int l=0;l<3;l++)
{
for(int r=0;r<13;r++)
{
for(int c=0;c<4;c++)
{
indata >> cabin[l][c][r];
cout << cabin[l][c][r];
indata >> rubbish[d];
d++;
}
cout << endl;
}
cout << endl;
}
cout << endl;
indata.close();
system("pause");
return 0;
}
the out put looks like this:
BIIB
BII
B BI
IB B
IIB
BIIB
WII
i am trying to make it like this (but 3 times):
BIIB
BIIB
BIIB
BIIB
BIIB
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Look at lines 21,23,25. Now look at line 27. You are still not being consistent with which variable is which. It should be [l][r][c] on line 27. If you read along your first output that looks jumbled the proper order of the letters is there but the pattern is all scattered. Unfortunately all you've done is put a band-aid over a huge bug.
bill_ 0 Newbie Poster
the cabin array is:
char cabin[3][4][13]={' '};
the for loop with 'l' is set to end at 3 then it goes in the first dim of the array: cabin[l]
the for loop with 'c' is set to end at 4 then it goes in the second dim of the array: cabin[l][c]
the for loop with 'c' is set to end at 13 then it goes in the third dim of the array: cabin[l][c][r]
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
I'm basing my opinion on this snippet that I wrote
char cabin[3][13][4];
ifstream ifs("littleswandb.txt");
for (int l = 0;l<3;l++)
for(int r = 0;r<13;r++)
for(int c = 0;c<4;c++)
ifs>>cabin[l][r][c];
for (int l = 0;l<3;l++)
{
for(int r = 0;r<13;r++)
{
for(int c = 0;c<4;c++)
{
cout<<cabin[l][r][c]<<" ";
}
cout<<endl;
}
cout<<endl;
}
It takes in the data properly and outputs exactly what was in the file. No need for a "rubbish" variable.
Edited by jonsca because: spacing argh
bill_ 0 Newbie Poster
problem solved. the problem wasnt with the code it was with the text file. i had a new line for every 4 characters which i would have liked, because that is the format i wanted, but it wasnt needed. i jst pt everything in a st8 line, and its working now. thank you jonsca for bearing with me. it was my first time asking for help on forums
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.