Thread: Conversion, atoi (apologies, searches have been done)

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    24

    Conversion, atoi (apologies, searches have been done)

    Hello,

    I have searched extensively, which is how i know that there are 700+ posts on conversion from char to int. However, i still appear to be stuck. I was initially using the atoi() method. Here is my code:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    	char aDigit[4];
    	int newInt = 0;
    
    	for ( int i = 0; i < 5; i++ )
    	{
    		cout << "Input integer " << i << " :";
    		cin >> aDigit[i];
    
    		//newInt = atoi(aDigit[i]);
    		newInt = static_cast<int>(aDigit[i]); 
    		
    		if ( newInt < 1 || newInt > 9 )
    		{
    			cout << "Please input an integer between 0 and 9" << endl;
    			i--;
    		}
    	}
    	return 0;
    }
    I was having trouble using atoi() - does it only return an integer, or does it allow storage of the value too? I would also appreciate it being pointed out where i am going wrong with my type conversion.

    Many thanks in advance for your help and advice.

    Regards, global

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you are storing integer values in the array of chars. You are setting each byte to one value. So you aren't using it as a string. atoi() works on a null terminated string. Since that's not what you have, atoi isn't the right choice. you just need to do the assignment.

    additionally, your loop has 5 iterations where the char array only has 4 indices. it will overstep the array's bounds.
    Last edited by FillYourBrain; 10-13-2004 at 11:34 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    to clarify, atoi does NOT convert from char to int. It converts from a character string to an integer.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Thank you very much for your response FillYourBrain, it is very much appreciated. I did not know that aoti() had to make use of a null terminator. So how do you recommend changing my static_cast<int> so that it allows me to then perform my test?

    Regards, global

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Try this:

    Code:
    newInt = static_cast<int>(aDigit[i]) - 30;

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    I don't think thats doing much, because i am always getting "please input an integer between 0 and 9" - even if i put 34. Is there a typeof() method in C++ that allows me to output the data type.

    Many thanks for your help.

    Regards, global

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    first off.... you're only setting one digit. That means a 0-9 restriction makes sense right? If you want to set more digits in the string, you need to "cin" the entire aDigit and not just aDigit[i].

    Also, if you want a number you can just use that in cin directly instead of converting.

    cin >> newInt;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Sorry, i should have mentioned that i want to be able to handle the user entering a chracter instead of an integer. aDigit[i] would get '56' or '34' as well as '6' or '9' - it's still 1 integer that can be stored in an array, or am i just wrong?

    Thank you for your continuing help.

    Regards, global

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    char intAsString[5];
    char ch;
    for(int i = 0; i < 4; ++i)
    {
       cout << "input digit #  " << i << endl;
       cin >> ch;
       while(ch < '0' || ch > '9')
       {
    	  cout << "input error" << endl;
    	  cout << "input digit # " << i << endl;
    	  cin >> ch;
       }
       intAsString[i] = ch;
    }
    intAsString[4] = '\0';
    int result = atoi(intAsString);
    cout << "the four digit number you entered was" << result;

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    ok, i see what your doing, but it does not get me any further, from just using the static conversion. If you enter a 2 digit number with the code above, it will fail. I really do appreciate your help, and will look at your code and try and figure something out, perhaps a mixture of your code and some other code.

    I really am a bit stuck.

    Thanks again!

    Regards, global

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What do you want to achieve, anyway? Do you want to read a number or do you want to read a digit?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    I would like to read 5 numbers into an array (between 0 and 9) and check that the user has only entered a number between nought and nine, then print them out backwards.

    Regards, global.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    cout << "the digits you entered backwards are" << endl;
    for(i = 4; i >= 0; ++i)
      cout << intAsString[i];

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    edit: never mind. Handled by others.
    Last edited by FillYourBrain; 10-13-2004 at 02:19 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by elad
    Code:
    cout << "the digits you entered backwards are" << endl;
    for(i = 4; i >= 0; ++i)
      cout << intAsString[i];

    you gave him a 64,000 loop

    change ++i to --i

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM