I have a problem, im wanting to type in a value, but the value needs to be limited in size.

the only way i have found to limit the size of the string you enter is by useing "keypress = getch()" in a loop etc.... but this needs a char array (string) in order for it to work.

im wanting to convert this string to an integer, so i enter the value: 1234, the program will convert it and display it in the integer as the number 1 thousand 2 hundrad and thirty four.

if this makes sence :confused:

i have had a g at doing this, but i still have no look, there must be a better way or even a way that accually works:

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
int test1;
char *test2
char test3;

test3 = getch(); //Get a character, the
//user enters a number char

strcat(test2,test3); //Concatinate the number to
//the end of the string

test1 = atoi(test2); //Conver string to integer

printf("A%dA",test1);

printf("\n\n\n\n\n\n\n\nPress any key to continue....");
getch();
}

05-08-2003

Sebastiani

Well the first problem is that the code won't compile. Second, test2 is a pointer, and pointers MUST point to some piece of memory. Finally, main() returns an int, not void.

You can limit the breadth of an array by NULL terminating it at the desired offset.

Code:

int main()
{
int num;

int limit = 4;

char buffer[256];

fgets(buffer, 256, stdin);

buffer[limit] = '\0';

num= atoi(buffer);

printf("A%dA",num);

system("pause");

return EXIT_SUCCESS;
}

05-08-2003

Hammer

>>display it in the integer as the number 1 thousand 2 hundrad and thirty four.
There were some other posts doing a similar sort of thing just recently. Try a search for them and see what you can find :)

05-08-2003

WaltP

Also, you have test3 defined as a char, so you cannot use

strcat(test2,test3);

This functions only work on strings.

Your logic is
-- input one character
-- add it to a string
-- convert string to int

You need to have a loop to enter more characters if you wish to 'limit' the size. As it is, you *are* limited -- to one char.