Search code examples
c++functionmethodsheaderprogram-entry-point

C++ Method wont work


So I created a method in order to start the process for adding, multiplying and etc polynomials, however, when attemepting to run start(), the compiler runs, however the box remains blank, even though it shouldn't. Not exactly sure what I'm doing wrong.

Any ideas?

Here is my code.

Here is my header

#ifndef _POLY_GUARD 
#define _POLY_GUARD 

#include <iostream>
using namespace std;

class Polynomial
{
public:
    Polynomial(int coef, int exp);
    void start();   
    friend ostream & operator << (ostream &out, const vector<int> &c);
    friend istream & operator >> (istream &in, const Polynomial &c);  
    void addPolynomials();    
    void multiplyPolynomials();
    void evaluatePolynomial();
    int findCoefficient();
    int findLeadingExponent();    
};

#endif

This is the source code.

#include "Polynomial.h"
#include <utility>
#include <iostream>
#include <vector>
#include <string>
using namespace std;  

void Polynomial::start()
{
    int choice;

    std::cout << "What do you wish to do?" << std::endl;
    std::cout << "1. Add two polynomials" << std::endl;
    std::cout << "2. Multiply two polynomials" << std::endl;
    std::cout << "3. Evaluate one polynomial at a given value" << std::endl;
    std::cout << "4. Find Coefficent for a given polynomial and given exponent" << std::endl;
    std::cout << "5. Find the leading exponent for a given polynomial" << std::endl;
    std::cout << "6. Exit " << std::endl;

    std::cin >> choice;

    if (choice < 1 || choice > 6)
    {
        do
        {
            std::cout << "Invalid entry: please reenter choice" << std::endl;
            std::cin >> choice;   
        } while (choice < 1 || choice > 6);
    }

    if (choice == 1)
    {       
    }
 }

And finally, here is my main

#include "Polynomial.h"
#include <string>
#include <vector>
#include <utility>

int main()
{
    Polynomial start();
    system("pause");
}

Solution

  • Read the comments above as they are just as helpful as the example below.

    So you have a class Polynomial from which you can create (instantiate) objects of that particular type.

    class Polynomial {
    public:
        /// default constructor
        Polynomial() = default;
    
        /// constructor with your coef and exp parameters
        /// when invoked it will use its arguments to initialize
        /// the data members coef and exp.
        Polynomial(int coef, int exp)
            : coef(coef)
            , exp(exp){};
    
        /// your member function start()
        void start();
    
    private:
        /// your private data members that 
        /// are initialized upon construction
        /// when calling the appropriate constructor
        int coef;
        int exp;
    };
    

    In you main function, as others have mentioned you can construct an object of type Polynomial, call it app or whatever:

    int main()
    {
        /// app is your object of type Polynomial
        /// its coef and exp are initialized
        /// using your arguments 4 and 5 respectively.
        Polynomial app(4, 5);
        /// now you can call you member function
        app.start();
    
        return 0;
    }