Thread: Turbo C++, Convertion problem

  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,708
    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 <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,164
    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, 06: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, 02:16 PM