Thread: scanf dont work inside in a while

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    4

    scanf dont work inside in a while

    Hello everybody, I dont know why, but when I put "scanf" inside of a "while, it doesnt work...

    My code is:
    Code:
    #include <stdio.h>
    
    main () {
    
        int numero[5];
        int contador = 0;
    
        while (contador < 5){
                contador++;
                printf("Enter with a value");
                scanf("%d",numero[contador]);
    
    
        }
    }
    Thank u!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This has nothing to do with while, and everything to do with a broken scanf statement. In other words, this line
    Code:
    scanf("%d", numero[contador]);
    won't work even if you get rid of the while.

    Scanf needs to know the location of where to put the answer.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Define "doesn't work".

    1. You shouldn't be incrementing contador until the bottom of the loop. Otherwise...
    2. Arrays start at 0, your first access point would be 1, if...
    3. You need the address of the element, not the value. Use &:
    Code:
    scanf("%d", &numero[contador]);

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

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    4
    I remember now quzah, thank u!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. scanf doesn't like order of input
    By yougene in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 12:59 PM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  5. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM