I am having trouble trying to get my code to work. If I pick selection 1 or 2 the cout statements execute but it does not wait for the cin and the program closes.

#include <iostream>
#include <cstdlib>
#include <string>
#include "poly.h"

using namespace std;

void enterPoly1(Polynomial&);
void enterPoly2(Polynomial&);
void addPoly(Polynomial&, Polynomial&, Polynomial&);
void subPoly(Polynomial&, Polynomial&, Polynomial&);

int main()
{
    char choice;

    Polynomial p1;
    Polynomial p2;
    Polynomial p3;

    do {

        cout << "Welcome to the Polynomial Program!!" << endl;
        cout << "In this program you can enter two polynomials," << endl;
        cout << "and then choose to add or subtract thos polynomials" << endl;
        cout << "PLEASE NOTE: you must use proper polynomial syntax when entering." << endl;
        cout << "This format is (Num)x^2 + or - (Num)x + or - (Num)" << endl;
        cout << "Please use the following choices to begin (enter the number of your choice):" << endl;
        cout << "1. Enter Polynomial 1" << endl;
        cout << "2. Enter Polynomial 2" << endl;
        cout << "3. Choose Addition" << endl;
        cout << "4. Choose Subtraction" << endl;
        cout << "5. Quit" << endl;
        cin >> choice;
        switch (choice)
        {
            case '1':
                enterPoly1(p1);

                break;

            case '2':
                enterPoly2(p2);

                break;
            case '3':
                subPoly(p1, p2, p3);

                break;
            case '4':
                subPoly(p1, p2, p3);
                break;
            case '5':
                cout << "Thanks for playing - GOODBYE!" << endl;
                break;
        }
    } while (choice != '5');





    system ("PAUSE");
    return 0;
}


void enterPoly1(Polynomial& p1) {
    cout << "PLEASE NOTE: you must use proper polynomial syntax when entering." << endl;
    cout << "This format is (Num)x^2 + or - (Num)x + or - (Num)" << endl;
    cout << "Also, use lower case x variables!" << endl;
    cout << "Enter Polynomial 1: " << endl;
    cin >> p1;
}

void enterPoly2(Polynomial& p2) {
    cout << "PLEASE NOTE: you must use proper polynomial syntax when entering." << endl;
    cout << "This format is (Num)x^2 + or - (Num)x + or - (Num)" << endl;
    cout << "Also, use lower case x variables!" << endl;
    cout << "Enter Polynomial 2: " << endl;
    cin >> p2;
}

Recommended Answers

All 4 Replies

how does the >> operator work for your Polynomial class? I ask because if you use getline inside the Polynomial class then cin >> will be throwing you off. What happens if you put cin.get() between lines 34 and 35?

istream& operator>> (istream& inp, Polynomial& poly) {

    bool isTrue = true;
    string temp;
    string tempCoef;

    while(isTrue) {
        getline(inp, temp);

        unsigned pos = temp.find("x^2");
        tempCoef = temp.substr(0, pos);
        poly.coef[0] = atoi(tempCoef.c_str());

        temp.erase(0, pos+3);

        pos = temp.find("x");
        tempCoef = temp.substr(0, pos);
        poly.coef[1] = atoi(tempCoef.c_str());

        temp.erase(0, pos+1);
        tempCoef = temp;
        poly.coef[2] = atoi(tempCoef.c_str());
        isTrue = false;
    }

    return inp;

}

Putting:
cin.get(choice);
results in an endless loop

I think what Nathan Oliver was trying to say is that cin>>X; and getline(X,Y) do not mix very well. This is because of how those functions modify the internal pointer for cin. Putting cin.get() (without arguments) can sometimes rectify the problem by forcing cin to "catch up" to getline. Basically lets say you type "The quick brown fox jumps over the lazy dog." into the console. STD_IN (cin/getline) gets filled with: "The quick brown fox jumps over the lazy dog.\n". cin reads by word so cin>> will get you these: "The","quick","brown","fox","jumps","over","the","lazy","dog." one at a time (storing its current location internally). By contrast getline will just grab everything before the \n instead. As such lets say you enter the above phrase. getline() should fetch you what you would expect, but then cin>> would probably (I do not have a compiler on this computer so I can't check) give you "The". The point is that getline() does not act like cin>>x until endline. They are seperate mechanisms working on the same stream. It leads to complications. Anyways, cin.get() where NathanOliver suggested could fix your problem.

I seem to have fixed the problem by modifying my functions as follows:

void enterPoly1(Polynomial& p1) {
    system("cls");
    cout << "PLEASE NOTE: you must use proper polynomial syntax when entering." << endl;
    cout << "This format is (Num)x^2 + or - (Num)x + or - (Num)" << endl;
    cout << "Also, use lower case x variables!" << endl;
    cout << "Enter Polynomial 1: " << endl;
    cin.ignore();  //adding this line seems to have fixed it
    cin >> p1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.