floating point execption

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SD14
    New Member
    • Apr 2010
    • 4

    floating point execption

    for some reason in my program when i try and add a number to my current number i get a floating point exception now i have narrowed down were it stops working to line 398 but i dont know as to why it is stopping right there. Here is my code any help would be great.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    using namespace std;
    
    #undef NULL //un defines anything from a class that has the same name as one of your constants
    
    typedef int element;
    
    const int NULL = 0;
    const char sentinel = '#';
    
    class listnode{
    
    	public:
    
    		element data;
    
    		listnode* next;
    
    	};
    
    
    class Llist{
    
    	private:
    
    		listnode* head;
    
    		listnode* tail;
    		char vignum;
    
    	public:
    
    		void read();
    
    		void print();
    		void clean();
    		void ReadBackward();
    		char read_element();
    		void Inserthead(element);
    		void inserttail(char);
    		element DeleteHead();
    		void steal(Llist&);
    		void append(Llist&);
    		void duplicate(Llist&);
    		void reverse();
    		void Mergeordered(Llist&, Llist&);
    		void Printinreverse();
    		void Mainmenu();
    		void Helpmenu();
    		void addvigesimalnum();
    		void newvigesimalnum();
    		void multiplyvigesimalnum();
    		int digitconvertor(char);
    		char convertback(int);
    		void Multiadd(int);
    		void difadd(Llist & second, Llist & combo, Llist & copy);
    
    		Llist();
    		~Llist();
    
    
    
    
    
    	};
    
    void Llist::difadd(Llist & second, Llist & combo, Llist & copy){
    	char cnum1;
    	char cnum2;
    	char cnumber;
    	element num1;
    	element num2;
    	element number;
    	element remainder;
    	listnode * temp1;
    
    	listnode * temp2;
    
    	temp1 = copy.head;
    
    	temp2 = second.head;
    
    	combo.head = NULL;
    
    	
    	while((temp1 != NULL)&&(temp2 != NULL)){
    		cnum1 = temp1 -> data;
    		cnum2 = temp2 -> data;
    
    		cnum1 = DeleteHead();
    		cnum2 = second.DeleteHead();
    
    		num1 = digitconvertor(cnum1);
    		num2 = digitconvertor(cnum2);
    
    		number = (num1 % num2) + remainder;    // Line 398
    		remainder = (num1 + num2) / 20;
    
    		cnumber = convertback(number);
    		
    		combo.inserttail(cnumber);
    
    		temp1 = temp1 -> next;
    		temp2 = temp2 -> next;	
    		
    		cout << "here 1" << endl;
    		combo.print();
    	}
    }
    Last edited by Banfa; Apr 9 '10, 10:58 AM. Reason: Removed excess code
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    L 398 says that 'number' is to be assigned the value of the remainder of num1 % num2 + remainder; but i cant see where the second remainder comes from.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The first time through the loop the variable remainder is not initialised to any value and is used on line 398 before it is set on line 399.

      Using an uninitialise automatic variable is undefined behaviour.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Line 398 evaluates num1 % num2.
        Are you sure num2 isn't zero?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Line 398 evaluates num1 % num2.
          Are you sure num2 isn't zero?

          Wait a minute! All the variables on line 398 are integers. I don't see any floating point operations, so I don't know why you're getting a floating point exception.

          Comment

          Working...