Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   PGM Help (http://www.go4expert.com/forums/pgm-help-t24991/)

humidifire 16Feb2011 00:58

PGM Help
I keep getting a segmentation fault when I run it.
Program will not get past the prinf line.
Here is my code


#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc,char **argv)

    int i,j,xres,yres,max,**image;
    ofstream fp;

// xres, yres, maxintensity, and image[][] must already be defined
    cin >> xres;
    cin >> yres;
    cout << "P2" << endl;
    cout << xres << " " << yres << endl;
    cout << "255" << endl;      // Almost always equals 255
    for (i=0;i<xres;i++)
      for (j=0;j<yres;j++)
          printf("%4d", image[i][j]);
return 0;

jimblumberg 16Feb2011 10:30

Re: PGM Help
So where is image initialized?
Why are you mixing C++ io (cin/cout) and C stdio (printf) you should pick one type or the other.


humidifire 16Feb2011 12:40

Re: PGM Help
The Instructor wants me to use printf and not cout.
and image is initialized as an int.

xpi0t0s 16Feb2011 14:35

Re: PGM Help

Originally Posted by humidifire (Post 79365)
image is initialized as an int.

No it's not. See those two asterisks? They change the meaning significantly. Also look at the comment; that should give you a clue. The reason for the segfault is basically that you've ignored the comment and are trying to use an uninitialised 2D array.

All times are GMT +5.5. The time now is 08:56.