Thread: For loop problem :)

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    For loop problem :)

    I thought I was comfortable with using for loop since it's pretty simple and now I came across this problem and I see nothing wrong with it but the error kept on insisting I have not declared my counter when I did. This is my code:

    Code:
        //Declare data type and variables for the users input
        double x_coord[4];  //variable for x coordinates
        double y_coord[4];  //variable for y coordinates
        double best_location_x, best_location_y, total_x, total_y;  //variable for the centroid\
        int i;  //use for counter
        //Use for loop to get the coordinates of each x and y coordinates in an array set
        for (i = 1; i < 4; i++){
        //Get users input on first location
        printf("What are the x-y coordinates of location %d (values in miles between -50 and 50): ", i);
        //Store the users input
        scanf("%lf%lf", &x_coord[i], &y_coord[i]);
        } //end for loop
    The error says in the line for(i = 1; i<4; i++) 'i' is undeclared (first use in this function)
    I declared i in int = i; so I have no idea why it's not running.
    I appreciate it if anyone could lend a hand
    A hint would be lovely ^.^

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have a comment right before the declaration that ends with '\' and thus extends it to the row below, so your declaration is out commented.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You also might want to start i at 0 that way you get input at the first part of the array rather than the 2nd index

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop problem?
    By benrogers in forum C Programming
    Replies: 9
    Last Post: 02-08-2011, 12:26 AM
  2. do...while loop problem
    By geoffrey in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 03:49 PM
  3. cin loop problem
    By Mr Fancy in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2007, 08:13 AM
  4. do - while loop problem
    By estranged in forum C Programming
    Replies: 8
    Last Post: 12-10-2003, 07:11 PM
  5. do while loop problem
    By nelinda in forum C Programming
    Replies: 1
    Last Post: 11-30-2003, 09:29 AM