Some of the characters that you are reading are the newline characters - you don't want those in your board array. Also, you are expecting to read 8 characters per line, but there is no guarantee that the input file maintains trailing blanks.
It is unclear from your question what the format of the file is. Are the /* and */ part of the file or not? You would be better putting the original in output tags (second item in the format menu) so that it preserves the spaces.
You don't need the while{ } loop - you've only got one board.
Personally, I would have used string board[SIZE], rather than char board[SIZE][SIZE], as you could then read and write more easily.
Please don't double-space unnecessarily, and try to indent consistently (which usually means using spaces rather than invisible tabs - or an unholy mix of the two).
Yes the /**/ is the actual input file I am reading from. For the assignment I am meant to use char [][SIZE], and have the input file read from and printed out.
Could you post the actual input file and place it within output tags so that we can see any spacing. (Just look at your original post online - it is rather ambiguous - THAT IS NOT A VALID CHESSBOARD)
Obviously if you put '/', '*', '\' in the file, and it also contains newline character(s), then they will end up getting read into the board array.
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
const size_t SIDE = 8;
bool loadBoard(const std::string& file, char board[][SIDE]) {
std::ifstream infile;
infile.open(file);
if (!infile.is_open())
returnfalse;
infile.ignore(2); //Ignore /*
for (size_t i = 0; i < SIDE; ++i) {
std::string row;
getline(infile, row); // Read each row as a string
for (size_t j = 0; j < SIDE; ++j)
board[i][j] = j < row.size() && (std::isalpha(row[j]) || std::isspace(row[j])) ? std::toupper(row[j]) : ' ';
}
infile.close();
returntrue;
}
void displayBoard(char board[][SIDE])
{
for (int i = 0; i < SIDE; ++i) {
for (int j = 0; j < SIDE; ++j)
std::cout << board[i][j];
std::cout << '\n';
}
}
int main()
{
char board[SIDE][SIDE] = {0};
std::string file;
std::cout << "Enter filo name: ";
std::getline(std::cin, file);
if (loadBoard(file, board))
displayBoard(board);
else
std::cout << "Cannot open file " << file << '\n';
}
It's usually better to have a function to do just one job - read or display etc. Your loadBoard() obtains the name of the file, reads it and displays the array. I'd suggest this is split as per above.
Given a file of:
/*r bqk nr
pppppp p
n p
P
P
P PP PPP
RNBQKB R*/
displays:
R BQK NR
PPPPPP P
N P
P
P
P PP PPP
RNBQKB R
as required with all input converted to uppercase.
Sorry everyone, this is my first time using this website so I am unfamiliar on posting. But could someone tell me on how to post the actual input file?
You start by saying that this is an assignment. It is helpful to post the actual requirements of the assignment , so everyone will know what you have to do.
It also helps to post enough code that can be compiled and run for testing. Do not make people guess at what is missing.
I am guessing that your imput file might look like this:
Please change the input filename in the below to that of your own, run the code, then upload the output.txt file to this forum.
What you have posted so far is an invalid chessboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
usingnamespace std;
int main()
{
string line;
ifstream in ( "input.txt" ); // replace with the name of your input file
ofstream out( "output.txt" );
while( getline( in, line ) )
{
replace( line.begin(), line.end(), ' ', '_' );
out << line << '\n';
}
}