Code Help (Redone)

This is a discussion on Code Help (Redone) within the C++ Programming forums, part of the General Programming Boards category; I redid my code completely, building around a skeleton my teacher provided. The code is below. It does not print ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    67

    Code Help (Redone)

    I redid my code completely, building around a skeleton my teacher provided. The code is below. It does not print to the output file for some reason, unless I take the section starting with

    Code:
    if (constsum != comparesum)
    and ending with

    Code:
    y++
    }
    }
    out of the code.

    It only prints the magic squares in my input file in proper format, meaning the output file is open correctly. However, it does not print my heading. Can anyone help me out again?

    Code:
    // Trey Brumley// Johnson 1063
    // Purpose: The purpose of this program is to 
    // Input: squares.txt - 4 5x5 arrays of numbers
    // Output: output.txt - Print heading, reprint the arrays and tell 
    //                        whether they are magic squares or not.
    // Usage: What the command line should look like:       
    //            executable_name  param1 param2 �
    //                       Example:
    //        encrypt input_file output_file 
    //---------------------------------------------------------------------
    
    
    
    
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    
    
    using namespace std;
    
    
    const int SIZE = 5;
    
    
    void Heading(ofstream &out);
    void GetSquare(int s[][SIZE], ifstream &in);
    void PrintSquare(int s[][SIZE], ofstream &out);
    int RowSum(int s[][SIZE], int r);
    int ColSum(int s[][SIZE], int c);
    int D1Sum(int s[][SIZE]);
    int D2Sum(int s[][SIZE]);
    
    
    int main(int argc, char* argv[])
    {
        ifstream infile;    
        ofstream outfile;
        int square[SIZE][SIZE];
        int constsum;
        int comparesum;
        bool magic;
        int x=1, y=1;
    
    
        Heading(outfile);
    
    
        infile.open(argv[1]);
        if(infile.fail())
        {
            cout << "Could not open file. \n\n";
            system("pause");
            return 0;
        }
        outfile.open(argv[2]);
        for (int i = 0; i < 4; i++)
        {
            GetSquare(square, infile);
    
    
            constsum = D1Sum(square);
            comparesum = D2Sum(square);
    
    
            if (constsum != comparesum)
                magic = false;
            else
                while (x <= SIZE)
                    {
                        comparesum = RowSum(square, x);
                        if (constsum != comparesum)
                            magic = false;
                        else
                        {
                            magic = true;
                            x++;
                        }
                    }
                while (y <= SIZE)
                    {
                        comparesum = ColSum(square, y);
                        if (constsum != comparesum)
                            magic = false;
                        else
                        {
                            magic = true;
                            y++;
                        }
                    }
    
    
            PrintSquare(square, outfile);
            if (magic == true)
                outfile << "The array above is a magic square with a constant sum = " << constsum << ". \n\n";
            else
                outfile << "The array above is not a magic square. \n\n";
        }
    
    
        infile.close();
        outfile.close();
        system("pause");
        return 0;
    }
    
    
    //------------------------------------------------------------------------------
    // The following function will print out a heading for the hard copy of results.
    //------------------------------------------------------------------------------
    void Heading(ofstream &out)
    {
        out << "Trey Brumley" << endl;
        out << "CMPS 1063 - Johnson" << endl;
        out << "February 8, 2013" << endl;
        out << "Program 1 - Magic Squares" << endl;
        out << "=========================" << endl << endl;
     
     
        out << "This program will take a certain number of number squares as determined by the user, and it will compare the sums of each row, column, and diagonal." << endl;
        out << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;
    }
    
    
    //--------------------------------------------------------------------------------------
    // The following function will read in numbers into a 2D array, forming a number square.
    //--------------------------------------------------------------------------------------
    
    
    void GetSquare(int s[][SIZE], ifstream &in)
    {
        for (int r = 0; r < SIZE; r++)
            for (int c = 0; c < SIZE; c++)
                in >> s[r][c];
    }
    
    
    //-----------------------------------------------------------------
    // This function will print the number square in the proper format.
    //-----------------------------------------------------------------
    
    
    void PrintSquare(int s[][SIZE], ofstream &out)
    {
        
        for (int r = 0; r < SIZE; r++)
        {
            for (int c = 0; c < SIZE; c++)
                out << setw(3) << s[r][c];
                out << endl;
        }
    }
    
    
    //-------------------------------------------------------------------------------
    // This function will determine and pass back the sum of the numbers in each row.
    //-------------------------------------------------------------------------------
    
    
    int RowSum(int s[][SIZE], int r)
    {
        int sum = 0;
        for(int c = 0; c < SIZE; c++)
            sum += s[r][c];
        return sum;
    }
    
    
    //----------------------------------------------------------------------------------
    // This function will determine and pass back the sum of all numbers in each column.
    //----------------------------------------------------------------------------------
    
    
    int ColSum(int s[][SIZE], int c)
    {
        int sum = 0;
        for(int r = 0; r < SIZE; r++)
            sum += s[r][c];
        return sum;
    }
    
    
    //-----------------------------------------------------------------------------------------
    // This function will determine and pass back the sum of all numbers in the first diagonal.
    //-----------------------------------------------------------------------------------------
    
    
    int D1Sum(int s[][SIZE])
    {
        int sum = 0;
        for(int r = 0; r < SIZE; r++)
        {
            int c = r;
            sum += s[r][c];
        }
        return sum;
    }    
    
    
    //------------------------------------------------------------------------------------------
    // This function will determine and pass back the sum of all numbers in the second diagonal.
    //------------------------------------------------------------------------------------------
    
    
    int D2Sum(int s[][SIZE])
    {
        int sum = 0;
        for(int r = 1; r < SIZE+1; r++)
        {
            int c = 6 - r;
            sum += s[r][c];
        }
        return sum;
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    You try to print the header (ie call Heading(outfile); ) before you open the output file (ie call outfile.open(argv[2]); )
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21