Am I reading User input into Arrays correctly?

This is a discussion on Am I reading User input into Arrays correctly? within the C Programming forums, part of the General Programming Boards category; So I need to read 32-bits of user input into 2 arrays and each bit is separated by a white ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    Am I reading User input into Arrays correctly?

    So I need to read 32-bits of user input into 2 arrays and each bit is separated by a white space. When I enter the 32bits and then press enter, the program crashes.

    example:

    Input 32bits: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1


    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    
    
    
    int main (void){
        
        int a[32], b[32], sum[32];
        
        
        printf("Enter 32 bits: ");
        scanf("%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,"
        "%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d", a[31], a[30], a[29], a[28], 
        a[27], a[26], a[25], a[24], a[23], a[22], a[21], a[20], a[19], a[18], a[17], 
        a[16], a[15], a[14], a[13], a[12], a[11], a[10], a[9], a[8], a[7], 
        a[6], a[5], a[4], a[3], a[2], a[1], a[0] );
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program is tired, and keeps tripping over all those ungodly comma's in your scanf() format string (between the double quotes).

    Get rid of them, for starters. Add an ampersand, before every array reference as well: &a[27], etc.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    use a loop to read in your value, you don't want to type all those in the scanf when the size of the array gets large.
    Code:
    for( int i = 0 ; i < MAX ; ++i)
       scanf("%d",&a[i]);
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    
    
    
    int main (void){
        
        int a[32], b[32], sum[32];
        
        
        printf("Enter 32 bits: ");
        gets(a);
    
    }
    use the gets() function from the stdio.h header file. This should allow you to put your 32 integers in to the array a.

    here is a link: gets - C++ Reference

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    gets will get you into a world of hurt.
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Quote Originally Posted by nimitzhunter View Post
    gets will get you into a world of hurt.
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    Eh, I didnt realize the prototype required char and ........, I guess I wont be using that much anymore thanks for sharing that.

    Well I'm am going to have to second the above post with the for loop then.

  7. #7
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by nimitzhunter View Post
    use a loop to read in your value, you don't want to type all those in the scanf when the size of the array gets large.
    Code:
    for( int i = 0 ; i < MAX ; ++i)
       scanf("%d",&a[i]);
    exactly what i had in mind.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by threwup View Post
    So I need to read 32-bits of user input into 2 arrays and each bit is separated by a white space. When I enter the 32bits and then press enter, the program crashes.
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    int main (void){
        
        int a[32], b[32], sum[32];
          
        printf("Enter 32 bits: ");
        scanf("%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,"
        "%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d", a[31], a[30], a[29], a[28], 
        a[27], a[26], a[25], a[24], a[23], a[22], a[21], a[20], a[19], a[18], a[17], 
        a[16], a[15], a[14], a[13], a[12], a[11], a[10], a[9], a[8], a[7], 
        a[6], a[5], a[4], a[3], a[2], a[1], a[0] );
    }
    1) #include for standard headers is #include <header.h> the "header.h" form is for local headers.

    2) 32 bits, as integers in an array.... (You will need to add your own error trapping)

    Code:
    char bits[33];
    
    printf ("Enter 32 bits (no spaces)");
    scanf("%32s",bits);
    for (int i = 0 ; i<32 ; i++)
       bits[i] -= '0';

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    With the code above even after putting it in a main and adding the bracketts all i cant see it doing anything.

    you have %32s, which i thought just gives it 32 spaces and then the for loop look like itll just put the '0' in each space.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EZcode View Post
    With the code above even after putting it in a main and adding the bracketts all i cant see it doing anything.

    you have %32s, which i thought just gives it 32 spaces and then the for loop look like itll just put the '0' in each space.
    scanf("%32s", data) reads 32 characters from the keyboard.

    Try it....
    Code:
    #include <stdio.h>
    
    char data[33];
    
    int main(void)
      { puts("Enter a 32 bit binary value:");
    
         scanf("%32s",data);
         
         puts(data);   // shows raw data
        
         for (int i = 0; i < 32; i++)    // convert from ASCII
             data[i] -= '0';
    
        for (int o = 0; i<32; i++)     // display numeric data
            printf("%d",data[i]);    
    
        return 0; }
    Now type in a set of 32 characters consisting only of 1 and 0....
    What do you get back?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. Replies: 4
    Last Post: 04-03-2008, 10:07 PM
  3. Replies: 4
    Last Post: 04-21-2004, 05:18 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21