1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PGM Help

Discussion in 'C++' started by humidifire, Feb 15, 2011.

  1. humidifire

    humidifire New Member

    Joined:
    Feb 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I keep getting a segmentation fault when I run it.
    Program will not get past the prinf line.
    Here is my code

    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;
        
        fp.open(argv[1]);
        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;
    }
     
    Last edited by a moderator: Feb 16, 2011
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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.


    Jim
     
  3. humidifire

    humidifire New Member

    Joined:
    Feb 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    The Instructor wants me to use printf and not cout.
    and image is initialized as an int.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page