Thread: C++ "class" program help

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

    C++ "class" program help

    Below is a code that is used to calculate complex numbers (a+bi, where i = sqroot (-1)) through multiplication and addition. However, on my output file, no Header is being printed; the only thing that is being printed is "8 + 7i + = "

    Is anyone able to tell me what is wrong with it? "complex.h" is included at the end of the code, if you need a look.

    Code:
    // Trey Brumley// CMPS 
    // Dr. Tina Johnson
    // March 1, 2013
    // Program 2:  Classes
    // This program will demonstrate the use of classes by using a custom "complex-number" (a+bi) class.
    // =================================================================================================
    
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>
    #include "complex.h"
    using namespace std;
    
    
    void Header();
    void PrintComplex (complex cn);
    
    
    int main()
    {
        ofstream outfile;
        outfile.open("output.txt");
    
    
        Header;
        complex c1, c2, c3, c4(3.0, 4.0);
        double real, imag;
        c2.SetReal(5.0);
        c2.SetImag(3.0);
        real = c4.GetReal();
        imag = c4.GetImag();
        c3 = c2.multiply(c4);
        c1 = c2.add(c4);
        PrintComplex(c2);
        outfile << " * ";
        PrintComplex(c4);
        outfile << " = ";
        PrintComplex(c3);
        outfile << endl << endl;
        PrintComplex(c2);
        outfile << " + ";
        PrintComplex(c4);
        outfile << " = ";
        PrintComplex(c1);
        outfile << endl;
    
    
        system("pause");
        return 0;
    }
    
    
    //This function will print a header on the outfile.
    void Header()
    {
        ofstream outfile;
        outfile.open("output.txt");
    
    
        outfile << "Trey Brumley" << endl;
        outfile << "Johnson - 1063-201" << endl;
        outfile << "March 1, 2013" << endl;
        outfile << "Program 2:  Classes" << endl;
        outfile << "===================" << endl << endl;
        outfile << "This program will demonstrate the use of classes by using a custom complex-number (a+bi) class." << endl << endl;
    
    
        return;
    }
    
    
    void PrintComplex (complex c)
    {
        ofstream outfile;
        outfile.open("output.txt");
    
    
        if (c.GetImag() < 0)
            outfile << c.GetReal() << " - " << (c.GetImag() * -1) << "i ";
        else
            outfile << c.GetReal() << " + " << c.GetImag() << "i ";
    
    
    }
    
    
    complex::complex(void)
    {
    }
    
    
    
    
    complex::~complex(void)
    {
    }
    
    
    
    
    complex::complex(double r, double im)
    {
        real = r;
        imag = im;
    }
    
    
    
    
    void complex::SetReal(double r)
    {
        real = r;
    }
    
    
    
    
    void complex::SetImag(double im)
    {
        imag = im;
    }
    
    
    
    
    double complex::GetReal()
    {
        return real;
    }
    
    
    
    
    double complex::GetImag()
    {
        return imag;
    }
    
    
    complex complex::multiply(complex comp)
    {
        complex c;
        c.real = (real * comp.real) + (imag * comp.imag * -1);
        c.imag = (imag * comp.real) + (real * comp.imag);
        return c;
    }
    
    
    
    
    complex complex::add(complex comp)
    {
        complex c;
        c.real = real + comp.real;
        c.imag = imag + comp.imag;
        return c;
    }
    
    #pragma once
    class complex
    {
    private:
        double real;
        double imag;
    public:
        complex(void);
        complex(double r, double im);
        ~complex(void);
        void SetReal(double r);
        void SetImag(double im);
        double GetReal();
        double GetImag();
        complex add(complex c);
        complex multiply(complex c);
    };
    Last edited by Trey Brumley; 02-28-2013 at 04:12 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Each time you open a file for writing, you truncate its contents.
    Possible solutions:
    - Pass the file by reference from main.
    - Open the file for appending later (see constructor for possible arguments).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    How do I fix that to where only one opening of output.txt is universal? Looking back on old programs, I didn't have this problem before.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An object is tied to a file. Each time you create an object, you create a file, or that's the idea.
    So one file = one object. Pass the object via reference to your functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Never mind, I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-19-2011, 03:12 AM
  2. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Replies: 2
    Last Post: 10-03-2002, 03:43 PM