### Code not working!

For this project, you will simulate this conversion. Your program will ask the user to enter a 4 digit numerical value. As the programmer, you will input the value as a string. You will devise an algorithm to convert the string of characters (string of digits) to a single integer, stored in a single integer value. At the end of this algorithm, you will print the addition (using the + operator) of the input string to itself, and the addition of the converted integer to itself. For example, if the user were to input 9275, the outputs would be 18550 and 92759275.Your program should generate appropriate error messages if the user enters: (1) less than 4 digits, (2) more than 4 digits, or (3) any non-numeric digits.

I came up with the code below and there are a couple of errors, can someone help me out? TIA

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include int main() { char num[4]; int dec = 0, i, j, len; printf("Enter a String of length 4: "); gets(num); len=strlen(num); for(int k=0;k57){ printf("invalid input"); exit0; } } if(len<4 || len>4){ printf("invalid input"); } else{ for(i=0; i
Hello marygamess,

On line 7 you have created an array to hold 4 numbers, but you forgot about space for the "\0" to end the string. Also you should initialize the array before yo use it.

On line 26 you are trying to add "num" to "num", but "num" only hase space for four numbers and this will not work. As yo will see I created "dest" to handle this.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include #include #pragma warning(disable : 4996) int main() { char num[5] = { 0 }; char dest[10] = { 0 }; int dec = 0, i = 0, j = 0, len = 0; printf("Enter a String of length 4: "); gets(num); len = strlen(num); for (int k = 0; k57) { printf("invalid input"); exit (1); } } if (len<4 || len>4) { printf("invalid input"); } else { for (i = 0; i

This works for the first number of your example, but not the second number to be output. This is not perfect, but should give you a better start.

Hope that helps,

Andy
Hello marygamess,

Do you need a C program or could you use a C++ program?

Andy
Hello marygamess,

After running the program for awhile I noticed this:

 ``123456789`` ``````for (int k = 0; k57) { printf("invalid input"); // <--- Could use a pause here to allow the user to read the display before exit. exit (1); } }``````

Do you see any problem between the loop iterator and the subscript?

Also this would work better: `if (num[i] < '0' || num[i] > '9')`. This way you do not nee to remember or look up the decimal value of zero - nine.

Lines 26 - 29 should come after the input to check the length. I used an if/else if to print a different message depending on the length.

The for loop at line 23 could easily be achieved with`dec = atoi(num);`.

One last point to keep in mind. Since you want to do a "strcat" of "num" into "num", "num" will need to be a multiple of 4 + 1 for the '\0' at the end and "num" will need to be large enough to hold a number greater than 4 when you first input a number.

Hope that helps,

Andy
Topic archived. No new replies allowed.