Function with bool return value- does not output string messages for errors
Oct 30, 2016 at 3:17pm UTC
Hi,
I was writing a function whose goal is to display some ships on a 10 by 10 char array,and to display some error messages if the ship does not match specifications. I know that these errors are being handled properly, but due to the fact that I am writing a bool function, when it is called, I only get the value 0 in output to my file instead of the error message. Please guide me on how to make it return both the bool value and display the error message. I have tried many things, but cannot seem to find a solution. Thank you. The function is of bool return type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
bool loadShips(string inputFile, char myArr[10][10]) {
bool success = true ;
// variables for reading line
char shipName;
char rowChar;
int startCol;
char direction;
// variables used for ship's name, length, position etc
int startRow;
string name;
int shipLength = 0;
int endColumn = 0;
int endRow = 0;
int tempRow = 0;
int tempCol = 0;
string longShipName;
//need to open the input file first
ifstream inFile(inputFile);
ifstream inFile2;
inFile.ignore(7, '\t' );
string boardName;
inFile >> boardName;
inFile2.open(boardName);
//have to open the board and then do that processing.
string outputFile;
ofstream outfile(outputFile);
//change it so it reads in and then loads the ships onto the board
while (inFile2 >> shipName >> rowChar >> startCol >> direction && success)
{
outfile << "Loading ship: " << shipName << " " << rowChar << " " << startCol << " " << direction << endl;
startRow = findRow(rowChar);
if (startRow < 0 || !isColumnValid(startCol))
{
success = false ;
if (success == 0)
{
outfile << "Bad row/column:" << rowChar << "," << startCol << endl;
outfile << "Error: contained invalid ship" << endl;
}
}
if (direction != 'u' && direction != 'd' && direction != 'l' && direction != 'r' )
{
success = false ;
if (success == 0)
{
outfile << "Bad direction: " << direction << endl;
outfile << "Error: contained invalid ship" ;
}
}
if (shipName != 'A' && shipName != 'B' && shipName != 'C' && shipName != 'D' && shipName != 'S' )
{
success = false ;
if (success == 0)
{
outfile << "Bad row/column:" << rowChar << "," << startCol << endl;
outfile << "Error: contained invalid ship" << endl;
}
}
else
{
if (shipName == 'A' ) {
shipLength = size_A;
longShipName = "Air Craft" ;
}
else if (shipName == 'B' ) {
shipLength = size_B;
longShipName = "Battle ship" ;
}
else if (shipName == 'C' ) {
shipLength = size_C;
longShipName = "Carrier" ;
}
else if (shipName == 'S' ) {
shipLength = size_S;
longShipName = "Submarine" ;
}
else if (shipName == 'D' ) {
shipLength = size_D;
longShipName = "Destroyer" ;
}
outfile << shipName << " direction " << direction << shipLength << endl;
if (direction == 'u' )
{
endColumn = startCol - 1;
endRow = startRow - (shipLength - 1);
}
if (direction == 'd' )
{
endColumn = startCol - 1;// -1 is used because columns are 0 based . only matters on up/down
endRow = startRow + (shipLength - 1);
}
if (direction == 'l' )
{
endRow = startRow;
endColumn = (startCol - 1) - (shipLength - 1);
}
if (direction == 'r' )
{
endRow = startRow;
endColumn = (startCol - 1) + (shipLength - 1);
}
if (endRow >= 10 || endRow < 0 || endColumn >= 10 || endColumn < 0)
{
success = false ;
if (success == 0)
{
outfile << "Bad row/column:" << rowChar << "," << startCol << endl;
outfile << "Error: contained invalid ship" << endl;
}
}
startCol -= 1; // startCol is being decremented here so that validation messages can show the position
// read from file
if (success==1) {
if (startRow > endRow)
{
tempRow = startRow;
startRow = endRow;
endRow = tempRow;
}
if (startCol > endColumn)
{
tempRow = startRow;
startRow = endRow;
endRow = tempRow;
}
//cout << shipName << " Length" << shipLength << " startcol:" << startCol << " endcol:" << endColumn << " startrow:" << startRow << " endrow:" << endRow << endl;
for (int i = startRow; i <= endRow; i++)
{
for (int j = startCol; j <= endColumn; j++)
{
if (myArr[i][j] == '.' )
myArr[i][j] = shipName;
else
{
success = false ;
if (success == 0)
{
outfile << "Board is taken at " << i << "," << j + 1 << endl;
outfile << "Error: contained invalid ship" << endl;
}
}
}
}
}
}
}
return success;
}
Last edited on Oct 30, 2016 at 3:23pm UTC
Topic archived. No new replies allowed.