Why can't I read an int in an array?

This is a discussion on Why can't I read an int in an array? within the C Programming forums, part of the General Programming Boards category; OK, so this is my code Code: #include <stdio.h> #include <stdlib.h> int main() { int number[21]; printf("Hi "); scanf("%d", &number); ...

  1. #1
    Banned
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    3

    Why can't I read an int in an array?

    OK, so this is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main() {
        int number[21];
        
        printf("Hi ");
        scanf("%d", &number);
        printf("%d", number[1]);
        printf("\n\n");
        system("pause");
        return(0);
    }
    And I try to enter in a number, say 442342. Then my program should print out the second number in 442342, which is 4, but it prints out a random number that has like 6 digits.
    This works with char arrays, but I dunno why it doesn't with int arrays. Can someone help me out? Thanks!
    Last edited by fred2028; 10-08-2006 at 07:50 AM.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Float does not have a "length" in characters, so the whole thing is read into the first array part.

    Do you understand what float is? This doesn't seem to be the right place to use it.

    Try:
    Code:
    #include <iostream>
    int main(){
    	char n[256];
    	scanf ("%s",&n);
    	printf("%c",n[1]);
    	system("PAUSE");
    	return 0;
    }
    This reads the data into chars. Hmm, scanf doesn't check for buffer overloading.
    Last edited by maxorator; 10-08-2006 at 07:54 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Banned
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    3
    Quote Originally Posted by maxorator
    Float does not have a "length" in characters, so the whole thing is read into the first array part.

    Do you understand what float is? This doesn't seem to be the right place to use it.
    Well from what I've learned in class float is a decimal number ...
    And I've tried using int, same problem. It always prints out the number "2", ...

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It reads everything to the first part of array as I said.
    You can't read it as a number if you want them to be separated.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Oki if you read a int foo in into an array of ints it will be stored in the first position ofthe array. You would need to input digit by digit in a for loop to get your desisred result. Thou Ive used a different approach, I will post the code with no comments so you can read and try to figure it out. I like to leanr by examples so don't throw rocks on my cause I'm "doing" his homework:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void itoa(int n, char s[]);
    void reverse(char s[]);
    
    
    void reverse(char s[])
    {
    	int c, i, j;
    
    	for (i = 0, j = strlen(s)-1; i<j; i++, j--){
    		c = s[i];
    		s[i] = s[j];
    		s[j] = c;
    	}
    }
    
    void itoa(int n, char s[])
    {
    	int i, sign;
    
    	if ((sign = n) < 0)
    		n = -n;
    	i = 0;
    	do {
    		s[i++] = n % 10 + '0';
    	} while ((n /= 10) > 0);
    	if (sign < 0)
    		s[i++] = '-';
    	s[i] = '\0';
    	reverse(s);
    }
    
    int main()
    {
    	int number;
    	char cnumber[256];
    	scanf("%d",&number);
    	itoa(number,cnumber);
    	printf("%c",cnumber[2]);
    
    	return 0;
    }
    All those functions are well explained and commented in the C Programming Language by K&R I would strongly suggest you read it. The basic idea is get an int, transfor it to a char where you can access the nth element wich is the nth digit and print it. Hope it helps.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This reverses the numbers and keeps it as an integer:
    Code:
    int reverseDigits(int number){
    	string streamer;
    	char buffer[256],data[256];
    	stringstream ssin(streamer, ios_base::out);
    	ssin<<number;
    	strcpy(buffer,ssin.str().c_str());
    	int len=strlen(buffer);
    	for(int i=0;i<len;i++){
    		data[len-1-i]=buffer[i];
    	}
    	stringstream ssout(data);
    	ssout>>number;
    	return number;
    }
    This is the same thing only to this you can pass a pointer:
    Code:
    int reverseDigits(int *number){
    	string streamer;
    	char buffer[256],data[256];
    	stringstream ssin(streamer, ios_base::out);
    	ssin<<*number;
    	strcpy(buffer,ssin.str().c_str());
    	int len=strlen(buffer);
    	for(int i=0;i<len;i++){
    		data[len-1-i]=buffer[i];
    	}
    	stringstream ssout(data);
    	ssout>>*number;
    	return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      
      char number[BUFSIZ];
      
      puts("enter text:\n ");
      fgets( number , sizeof number , stdin );
      printf ( " your number is : %s\n" , number );
      getchar();	
      return 0;
    }
    output:
    Code:
    enter text:
    
    1234
    your number is : 1234

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    @mrsirpoopsalot: Not quite the same.

    OP, the only thing you really need to do here is pay attention to your array indices. &number is a pointer to a pointer, not a pointer to an int; &number[1] is empty, so you get whatever might be in the memory - or not.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int number[21];
    
       printf("Hi ");
       scanf("%d", &number[0]);
       printf("The number is %d\n", number[0]);
    
       /* system("pause"); sucks */
       return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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