... You really have a long way to go! Try getting and reading a C++ book or pdf. This one is very good.
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 ...
... 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...
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?
JimCode:struct coeffStruct { double coeffs[3]; bool coeffsFlag; // is a equal zero? };
I'm using them to solve a quadratic equation.
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
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>
Well it looks like you don't need to pass any variables into your functions.
Jim
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);
}
}