Function with bool return value- does not output string messages for errors
Oct 31, 2016 at 10:14pm UTC
I am writing a bool function that displays error messages if one of the conditions are not true, that are strings. When I run my function, it does not output the error messages, but only the return value of the bool function. I was wondering how I could make sure it outputs the error message, while still having a false value in the bool function as a return value. Please guide me,
Thank you
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;
string outputMessage;
string message2;
ofstream outfile(outputFile);
//change it so it reads in and then loads the ships onto the board
while (inFile2 >> shipName >> rowChar >> startCol >> direction)
{
outfile << "Loading ship: " << shipName << " " << rowChar << " " << startCol << " " << direction << endl;
startRow = findRow(rowChar);
if (startRow < 0 || !isColumnValid(startCol))
{
outputMessage = "Bad row/column:" ;
message2 = "Error: contained invalid ship" ;
outfile << outputMessage << rowChar << "," << startCol << endl;
outfile << message2 << endl;
success = false ;
}
if (direction != 'u' && direction != 'd' && direction != 'l' && direction != 'r' )
{
outputMessage = "Bad direction: " ;
message2 = "Error: contained invalid ship" ;
outfile << outputMessage << direction << endl;
outfile << message2;
success = false ;
}
if (shipName != 'A' && shipName != 'B' && shipName != 'C' && shipName != 'D' && shipName != 'S' )
{
outputMessage = "Bad ship: " ;
message2 = "Error: contained invalid ship" ;//this didn't work either.
outfile << outputMessage<< rowChar << "," << startCol << endl;
outfile << message2 << endl;
success = false ;
}
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 == 'r' )
{
endRow = startRow;
endColumn = (startCol - 1) + (shipLength - 1);
}
if (direction == 'l' )
{
endRow = startRow;
endColumn = (startCol - 1) - (shipLength - 1);
}
if (endRow >= 10 || endRow < 0 || endColumn >= 10 || endColumn < 0)
{
outfile << "Bad row/column:" << rowChar << "," << startCol << endl;
outfile << "Error: contained invalid ship" << endl;
success = false ;
}
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)
{
tempCol = startCol;
startCol = endColumn;
endColumn = tempCol;
}
for (int i = startRow; i <= endRow; i++)
{
for (int j = startCol; j <= endColumn; j++)
{
if (myArr[i][j] == '.' )
myArr[i][j] = shipName;
else
{
outfile << "Board is taken at " << i << "," << j + 1 << endl;
outfile << "Error: contained invalid ship" << endl;
success = false ;
}
}
}
}
}
}
return success;
}
Topic archived. No new replies allowed.