Turbo C++, Convertion problem

This is a discussion on Turbo C++, Convertion problem within the C Programming forums, part of the General Programming Boards category; I have a problem, im wanting to type in a value, but the value needs to be limited in size. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Turbo C++, Convertion problem

    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

    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();
    }
    Shizzle Ma 'C' Whizzle's

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    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;
     }
    Code:
    #include <ip.hpp>

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    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.

    Walt

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    For getting numbers, read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. I have a problem with turbo c++ v3.0
    By leeng_viper in forum C Programming
    Replies: 1
    Last Post: 12-22-2002, 01:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21