Thread: I'm so confused...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    I'm so confused...

    Alright, i'm going to try to explain this assingment the best that i can... we have to write a program that will scan in #'s of large integers into a link list and then make it so the user can add or subtract those numbers with another set of numbers in a link list... I'm gettin errors in the addition when it adds for some reason it isn't adding correctly.. the last number it prints twice.. and if one # is 1234 and the other is 12 it will give me 46 only the last 2 numbers will be added... i tried adding leading zeros a while ago to fix that error but it never worked out correctly... there are flaws all throughout this program and i was going to do it without asking for help but it's due in a day and i have no clue how to fix it. Any help that you can provide would be great. Thanks -John

    here is the integer.h file...
    Code:
    typedef struct integerNode{
          int integer;
     struct integerNode *next;
       };
    typedef struct integerNode1{
    	  int integer1;
          struct integerNode1 *next;
       };
    typedef struct integerNode2{
    		int integer2;
    		struct integerNode2 *next;
       };
    
    int AddTwoIntegers(int, int);
    int ReadInt();
    void PrintInteger(int);
    int compare(int, int);
    int SubtractIntegers(int, int);
    int RecycleInteger(int);
    Here's the main program's code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "integer.h"
    
    #ifndef _bigint_h
    #define _bigint_h
    
    int x;
    int n1;
    int n2;
    int n;
    int cnt;
    int cnt1;
    int cnt2;
    int myint;
    int sum;
    int rollover;
    int flag;
    int result;
    int temp;
    
    	struct integerNode *head = NULL;
    	struct integerNode *curr;
    	struct integerNode1 *head1 = NULL;
    	struct integerNode1 *curr1;
    	struct integerNode2 *head2 = NULL;
    	struct integerNode2 *curr2;
    
    void main()
    	{
    		printf("\n\n1. Add\n2. Subtraction\n3. Quit\n\nEnter Choice: ");
    		scanf("%d", &x);
    		switch(x){
    		case 1:
    			myint = ReadInt();
    			cnt = 0;
    			for(x=0;x < myint; x++){
    			n1 = curr -> integer;
    			n2 = curr1 -> integer1;
    			result = AddTwoIntegers(n1, n2);
    			}
    			n = result;
    			PrintInteger(n);			
    			(int)head = RecycleInteger((int)head);
    			main();
    		case 2:
    			myint = ReadInt();
    			for(x=0;x < myint; x++){
    			n1 = curr -> integer;
    			n2 = curr1 -> integer1;
    			flag = compare(n1, n2);
    			temp=flag+temp;
    			}
    			if (temp<0){
    			for(x=0;x < myint; x++){
    			n1 = curr -> integer;
    			n2 = curr1 -> integer1;
    			result = SubtractIntegers(n2, n1);
    			}}
    			else {
    			for(x=0;x < myint; x++){
    			n1 = curr -> integer;
    			n2 = curr1 -> integer1;
    			result = SubtractIntegers(n1, n2);
    			}
    			PrintInteger(n);
    			(int)head = RecycleInteger((int)head);
    			main();
    		case 3:
    			exit (0);
    		default:
    			exit (0);
    		}
    	}
    }
    
    	int ReadInt()
    		{
    		cnt1=0;
    		while(myint = getchar()){
    		if (isdigit(myint) == 0){break;}}
            
    		printf("Enter the first number: ");
    		 while(myint = getchar()){
    		 if (isdigit(myint) == 0){break;}
    		 curr = malloc(sizeof(struct integerNode));
             curr -> integer = myint-48;
             curr -> next = head;
             head = curr;
    		 cnt1++;
    		 }
    		
    		 cnt2=0;
    		 printf("Enter the second number: ");
    	     while(myint = getchar()){
    		 if (isdigit(myint) == 0){break;}
    		 curr1 = malloc(sizeof(struct integerNode1));
             curr1 -> integer1 = myint-48;
             curr1 -> next = head1;
             head1 = curr1;
    		 cnt2++;
    				}
    		 if(cnt1<cnt2){return cnt1;}
    		 if(cnt2<cnt1){return cnt2;}
    		else return cnt1;
    		}
    
    
    	int AddTwoIntegers(n1, n2)
    		{			
    			n1 = n1 + rollover;
    			sum = n1 + n2;
    			rollover = 0;
    			if (sum>9){sum = sum - 10; rollover = 1;}	
    			curr2 = malloc(sizeof(struct integerNode2));
    			curr2 -> integer2 = sum;
    			curr2 -> next = head2;
    			head2 = curr2;
    			cnt2++;
           		curr1 = head1;
    			head1 = head1 -> next;
    			curr = head;
    			head = head -> next;
    
    		return cnt2;
    		}
    	
    	void PrintInteger(n)
    		{
    		int i;
    		for(i=0;i<n-3;i++){
    			printf("%d", curr2->integer2);
    			curr2 = curr2 -> next;
    						}
    		}
    	
    	int compare(n1, n2)
    		{
    		if(n1==n2){return 0;}
    		if(n1>n2){return 1;}
    		if(n2>n1){return -1;}
    		return 0;
    		}
    	
    	int SubtractIntegers(n1, n2)
    		{
    			sum = n1 - n2;
    			curr2 = malloc(sizeof(struct integerNode2));
    			curr2 -> integer2 = sum;
    			curr2 -> next = head2;
    			head2 = curr2;
    			cnt2++;
    			curr1 = head1;
    			head1 = head1 -> next;
    			curr = head;
    			head = head -> next;
    		return 0;
    		}
    	
    	int RecycleInteger(n)
    		{
    		while(head != NULL)
    		{         
    		RecycleInteger((int)head->next);
    		free(head);       
    		}
    		return 0;
    		}
    #endif

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Just one quick comment, the function main() should not be recursed i.e.

    main(){ main(){ main()...etc.} }

    Recursion is valid solution but not in this case and defiantly not with main().

    try the following structure, this will not fix your posted problem:
    Code:
    int main()
    {
          bool bQuit = FALSE; 
          do
          {
                printf("\n\n1. Add\n2. Subtraction\n3. Quit\n\nEnter Choice: ");
                scanf("%d", &x);
                switch(x)
                {
                      case 1: 
                           break;
                      case 2:	
                           break;
                      case 3:
                      default:
                           bQuit = TRUE; 
                           break;
                }
          }while( bQuit != TRUE );
           return 0;	
    }
    Last edited by Scarlet7; 03-13-2003 at 03:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 05:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM