#include <iostream> #include <fstream> #include <string> #include <cstdlib> usin
ID: 2246505 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 150;
class color
{
protected:
int a[SIZE][SIZE*3];
int m;
int n;
public:
color();
color(unsigned char, unsigned char, unsigned char);
unsigned char r;
unsigned char g;
unsigned char b;
static const int Width = 240;
static const int height = 320;
int getM()const;
void setM(int m);
int getN()const;
void setN(int n);
void writeppm(string);
void createpicture();
};
int color::getM()const{
return m;
}
void color::setM(int M){
this->m=m;
}
int color::getN()const{
return n;
}
void color::setN(int n){
this->n=n;
}
color::color(){
r = 0;
g = 0;
b = 0;
}
color::color(unsigned char, unsigned char, unsigned char){
r = 0;
g = 0;
b = 0;
}
void color::writeppm(string filename){
ofstream File(filename.c_str());
int amount = 255;
File << "P3" << endl;
File << "Created by " << endl;
File << Width << " " << height << endl;
File << amount << endl;
File << " " << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
File << a[i][j]<< endl;
File << endl;
}
File << endl;
}
void color::createpicture(){
color grey(127,127,127);
for(int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j*3]=grey.r;
a[i][j*3+1]=grey.g;
a[i][j*3+2]=grey.b;
}
}
}
int main()
{
color X;
X.createpicture();
X.writeppm("image1.ppm");
cout << "Image has been created!!" << endl;
return 0;
}
I am not gettint the image -getting the pm, width height - but no image no cout image has been created - what I'm I doing wrong? thanks C++
Explanation / Answer
Problem with your code:
m and n are not set.
m was found to be -2144153320 and n was 1 and hence the loop didnot work. NOTE that both the values are garbage values since m and n are never set in the code you provided.
We must set m and n to their default values inside the constructor (both default and parameterized). That should make the code work as intended.
I hope this resolves your issue. If this does not resolve your issue, please let me know. Shall help you to resolve the issue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.