How do I write a do...while loop in a class.

How do i fix this error? I understand it needs a function before the command but i'm not doing this in my int main(), so what else can go before. LINE 26

include\Polynomial.h|27|error: expected unqualified-id before 'do'|
include\Polynomial.h|45|error: expected unqualified-id before 'while'|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial : public Term
{
    public:
        Polynomial();

         ~Polynomial();

          // function to build a polynomial

        list <Term> myList;
        list <Term>::iterator(it);
        int request;


        do {
                // create a term object
                Term *terms = new Term();


                // prompt the user for coefficient and power values and set them into the term object





                // use an iterator to add the term object to the list...push_front

                // insert the term object into the list




        } while (()



    private:
        list <Term> poly; // the list stores the attributes from Term class
                          // and poly represents the copy n paste shell for
                          // ex poly1, poly2, etc.. and in "poly" it asks for
                          // the coefficient and power.


};

#endif // POLYNOMIAL_H
Last edited on
Your loop doesn't appear to be inside any function which is required.
So I have used do.. while in my int main() functions but what type of function would I add to this class? It has a purpose of adding a terms to the Term class. By that I mean I can add, remove or multiply polynomials.


include\Polynomial.h|46|error: expected unqualified-id before 'while'|
What jlb said.

What you appear to be doing here is declaring the class Polynomial in a header file, your .h file. You'll need another file (in your case something like Polynomial.cpp) which will contain any definitions of the declarations you've made in the .h header file. What you're trying to do in your code is writing executable code outside of a function definition.

Inside your Polynomial .h, declare a public function <return-Type> AddNewTerm(<parameters>), for example. You'll have to figure out the return-Type and parameters. Put your do-while code in the function's definition contained in the .cpp.

Suggest you research how to compile a C++ program and the use of the #include preprocessor directive before you go much further with the development of your class.
Last edited on
In one of your previous posts you said.... Theres the answer your looking for


I've gotten this far, but now i'm confused on what to do next. Do I write more in the Get and Set functions? and then for the multiplying do poly3 = poly1 * poly2 ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();
        Polynomial(int poly); // creates the polynomial.

         ~Polynomial();

         int Getpoly();   // gets the poly
         void Setpoly();  // sets the poly
         void display();  // displays the poly




    private:
        list <Term> poly;

};

#endif // POLYNOMIAL_H 


Thanks! I think I kind of got the hang of it, i tried putting the function into my main to see if it'd prompt the user correctly and for some reason the function does nothing, can someone explain why?

.h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial : public Term
{
    public:
        Polynomial();

         ~Polynomial();

          // function to build a polynomial

        list <Term> myList;
        list <Term>::iterator(it);
        int answer;
        float coefficient;
        int power;

            void AddNewTerm()
            {

            cout << "Enter polynomial terms" << endl << endl;
          do {
                // create a polynomial
                Term *newTerm = new Term();


                // get and set values
               cout << "Enter coefficient: " << endl;
               cin >> coefficient;
               cout << "Enter power: " << endl;
               cin >> power;
               newTerm->Setcoefficient(m_coefficient);
               newTerm->Setpower(m_power);


                // find location for new term
                poly.insert(it, *newTerm);

                // insert the term object into the list.
                cout << "Enter more terms (y/n)?";
                cin >> answer;




             } while ((answer == 'y') || (answer == 'Y'));


           }



    private:
        list <Term> poly; // the list stores the attributes from Term class
                          // and poly represents the copy n paste shell for
                          // ex poly1, poly2, etc.. and in "poly" it asks for
                          // the coefficient and power.


};

#endif // POLYNOMIAL_H


MAIN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <list>
#include <string>
#include "Term.h"
#include "Polynomial.h"

using namespace std;

int main()
{
      // create poly variable
      Term poly1;

      // bring in create a poly function
      void AddNewTerm();







    return 0;
}
Last edited on
Topic archived. No new replies allowed.