Thread: Getting input help

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    34

    Getting input help

    Hello ladies and gentlemen,

    I am trying to convert a binary string of arbitrary length to hex.
    Here is my logic-> convert to decimal then printf("%X",var)

    My problem is i want to read in 8 bits at a time form input like this:

    1 0 1 0 1 0 1 0 1 0
    1 0 1 0 1 1 1 1 1 1
    ...

    *OR*

    1 1 1
    1 1 0
    1 0 1
    ...


    but my scanf

    scanf("%i %i %i %i %i %i %i %i",&a,&b,&c,&d,&w,&x,&y,&z);

    Reads 8 then it reads the next 8 and it there isnt enough 8 it goes on to the next line to read it.

    Is there a way using scanf to read 8 at a time and if it falls short to just read those few inputs and not go to the next line for more.

    Please help asap, in a crunch

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    scan as a string, then you can count the numbers before and the newline, if not enough, after converting those to ints, scan another line, then repeat. My suggestion would to use fgets, and also, when scanning those 8 things, it might be better to use an array.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    thank for replying linuxdude

    sry i didnt mention

    am not allowed to use arrays ( thast the problem lol)
    and can only use scanf :'(

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    if((scanf("%d %d %d %d %d %d %d %d\n")!=8)){
         FLUSH;
         if((scanf("%d %d %d\n")!=3)){
              printf("Syntax error\n");
              exit(1);
         }
         /*DO STUFF*/
    or something like that. Scanf stands for scan formatted. So you should really know how to read what you want before you actually do or else scanf is unsafe.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and can only use scanf :'(
    u may have trouble detecting end-of-line with scanf(). And if u are not allowed to use fgets(), then instead use getchar() to read one char at a time.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    while((x=fgetc(FILE))==1 || x==0){
         /*do stuff with the information you get*/
    }
    good idea swoopy

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nah, reading the end of the line will work just fine with scanf. Since that's the only input function they're allowed, fgetc won't work. However, they can make scanf work just like it, by only reading one byte at a time:
    Code:
    while( scanf("%c",&byte) != EOF )
    {
        if( count == '0' || count == '1' )
        {
            ...if it would be in our eight bytes for our first number, use it...
            ...otherwise, make a new number with it...
        }
    }
    I'll leave the middle portion to you, because what's the point of having homework if everyone here does it for you? However, since this is supposedly from a file, don't you mean fscanf?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The problem is, the first number may not be 8 wide. It may only be 3 wide.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So add a check for '\n' in there first. Then, based on the total count, shift by the remaining places once the newline is discovered. It doesn't matter. You can still handle it the same way.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    34

    Unhappy

    Sorry guys, but am not making u my homework, just need some help...

    Here is what i got after a while of thinking, its reading one char at a time.
    <code>
    int input=0;
    int count=0;
    int curr=0;
    int a=0,b=0,c=0,d=0,w=0,x=0,y=0,z=0;

    do{
    a=0,b=0,c=0,d=0,w=0,x=0,y=0,z=0;
    count = 0;
    do{
    count+=1;
    if(count>8){
    continue;
    }

    input = scanf("%i",&curr);

    if(input <1){
    continue;
    }
    // make sure u only get 1 adn 0's
    //printf("Count is: %i\n",count);
    //printf("Curr is: %i\n",curr);
    if(count==1){
    printf("Assigning a to curr before: %i ",a);
    a = curr;
    printf("after: %i\n",a);
    }
    else if(count ==2){
    printf("Assigning b to curr before: %i ",b);
    b = curr;
    printf("after: %i\n",b);
    }
    else if(count ==3){
    printf("Assigning c to curr before: %i ",c);
    c = curr;
    printf("after: %i\n",c);
    }
    else if(count ==4){
    printf("Assigning d to curr before: %i ",d);
    d = curr;
    printf("after: %i\n",d);
    }
    else if(count ==5){
    printf("Assigning w to curr before: %i ",w);
    w = curr;
    printf("after: %i\n",w);
    }
    else if(count ==6){
    printf("Assigning x to curr before: %i ",x);
    x = curr;
    printf("after: %i\n",x);
    }
    else if(count ==7){
    printf("Assigning y to curr before: %i ",y);
    y = curr;
    printf("after: %i\n",y);
    }
    else if(count ==8){
    printf("Assigning z to curr before: %i ",z);
    z = curr;
    printf("after: %i\n",z);
    }

    } while(input>0 && count >0 && count <9);

    int first = decimal(a,b,c,d);
    int second = decimal(w,x,y,z);

    // second and first switched to perform little-endian big-endian issue
    printf("Here is hex: 0x%X%X\n",second,first);
    printf("a=%i b=%i c=%i d=%i w=%i x=%i y=%i z=%i \n",a,b,c,d,w,x,y,z);

    }while(input!=EOF);
    </code>

    Basically i simulated an array but it chagnes when its not 8 inputs, it works fine with most of the input i tried,

    The outermost do while loop goes through the whole line,
    Now i have to make it so that it processes the next line and so on until EOF.

    Gonna tkae a break been working for hours.

    thank you for the on going help guys

    p.s. here si my assignment if u need to see clarification
    http://www.cs.toronto.edu/~iq/csc209.../csc209_a1.pdf

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    and also the the width and height of the input can be any size not just 8 and 3

    so hard to do this without arrays and get lines

    graaaaaaaaaa!!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To use code tags, use [ ] and not < >. Also, be sure you indent, otherwise what's the point?
    Here is the basic idea:
    Code:
    read one character, like I did in my example
        if the character is a one or zero
            increment counter
            << the current value one place
            & the new value
        if the character is a newline
            handle the current value however you need to
                (return it, pass it some place, shift it to pad left if that's what you're
                doing, whatever)
            reset the counter to zero to begin a new word
        otherwise, ignore whatever this character is and continue.
    As I've illustrated, it's fairly simple. All you have to do is decide if you're left padding your value by the remainder or not (ie: the first byte you read, is it the left most? Or is its "leftness" determined only by the number of "bits" listed on this line?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM