Thread: loop question..

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    loop question..

    I really am starting to feel stupid because i no theres only something tiny wrong with this.. im using a for loop to read in values from the keyboard into an array but after i enter in the 5th value i get an error message..

    Code:
    #include <stdio.h>
    main()
    {
    	float euro[4];
    
    
    
       int i;
    
    
    
       for (i=0; i<5; i++)
       	{
             printf("Please enter no.%d value",i+1);
          	scanf("%f",&euro[i]);
          }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Balana
    Code:
    #include <stdio.h>
    main()
    {
    	float euro[4];
    
    
    
       int i;
    
    
    
       for (i=0; i<5; i++)
       	{
             printf("Please enter no.%d value",i+1);
          	scanf("%f",&euro[i]);
          }
    }
    You only allocated space for 4 floats.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    thank you

    my array has to contain 5 elements. When i had

    Code:
     float euro[5];
    somebody told me i was wrong because that means i have 6 elements because i was forgetting about the 0

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    euro[5] contains the following valid indicies:

    euro[0]
    euro[1]
    euro[2]
    euro[3]
    euro[4]

    but NOT euro[5].
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    4
    After looking at the code revising, cant get what's the error.

    is "float euro[5]; "?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM