Thread: Adding a number digit by digit.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Revere, Massachusetts, United States
    Posts
    2

    Adding a number digit by digit.

    I looked at the other old post, but I could not make much from it as our codes are pretty different. I'm just trying to figure out on the adding portion and dealing with the carry. Such as if the last numbers added equal over 9 an extra position would be for 10.

    Code:
    // The FINAL Program
    // By Alex Pho
    // April 26, 2011
    
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_LENGTH_STRING 101
    
    
    int ReadingNumber(char *numberToBeRead) {
    	char c, acceptableCharacters[]="0123456789-,. \n\t", s[]=" ";
    	//char yesOrno;
    	char letter, a;
    	letter=a;
    	//int continueLoop=1;
    	int counter=0, errorCheck=0;
    	
    	while ( (c=getchar())!=EOF && !( c=='=' && counter!=0 ) && counter<MAX_LENGTH_STRING ) {
    		s[0]=c;
    		if (c=='=' && counter==0)
    			// This checks the case the luser typed '=' without a number before
    			printf("\nGet it right!\n");
    		else
    			// so, if you are here, it means you got a character
    			if (strpbrk(acceptableCharacters, s)==NULL)
    				// this checks if the character read is acceptable
    		
    				errorCheck=-1;
    		// if it isn't, I would type an error message and stop
    			else // added by Fr. Luca (I would put the next if inside of an else from the previous)
    				if (strpbrk("0123456789-.", s)!=NULL) {
    					numberToBeRead[counter++]=c;
    				}
    	}
    	numberToBeRead[counter]=0; //Tells the string to terminate
    	return 0;
    }
    
    
    
    int main (int argc, const char * argv[]) {
    	clock_t initialTicks, finalTicks;
    	FILE *pointer, *pointer2, *readerpointer, *readerpointer2, *New3, *New2;
    	char firstNumberToBeRead[101], secondNumberToBeRead[101], stringContainingFormat[101], finalAnswer[102];
    	int firstNumberLength, secondNumberLength;
    	
    	// You don't need a structure if it contains only one datum. Just use: char numbers[2][101];
    	// and here you have 101 myNumbers (which are structures), each one containing a string of 101 characters; not what you meant, I think.
    	
    	initialTicks=clock();
    	
    	
    		pointer=fopen("Number1", "w");
    		printf("Please enter a number up to 100 digits and put an '=' at the end.\n");
    		ReadingNumber(firstNumberToBeRead);
    		printf("\nGot it! Give me the second\n");
    		pointer2=fopen("Number2", "w");
    		ReadingNumber(secondNumberToBeRead); 
    		printf("\nOkay we got the second\n");
    	
    		fprintf(pointer,"%s",firstNumberToBeRead);
    		fprintf(pointer2,"%s",secondNumberToBeRead);
    		fclose(pointer);
    		fclose(pointer2);
    		printf("\nOkay I will add the numbers up for you in a jiffy!\n");
    	
    		readerpointer=fopen("Number1", "r");
    		readerpointer2=fopen("Number2", "r");
    		fscanf(readerpointer, "%s",firstNumberToBeRead); //the variable firstNumberToBeRead is related to the string format
    		fscanf(readerpointer2, "%s",secondNumberToBeRead);
    		firstNumberLength = strlen (firstNumberToBeRead);
    		printf("There are %d digits for the first number\n", firstNumberLength);
    		secondNumberLength = strlen (secondNumberToBeRead);
    		printf("There are %d digits for the second number\n", secondNumberLength);
    	//(d=firstNumber)
    	//(dd=secondNumber)
    	
    	char x;
    	char y;
    	int i=0.;
    	int sum;
    	int carry;
    	
    	if (firstNumberLength<secondNumberLength) {
    	
    	    New2=fopen("string1", "a");
    		sprintf(stringContainingFormat,"%%%ds\n",secondNumberLength);
    		fprintf(readerpointer,stringContainingFormat, secondNumberToBeRead[0]);
    	    printf(stringContainingFormat, secondNumberToBeRead[0]);
    	    fclose(readerpointer);
    		
    		for (i=0.; i<secondNumberLength; i++) {
    			
    		}
    		
    	}
    	
    	if (firstNumberLength>secondNumberLength) {
    		
    	    New3=fopen("string2", "w");
    		sprintf(stringContainingFormat,"%%%ds\n",firstNumberLength);
    		fprintf(New3,stringContainingFormat, firstNumberToBeRead[0]);
    	    printf(stringContainingFormat, firstNumberToBeRead[0]);
    	    fclose(New3);
    		
    		for (i=0.; i<firstNumberLength; i++) {
    			
    		}
    		
    	}
    	
    	sprintf(x,stringContainingFormat,firstNumberToBeRead);
    	sprintf(y,stringContainingFormat,secondNumberToBeRead);
    	
    //Math Calcuation
    	
    	
    
    
    	/*
    	finalAnswer=
    	printf("\nHere is your final answer: %s", finalAnswer);	*/
    
    	
    
    	
    		finalTicks=clock();
    	
    	return 0;
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would guess that you would probably want to turn your characters into numbers and then add them from right to left. (You'll need to do the conversion, since 4 + 5 = 9, but '4' + '5' = 'i' (assuming ASCII), and that's not at all what we want.)

    Easiest way to do that is the "subtract '0'" trick.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    int ReadingNumber(char *numberToBeRead) {
        char c, acceptableCharacters[]="0123456789-,. \n\t", s[]=" ";
        //char yesOrno;
        char letter, a;
        letter=a;
        //int continueLoop=1;
        int counter=0, errorCheck=0;
    	
        while ( (c=getchar())!=EOF && !( c=='=' && counter!=0 ) && counter<MAX_LENGTH_STRING ) {
    The getchar function returns an int, not a char. This becomes especially important when comparing against EOF as you are doing.



    #2
    Code:
    int ReadingNumber(char *numberToBeRead) {
        char c, acceptableCharacters[]="0123456789-,. \n\t", s[]=" ";
        //char yesOrno;
        char letter, a;
        letter=a;
        //int continueLoop=1;
        int counter=0, errorCheck=0;
    	
        while ( (c=getchar())!=EOF && !( c=='=' && counter!=0 ) && counter<MAX_LENGTH_STRING ) {
            s[0]=c;
            if (c=='=' && counter==0)
                // This checks the case the luser typed '=' without a number before
                printf("\nGet it right!\n");
            else
                // so, if you are here, it means you got a character
                if (strpbrk(acceptableCharacters, s)==NULL)
                    // this checks if the character read is acceptable
    		
                    errorCheck=-1;
            // if it isn't, I would type an error message and stop
                else // added by Fr. Luca (I would put the next if inside of an else from the previous)
                    if (strpbrk("0123456789-.", s)!=NULL) {
                        numberToBeRead[counter++]=c;
                    }
        }
        numberToBeRead[counter]=0; //Tells the string to terminate
        return 0;
    }
    The loop has the potential to process and assign up to MAX_LENGTH_STRING(101) characters in array indexes from 0 through MAX_LENGTH_STRING-1(100) after which counter could be equal to 101. This means there is a possibility for the array to be completely filled by the time you exit the loop (all valid index values could be used up). After the loop you then attempt to terminate the array - making it a string - by appending a null character. This null has the potential to be assigned outside the bounds of the passed in array. To account for this potential occurrence, the loop should only go up to MAX_LENGTH_STRING-1 instead of just MAX_LENGTH_STRING.


    #3
    Code:
    int main (int argc, const char * argv[]) {
        clock_t initialTicks, finalTicks;
        FILE *pointer, *pointer2, *readerpointer, *readerpointer2, *New3, *New2;
        char firstNumberToBeRead[101], secondNumberToBeRead[101], stringContainingFormat[101], finalAnswer[102];
    From a maintenance standpoint, you should be using MAX_LENGTH_STRING identifier here... MAX_LENGTH_STRING+1 for that last one. You could also have your printf in main use this value (minus-1) instead of a hardcoded 100.


    #4
    Code:
    pointer=fopen("Number1", "w");
    printf("Please enter a number up to 100 digits and put an '=' at the end.\n");
    ReadingNumber(firstNumberToBeRead);
    printf("\nGot it! Give me the second\n");
    pointer2=fopen("Number2", "w");
    
    ...
    
    readerpointer=fopen("Number1", "r");
    readerpointer2=fopen("Number2", "r");
    
    ...
    
    if (firstNumberLength<secondNumberLength) {
    	
        New2=fopen("string1", "a");
        sprintf(stringContainingFormat,"%%%ds\n",secondNumberLength);
        fprintf(readerpointer,stringContainingFormat, secondNumberToBeRead[0]);
        printf(stringContainingFormat, secondNumberToBeRead[0]);
        fclose(readerpointer);
    You never validate that your files are open before use. Furthermore, you never call fclose on the readerpointer2 or New2 file pointers. And, I'm guessing your use of readerpointer in the last fprintf/fclose pair of calls should actually be New2 instead.


    ...and at this point I'll stop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Revere, Massachusetts, United States
    Posts
    2
    thanks I'll make some changes. though I'm still not sure about those fcloses and fopens is it unnecessary then to have the fclose?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sdfx View Post
    thanks I'll make some changes. though I'm still not sure about those fcloses and fopens is it unnecessary then to have the fclose?
    Some (most) things happen in pairs...
    You open a brace, you close it
    You open a bracket you close it
    You enter a function you leave it
    You allocate memory, you free it
    You create a handle you release it
    and
    You open a file, you close it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from file - 1-digit and 2-digit numbers
    By Bonaventura in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 06:33 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM