# Thread: Adding a number digit by digit.

1. ## 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

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]=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;
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");
printf("\nGot it! Give me the second\n");
pointer2=fopen("Number2", "w");
printf("\nOkay we got the second\n");

fclose(pointer);
fclose(pointer2);
printf("\nOkay I will add the numbers up for you in a jiffy!\n");

fscanf(readerpointer, "%s",firstNumberToBeRead); //the variable firstNumberToBeRead is related to the string format
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);

for (i=0.; i<secondNumberLength; i++) {

}

}

if (firstNumberLength>secondNumberLength) {

New3=fopen("string2", "w");
sprintf(stringContainingFormat,"%%%ds\n",firstNumberLength);
fclose(New3);

for (i=0.; i<firstNumberLength; i++) {

}

}

//Math Calcuation

/*

finalTicks=clock();

return 0;
}```

2. 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. #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]=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;
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");
printf("\nGot it! Give me the second\n");
pointer2=fopen("Number2", "w");

...

...

if (firstNumberLength<secondNumberLength) {

New2=fopen("string1", "a");
sprintf(stringContainingFormat,"%%%ds\n",secondNumberLength);
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.

4. 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. Originally Posted by sdfx
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