functions and pgm help please!

Hey guys can you help me please! I am trying to write this code but I am having the hardest time. I suppose to add three functions and their prototypes. This program should read a PGM from a file, save it to a new file, and print the image header and pixel values to the console. You'll need one function to open a PGM file, a second one to save the PGM file already read into new PGM file, and the third one to display the array to the console. Heres the code so far with comments to show where everything is suppose to go. Any hints/suggestions/help is greatly appericated:

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
#include <iomanip>
#include <iostream>
#include <fstream>


using namespace std;

void get_filenames(string &infname, string &outfname);
    
//...............put your prototypes here...................


//..........................................................

int main (void)

{

    string infilename, outfilename;
            // for image:
    string type;
    int ncols=0, nrows=0, maxval=0;
    static int pixels[1024 * 1024] = {0};       // large array needs to be static!
    
    get_filenames(infilename, outfilename);
    
    cout << "reading "<<infilename<<", displaying, and copying to "<<outfilename<<endl;
    
    // Read the PGM file
 
    readPGM(infilename, type, ncols, nrows, maxval, pixels);
    if (ncols*nrows == 0)       // if image array is size 0, problem!
    {
        cout << "error reading file "<<infilename<<endl;
        return 0;
    }   
 
     // Display the PGM header info and the pixel array
     
//...........add your function call to printPGM here!

    
    // Save PGM file

//...........add your function call to savePGM here! 


     return 0;
}


        // get input & output filenames, verify input file can be read
        // outputs:  infname and outfname, both strings.
void get_filenames(string &infname, string &outfname)
{
    cout << "Enter the name of the PGM file: ";
    cin >> infname;
    ifstream ifs(infname.c_str());   // verify file is readable
    if (!ifs)
    {
        cout << "Error:  can't open "<<infname<< " for reading "<<endl;
        exit(1);    // bail out of program altogether.
    }
    ifs.close();
    
    cout << "enter the outputname to copy the PGM to: ";
    cin >> outfname;
    ofstream ofs(outfname.c_str());   // verify we can write to the output file
    if (!ofs)
    {
        cout << "Error:  can't open "<<outfname<< " for writing "<<endl;
        exit(1);    // bail out of program altogether.
    }
    ofs.close();    // close it.  you'll open it later in savePGM
}

//...............put your prototypes here...................


Topic archived. No new replies allowed.