Question: Why is my code not opening the text file ? ? I keep getting an error message ? ? Am I inserting the text file

Why is my code not opening the text file ?? I keep getting an error message ?? Am I inserting the text file into the program incorrectly ?
#include
#include
using namespace std;
// Function to calculate piece value
int getPieceValue(char piece){
switch (piece){
case 'P': case 'p': return 1;
case 'N': case 'n': return 3;
case 'B': case 'b': return 3;
case 'R': case 'r': return 5;
case 'Q': case 'q': return 9;
default: return 0; // For kings and empty spaces
}
}
// Function to check if piece is black
bool isBlackPiece(char piece){
return piece >='A' && piece ='Z';
}
// Function to read and display chessboard, and calculate scores
void processChessboard(ifstream& file){
char board[8][8];
int blackScore =0, whiteScore =0;
// Reading chessboard (8x8 characters)
for (int i =0; i 8; i++){
for (int j =0; j 8; j++){
file >> board[i][j];
char piece = board[i][j];
if (piece !='.'){
int value = getPieceValue(piece);
if (isBlackPiece(piece)){
blackScore += value;
}
else {
whiteScore += value;
}
}
}
}
// Display the chessboard
cout "Chessboard:" endl;
for (int i =0; i 8; i++){
for (int j =0; j 8; j++){
cout board[i][j]"";
}
cout endl;
}
// Display the scores
cout "Black Pieces Value: " blackScore endl;
cout "White Pieces Value: " whiteScore endl;
cout "----------------------------------------" endl;
}
int main(){
ifstream file("chessboards.txt");
if (!file){
cerr "Error opening file!" endl;
return 1;
}
// Processing each chessboard in the file
while (!file.eof()){
processChessboard(file);
}
file.close();
return 0;
}
Why is my code not opening the text file ? ? I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!