Help with classes & structures

This is a discussion on Help with classes & structures within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by PenguinTux what am I doing that's making it take coeffstruct? and what can i do to make ...

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by PenguinTux View Post
    what am I doing that's making it take coeffstruct? and what can i do to make it take double?
    ... You really have a long way to go! Try getting and reading a C++ book or pdf. This one is very good.
    Last edited by GReaper; 04-10-2011 at 06:01 PM.
    Devoted my life to programming...

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    2,803
    Are you sure you even need to pass anything to the function? Are you trying to do something with the coeffs array that is in your structure?
    Code:
    struct coeffStruct {
    double coeffs[3];
    bool coeffsFlag; // is a equal zero?
    };
    Jim

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    I'm using them to solve a quadratic equation.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    2,803
    After looking closer at your code you should not need to pass any variables into your functions. You have a read function that stores values into the structure coefStructure. Then all of the other functions, which are members of your class can use the values held in this structure to do their calculations.

    You really need to study up on how classes operate. You can start with these links: Classes I and Classes II. These two links along with your textbook should shed some light on the subject of C++ Classes.


    Jim

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Thanks, we just started learning classes and my professor is giving us an assignment that involves a bit more than what he's taught so far. Would you mind telliing me directly how i can fix my program for now so I don't have to worry about the assignment. and I can use it to help me gain a better understanding on how it works>

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    2,803
    Well it looks like you don't need to pass any variables into your functions.

    Jim

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Well I got my program to run...here's the final results. The only thing that isn't working properly is my loop isn't running the number of times I put into the command line. The thing that wasn't letting it compile was that I needed to access the particular element of the structure by using dot notation. Thank you for your help jim. Sipher not so much....

    solver.h


    / file solver.h
    // Specification of a class providing service of solving
    // quadratic equations of the form a*b^2 + b*x + c = 0

    #ifndef COUNTER_H
    #define COUNTER_H

    class solver {

    public:
    struct coeffStruct {
    double coeffs[3];
    };

    struct rootStruct {
    double roots[2];
    };

    public:
    // Constructor
    solver();

    // Function to read coefficients a, b, c, from the keyboard
    // and return them into a 3-element array (plus boolean,
    // whether a is equal zero or not)
    coeffStruct readCoeffs();

    // Function to calculate roots and return them into
    // a 2-element array (plus Boolean, whether they exist)
    rootStruct calcRoots(double coeffs[]);

    // Function to display values of the roots or an error message,
    // if the roots do not exist
    void displayRoots(double roots[], double coeffs[]);

    //
    //
    void toFile(double coeffs[], double roots[]);



    private:
    // Function to calculate the discriminant
    double discr(double coeffs[]);
    };

    #endif // COUNTER_H


    solver.cpp


    #include "solver.h"
    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <cstdlib>

    using namespace std;

    ////////////////////////////////////////////////////////////////////////
    // Default constructor
    solver::solver() {

    }

    ////////////////////////////////////////////////////////////////////////
    solver::coeffStruct solver::readCoeffs() {
    coeffStruct coeffs;

    cout << "Enter coefficients in a single line: ";
    cin >> coeffs.coeffs[0] >> coeffs.coeffs[1] >> coeffs.coeffs[2];

    return coeffs;
    }

    ////////////////////////////////////////////////////////////////////////
    solver::rootStruct solver::calcRoots(double coeffs[]) {
    rootStruct roots;

    roots.roots[0] = ((-coeffs[1]+sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
    roots.roots[1] = ((-coeffs[1]-sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));

    return roots;

    discr(coeffs);
    }

    ////////////////////////////////////////////////////////////////////////
    void solver::displayRoots(double roots[], double coeffs[]) {
    if (sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])) >= 0 && coeffs[0] != 0) {
    cout << "The solutions of a quadratic equation with coefficients: " << endl;
    cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
    cout << "are as follows " << endl;
    cout << "Root 1: " << roots[0] << endl;
    cout << "Root 2: " << roots[1] << endl;
    cout << endl;
    }

    else {
    cout << "The solution of a quadratic equation with coefficients: " << endl;
    cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
    cout << "does not exist in the real domain. " << endl;
    cout << endl;
    }
    }

    ////////////////////////////////////////////////////////////////////////
    double solver::discr(double coeffs[]) {

    double dscrResult = ((coeffs[0]*coeffs[1])-(4*coeffs[0]*coeffs[2]));

    return dscrResult;
    }

    /////////////////////////////////////////////////////////////////////////
    void solver::toFile(double coeffs[], double roots[]) {
    ofstream outp;
    outp.open("results.dat", ios::app);
    outp << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
    outp << "Root1 = " << roots[0] << " Root2 = " << roots[1] << endl;
    outp << endl;
    outp.close();
    }


    solverTest.cpp



    #include "solver.h"
    #include <iostream>

    using namespace std;

    int main(int argc, char* argv[]) {
    solver solve;
    solver::coeffStruct coeffs;
    solver::rootStruct roots;

    for (int count = 0; count < argc; count++) {

    coeffs = solve.readCoeffs();
    roots = solve.calcRoots(coeffs.coeffs);

    solve.displayRoots(roots.roots, coeffs.coeffs);
    solve.toFile(roots.roots, coeffs.coeffs);
    }
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with structures and classes
    By jdcollins in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2009, 04:07 PM
  2. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 04:03 PM
  3. Classes and structures
    By mcorn in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2002, 10:50 AM
  4. classes and structures
    By mcorn in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 04:08 PM
  5. structures and classes
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2002, 02:26 AM

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