Thread: Reading an integer array from console??

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    29

    Reading an integer array from console?? [SOLVED]

    Sorry for being a pain but I am google eyed from searching the net with no success..

    Basically this is my program, working fine.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int Number[14]={1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    int i, Calc;
    
    /*Here some fancy calculation happens to
    the integer array digits which results in a
    single integer value Calc */
    
    printf("%d", Calc);
    
    return 0;
    }

    Now this all works fine, exactly as expected but what I want to do is promt the user to input the 14 digit number and fill the integer aray using their input.

    What I have tried so far:

    Code:
    scan f("%d %d %d %d.....%d",  &Number[0], &Number[1], &Number[2]....) //etc
    Basically scan for 14 integers and store them into each array segment.

    The code compiles but the program hangs after inputting any numbers.

    I have also tried:

    Code:
    char Number_In[15];
    int Number_Out[14];
    int i;
    
    for (i=0;i<14;i++)
    {
    scanf("%s",Number_In);
    Number_Out[i] = atoi(Number_In[i]);
    }
    Basically inputting a string and using atoi to convert to integer. This however produces the wrong value of Calc digit, and printing the value of Number_Out shows that atoi is not working as expected..

    Surely there must be a simple way to do this that I have missed?

    Any help greatly receieved,
    Cheers.
    D.
    Last edited by pico; 12-23-2008 at 03:19 AM. Reason: Case closed

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure that atoi() works as expected, just that feeding it the address that is the character of the string you read is probably not the right thing. In your loop, you probably should read the string, and then do atoi() on number_in, not number_in[i] - which is a single character, and it is used as an address, so you get a very low memory address as the input to atoi - you are presumably doing this with something like turbo C or an embedded compiler, or your complaint would have been that the program crashes.

    Your method that reads 14 integers should also work - however, if you enter less than 14 integers, it will wait for more to be entered, until it has got 14 integer values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    I'm working in VC++ 6, but have been doing a lot of embedded C lately )

    If I carry out something like:

    Code:
    int Number_Out = atoi("11111111111111")
    This does not return 14 integers of value 1, I dont know if thats what you mean?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, of course not. But your loop will read in 14 distinct strings, so your string will not contain 11111111111111 - at least not if you do it right. Your string should be "1", then another string with "1", etc, etc.

    Just like your initial exampel doesn't set
    Code:
    int Number = 11111111111...
    , but
    Code:
    int number[14] = {1, 1, 1, ...};
    If you really want to read in ONE string of lots of digits, and then split it into an array of integers, then you would not be using atoi() - perhaps number_in[x] - '0' is the right thing for you? But that doesn't read in an integer array from the console, it converts a string of digits into an array of integers - which is SIMILAR BUT NOT THE SAME.

    By the way, atoi("111111111111111") will overflow, so you will get some completely different number back - because the max number in an integer is about 11-12 digits long. atoi does not recognize overflows, but will just continue converting the whole number, and give you back whatever the result is as a 32-bit number (which may be a negative number, even if the input was positive).


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    Hmm, thanks for your help - but I seem no further forward..

    I have broken the problem down into blocks; I need to do the input phase now.

    I have knowcked up this code to read a two digit string and atoi() into integer array.

    However it does not work as expected (btw I have switched to Visual C++2008 express in work this morning). Hopefully can scale it up to 14 integers once working..

    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int intArry[2];
    	char strArry[3];	//Extra for Null terminate
    	int i;
    
    	printf("Enter two integers: \n");
    
    	scanf("%s", strArry);
    
    	for (i=0;i<2;i++)
    	{
    		intArry[i] = atoi(&strArry[i]);
    	}
    
    	printf("%d%d" , intArry[0],intArry[1]);
    
    	return 0;
    }
    I really dont get it, I would be expecting:

    Enter two integers:
    23
    23
    Press any key to exit.....
    But instead I get:

    Enter two integers:
    23
    233
    Press any key to exit..
    Any advice?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well. atoi(&strArry[0]) is 23, since that's what "23" is as an int. And similarly, atoi(&strArry[1]) is 3, since that's what "3" is as an int. You need to decide whether you're supposed to have integers or digits, and act accordingly.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    Blah Blah..

    Something really wrong with the internet these days, to much bull$hit messages.

    After many hours searching and reading "riddle" posts, like those above: problem solved using some logic.

    I will put here for countless others with the problem that I saw posting online:

    Code:
    #include <stdio.h>
    
    int main()
    {
    intArry[3];
    charArry[4]; // Allow one extra for NULL terminat
    
    scanf("%s", charArry);
    
    for (i=0;i<3;i++)
    {
    intArry[i] = charArry[i] - 48; //Here is the magic, just look at ASCII table to see how its work
    }
    Once again, thanks for your bull$hit answers!

    Thread closed

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pico
    Something really wrong with the internet these days, to much bull$hit messages.
    Including your own, since you did not clearly express your requirements, leaving the reader to deduce what you wanted from posts #1 and #3.

    Quote Originally Posted by pico
    I will put here for countless others with the problem that I saw posting online:
    Good to see that you managed to find an answer by searching the Web. However, the answer could be improved by using a char literal instead of a "mysterious" number:
    Code:
    #include <stdio.h>
    #include <stddef.h>
    
    int main()
    {
        intArry[3];
        charArry[4]; /* Allow one extra for NULL terminator */
        size_t i;
    
        scanf("%s", charArry);
    
        for (i = 0; i < 3; i++)
        {
            intArry[i] = charArry[i] - '0';
        }
    
        /* ... */
    }
    See? No need for "magic". Of course, note that this particular use of scanf() is vulnerable to buffer overflow.

    Quote Originally Posted by pico
    Once again, thanks for your bull$hit answers!
    I am giving you an unofficial warning: stop trolling and behave yourself.
    Last edited by laserlight; 12-23-2008 at 05:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And explictly putting the types on the arrays...

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    laser-

    Trolling?

    This is my first topic in the forums for 4 years, hardly a troll.

    I was explicit with my question, i posted source that I was working on and what I expected / recieved from my program.

    Is this not how to ask a question?

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    Or I am very sorry that we are not all "experts" like you, otherwise no need for the forums

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pico
    Trolling?

    This is my first topic in the forums for 4 years, hardly a troll.
    Yes, in your reaction. If you did not get the answer you were looking for here but found it elsewhere, go ahead and post the answer that you found, but there is no need to accuse the replies as being "bull$hit".

    Quote Originally Posted by pico
    I was explicit with my question, i posted source that I was working on and what I expected / recieved from my program.

    Is this not how to ask a question?
    The problem is that you posted things related to your actual question, but did not actually present the requirements. It is indeed good to post what you tried, but without knowing exactly what you are trying to do, this secondary information can be misleading.

    Consider your statement that you wanted to "Basically scan for 14 integers and store them into each array segment." From the scanf format string, it looks like those 14 integers are delimited by spaces. If you read matsp's reply in post #2, you would see that he indeed assumed as such. Then in post #4 matsp finally made the relevant guess:
    Quote Originally Posted by matsp
    If you really want to read in ONE string of lots of digits, and then split it into an array of integers, then you would not be using atoi() - perhaps number_in[x] - '0' is the right thing for you? But that doesn't read in an integer array from the console, it converts a string of digits into an array of integers - which is SIMILAR BUT NOT THE SAME.
    Unfortunately, you apparently did not notice that guess. (To be honest, neither did I, until further examination of the thread.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    Read post one, line three

    what I want to do is promt the user to input the 14 digit number and fill the integer aray using their input

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    if you are not going to read the post in full, do not reply.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pico
    what I want to do is promt the user to input the 14 digit number and fill the integer aray using their input
    That's precisely what I mean by it not being clear: how do you want to fill the integer array? If you changed "fill the integer aray using their input" to "store each digit as a separate integer in the integer array", then I suspect matsp would have went ahead with the number_in[x] - '0' suggestion immediately in post #2, instead of just guessing in post #4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM