Function not running yet runtime error???

This is a discussion on Function not running yet runtime error??? within the C++ Programming forums, part of the General Programming Boards category; I am trying to evaluate a polynomial that is stored in a linked list. I ask the user to input ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    Function not running yet runtime error???

    I am trying to evaluate a polynomial that is stored in a linked list. I ask the user to input a string, the string gets converted to a linked list polynomial, then when I try to evaluate it the program gets a run time error and closes. The weird thing is "checkpoint 1" shows, but "checkpoint 2" does not. Here is the piece(s) of code I am working with, and the evaluate function. My linked list is fine, and there is nothing wrong with it. I have tried

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ctype.h>
    #include "class.h"
    
    double EvaluatePoly(POLY input, int num);
    
    //pre-condtion: the input string must have all coefficients
    //including 1. does not need to have all powers with 0 coefficients
    //DOES NEED powers of zero if only a coefficient!
    void TranslateStrToLinkedList(char* str, POLY &input);
    
    int main()
    {
        char* input;
        int from, to;
        int answer = 0;
        POLY poly;
    
        cout<<"Enter a polynomial like t^3+2t^1+9t^0: ";
        cin>> input;
    
        TranslateStrToLinkedList(input, poly);
        
        poly.print();
        
        cout<<endl<<"Enter domain for t: ";
        cin>> from >> to;
        while(from <= to){
            cout<<"checkpoint 1\n";
            answer = answer + EvaluatePoly(poly,from);
            cout<< answer <<endl;
            from++;
        }
    }
    
    //evaluates a linked list with polynomials
    double EvaluatePoly(POLY input, int num)
    {
        cout<<"checkpoint 2"; //NOT SHOWN!!! >.<
        double ans = 0; //prevents problems if input is empty
        int hi_degree = 0; //prevents problems if input is empty
        cout<<"checkpoint 3"<<endl;
        hi_degree = input.degree();
    
        while(input.isLast(hi_degree) != 0){
            ans = ans + input.coefficient(hi_degree)*(num^hi_degree);
            cout<<ans<<endl;
            hi_degree--;
        }
        return ans;
    }
    
    void TranslateStrToLinkedList(char* str, POLY &input)
    {
        char ch, *chstr;
        double coef = 0;
        int pow = 0, n = 0;
        while( n < strlen(str)-1){
            ch = str[n];
            
            if(isdigit(ch) || ch == '-' || ch == '+'){
                if(ch == '-'){
                    ch = str[n+1];
                    chstr = &ch;
                    coef = -1*atof(chstr);
                    chstr = &str[n+4];
                    pow = atoi(chstr);
                    cout<<endl<<coef<<" 1 "<<pow<<endl;
                    n++; n++; n++; n++; n++; //move 5 places
                    input.append(coef,pow);
                }
                else if(ch == '+'){
                    ch = str[n+1];
                    chstr = &ch;
                    coef = atof(chstr);
                    chstr = &str[n+4];
                    pow = atoi(chstr);
                    cout<<endl<<coef<<" 2 "<<pow<<endl;
                    n++; n++; n++; n++; n++; //move 5 places
                    input.append(coef,pow);
                }
                else if(isdigit(ch)){
                    chstr = &ch;
                    coef = atof(chstr);
                    chstr = &str[n+3];
                    pow = atoi(chstr);
                    cout<<endl<<coef<<" 3 "<<pow<<endl;
                    n++; n++; n++; n++; //move 4 places
                    input.append(coef,pow);
                }
            }
        }
        return;    
    }
    Here is the way it looks when ran. The "3 3 2" means coefficient is 3, it went to the 3rd if statement in TranslateStrToLinkedList(), and has a power of 2. This was for my assurance it was working right.
    Code:
    Enter a polynomial like t^3+2t^1+9t^0: 3x^2
    
    3 3 2
    3x^2
    Enter domain for t: 2 4
    checkpoint 1

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not solving your problem as such, but:
    Code:
    num^hi_degree
    Are you sure you want to xor these values?

    Also, what is the code for the functions marked in red below::
    Code:
        hi_degree = input.degree();
    
        while(input.isLast(hi_degree) != 0){
            ans = ans + input.coefficient(hi_degree)*(num^hi_degree);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    degree() returns the highest degree, or the first link in the linked list.
    coefficient() returns the coefficient of a chosen degree. If the degree doesn't exist it returns 0.

    The problem I don't understand is checkpoint 1 shows, but checkpoint 2 does not and all it should have done is go directly to the top of the function, and not even declare anything. I changed the function to make more sense, I wrote this late last night and wasn't thinking clearly with the ^

    If I comment out where I use EvaluatePoly() in main() then there is no run time error, so I know it has to do with the function.

    Code:
    //evaluates a linked list with polynomials
    double EvaluatePoly(POLY input, int num)
    {
        cout<<"checkpoint 2"; //NOT SHOWN!!! >.<
        double ans = 0; //prevents problems if input is empty
        int hi_degree = 0; //prevents problems if input is empty
        cout<<"checkpoint 3"<<endl;
        hi_degree = input.degree();
    
        while(input.isLast(hi_degree) != 0){
            ans = ans + input.coefficient(hi_degree)*pow(num,hi_degree); //changed this line
            cout<<ans<<endl;
            hi_degree--;
        }
        return ans;
    }
    EDIT: Now I have a new problem, I changed the arguments of EvaluatePoly() from POLY input to POLY& input and now it will run without a problem. However, now when I try to get the highest degree it always returns zero, but yet the power is not zero when manually checked. If I can't figure that out I will be back but for now thank you.
    Last edited by scwizzo; 10-29-2008 at 11:10 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try adding a newline or std::endl to yout checkpoint2 output - it's likely that it's just being buffered until the next newline (and it crashes before the next newline is output) - most likely because something is wrong in the input.xxx functions that I mentioned before. Does EvaluatePoly() work correctly if you remove (substitute for constonts) the input.degree() and/or input.coefficient?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I got it working now. Everything got traced back to the way I was using TranslateStrToLinkedList(). I forgot when I made the linked list that I made the assumption the first coefficient and power need to be entered using some member functions called changecoefficient() and changepower(), and every term after that is made using append(). I added another function to the end of TranslateStrToLinkedList() called SetupNormal() where it sets up my polynomial correctly in the linked list. It evaluates everything correctly now. Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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