My unhappy paths for this function are not passing. Due to the grading software, I am unable
Question:
My unhappy paths for this function are not passing. Due to the grading software, I am unable to see what is being entered in so I can't see what I'm missing in this code. Can someone fix my code and "REWRITE THE ENTIRE CODE" provided with the changes made, please.
Here is the information given for the function:
Here is my functions:
#include
#include
#include
#include
#include "functions.h"
using namespace std;
void initializeImage(Pixel image[][MAX_HEIGHT]) {
// iterate through columns
for (unsigned int col = 0; col < MAX_WIDTH; col++) {
// iterate through rows
for (unsigned int row = 0; row < MAX_HEIGHT; row++) {
// initialize pixel
image[col][row] = {0, 0, 0};
}
}
}
void loadImage(string filename, Pixel image[][MAX_HEIGHT], unsigned int& width, unsigned int& height) {
ifstream ifs(filename);
if (!ifs.is_open()) {
throw std::runtime_error("Failed to open " + filename);
}
string Type;
ifs >> Type;
if (toupper(Type.at(0)) != 'P' || Type.at(1) != '3') {
throw std::runtime_error("Invalid type " + Type);
}
ifs >> width;
if (ifs.fail() || width <= 0 || width > MAX_WIDTH) {
throw std::runtime_error("Invalid dimensions");
}
ifs >> height;
if (ifs.fail() || height <= 0 || height > MAX_HEIGHT) {
throw std::runtime_error("Invalid dimensions");
}
int colorMax = 0;
ifs >> colorMax;
if (ifs.fail() || colorMax != 255) {
throw std::runtime_error("Invalid color value");
}
for (unsigned int row = 0; row < height; ++row) {
for (unsigned int col = 0; col < width; ++col) {
ifs >> image[col][row].r;
if (ifs.fail() || image[col][row].r < 0 || image[col][row].r > 255) {
throw std::runtime_error("Invalid color value");
}
ifs >> image[col][row].g;
if (ifs.fail() || image[col][row].g < 0 || image[col][row].g > 255) {
throw std::runtime_error("Invalid color value");
}
ifs >> image[col][row].b;
if (ifs.fail() || image[col][row].b < 0 || image[col][row].b > 255) {
throw std::runtime_error("Invalid color value");
}
}
}
}
Error message:
loadImage (unhappy paths) (0/3)
Filename does not exist
Preamble type is invalid (i.e. not P3 or p3)
Preamble height is not an integer
Color value is negative
Not enough color values in ppm
Too many color values in ppm expected loadImage(filename, image, w, h) to throw std::runtime_error, but nothing thrown
[FAIL] some test(s) failed in main
Business Communication Essentials a skill based approach
ISBN: 978-0132971324
6th edition
Authors: Courtland L. Bovee, John V. Thill