Thread: Reading integers until end of input

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Reading integers until end of input

    HI,

    I am writing a program that turns decimal numbers to their binary form. The transforming from decimal to binary is easy but I have a problem with the input.
    I should be able to receive any length of input but I am not allowed to keep it in an array.
    The display of output should take place only after the user finishes inserting the numbers.
    For example:
    Input: 3 4 9
    output:

    3 11
    4 100
    9 1001
    Also, I should get receive the numbers as integers and not as string.
    So, have any idea?

    thanks,
    nivo

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I should be able to receive any length of input but I am not
    > allowed to keep it in an array.

    What? Meaning I could do this:

    23333333333444444444444444444444444444444443234143 23412413421358598589518998598518951985988951895985 98981981598519859898598598538915989859819859859859 89895182985985985219859815298985981598539853985985 19859823598533529853981598159819859819858915982895 89125935823333333333444444444444444444444444444444 44323414323412413421358598589518998598518951985988 95189598598981981598519859898598598538915989859819 85985985989895182985985985219859815298985981598539 85398598519859823598533529853981598159819859819858 91598289589125935823333333333444444444444444444444 44444444444323414323412413421358598589518998598518 95198598895189598598981981598519859898598598538915 98985981985985985989895182985985985219859815298985 98159853985398598519859823598533529853981598159819 859819858915982895891259358

    And then you'll convert that into it's binary form? HAH! Good
    _______ luck. Sure, it could be done, but I damn wouldn't waste
    time on it.

    If this isn't what you mean please clarify. (HINT: Linked Lists are your friend.)

    Quzah.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    I'll explain

    HI,

    The input will be someting like that:"243 54 67 82".
    The problem is that I cannot store it amd I should not get it as a string.
    The ouptut will be:

    Decimal Binary
    243 1101
    54 10011
    and so on

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I hate to break it to you, but you have to store it somehow. You cannot simply read input forever and ever without storing it some place.

    Can you not use an array, or what exactly?
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       int c;
       FILE*fp=fopen("myfile.dat","w");
       if(!fp){exit(0);}
    
       puts("Enter numbers seperated by spaces. Terminate input by pressing ENTER.");
       while( (c=fgetc(stdin)) != '\n' ) fputc( c, stdout );
       fclose( fp );
       fp = fopen("myfile.dat","r");
       /**
       *** Now, read an integer using fscanf() and conver to binary.
       *** repeat until the file is done.
       **/
    
       return 0;
    }
    I've just "cheated". Store it in a file, then read it. It isn't an array, but you can basicly treat it as one. Otherwise, without storing it in memory at the time, there is no viable way to do this.

    Quzah.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include<stdlib.h>
    #include<ctype.h>
    
    int main()
    {
    	int num;
    	char more;
    	
    	while(true)
    	{
    		printf("\nEnter a number to convert: ");
    		while( scanf("%d",&num) != 1 || num < 0 || num > 32767)
    		{
    			while(getchar() != '\n') continue;
    			printf("\nError, invalid input. Try again: ");
    		}
    		while(getchar() != '\n') continue;
    		//convert number to binary and print it
    		printf("\nNumber: %d\n",num);
    		printf("\nAgain?(Y/N): ");
    		while( scanf("%c",&more) != 1)
    		{
    			while(getchar() != '\n') continue;
    			printf("\nInvalid! Again? (Y/N): ");
    		}
    		if( toupper(more) == 'N')
    			break;
    	}
    }
    It's a little bit messy because of scanf's leaving newline characters in the buffer, but you can work with it. It's one solution in many possibilities.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is, Troll_King, it sounds like they have to input _ALL_ numbers first, without storing any of them, and then parse them and convert to a binary. So it looks like (from my understanding of it) that they need:

    "Input numbers:"
    32
    75
    235
    2664
    344
    2344
    233

    "Here is your input converted to binary!"
    32 (binary form here)
    75 (binary form here)
    ... and so on

    Quzah.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You can extract the digits than run them through a case statement, extracting the digits for example:

    number = 435;

    Code:
    		if(num > 99)
    		{
    		    int first = num / 100;
    			int second = (num / 10) % 10;
    			int third = num % 10;
    			printf("first %d\n",first);
    			printf("second %d\n",second);
    			printf("third %d\n",third);
    		}
    But if you have to enter them all first than it is illogical. It can however be done without arrays. Yet arrays would make it easier.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ... try this
    Code:
    #include <stdio.h>
    
    main ()
    {
     int i;
     char c;
     
     for (; ((c = getc (stdin)) != '\n') && c != EOF;)
     {
      ungetc (c, stdin);
      scanf ("%d", &i);
      printf ("%d", i);
      printf (" ");
      // Firugre out what the binary equivalent is...
      printf ("You typed in %d, BTW.\n", i);
     }
     return 0;
    }
    It is quite possible that this is what you want. I think it's compiler dependent, but I'm pretty much relying on the fact that once you do the first scanf or getc, the program's not going to do any processing untill you're finished supplying the buffer with input (you finish by pressing enter... or maybe by putting in too many characters). All the stuff with the char c is just to detect whether return has actually been encountered.

    Again however, I stress that this is compiler dependent. Try compiling it, and see if it works for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM