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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;k<len;k++)
    {
        if(num[i]<48 || num[i]>57){
            printf("invalid input");
            exit0;
        }
        }
        if(len<4 || len>4){
            printf("invalid input");
        }
        else{
            for(i=0; i<len; i++){
                dec = dec * 10 + ( num[i] - '0');
            }
            strcat(num,num);
            
            dec+=dec;
            printf("%d/n",dec);
            printf("%s\n",num);
        }
        return 0;
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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; k<len; k++)
	{
		if (num[i]<48 || num[i]>57)
		{
			printf("invalid input");
			exit (1);
		}
	}

	if (len<4 || len>4)
	{
		printf("invalid input");
	}
	else
	{
		for (i = 0; i<len; i++)
		{
			dec = dec * 10 + (num[i] - '0');
		}

		strcat(dest, num);

		dec += dec;
		printf("%d\n", dec);
		printf("%s\n", num);
	}

	return 0;
}


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:

1
2
3
4
5
6
7
8
9
for (int k = 0; k<len; k++)
{
	if (num[i]<48 || num[i]>57)
	{
	    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 withdec = 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
Registered users can post here. Sign in or register to post.