Thread: function with for loop

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    8

    function with for loop

    Hello I need help getting this program to run. I think something is wrong with the for loop.



    Code:
    #include<stdio.h>
    #include<math.h>
    
    int power_table(int startingint, int endingint, int exponent); //fucntion prototype
    
    
    int main()
    { 
        char line[10];
        int startingint;
        int endingint;
        int exponent;
        int x;
    
    
        printf("Enter the starting integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &startingint);
    
    
        printf("Enter the ending integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &endingint);
        
        printf("Enter the exponent:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &exponent);
        
        power_table(startingint, endingint, exponent);   \\function call
    }
    
    
    int power_table(int startingint, int endingint, int exponent) 
    {
        printf("x   x^n \n");
        printf("--------\n");
        for(x=startingint; x<=endingint; x++)            \\code errors out at for loop
        {
        printf("%d   %d \n", startingint, exponent);
        }
    }
    Last edited by rabert1; 04-16-2012 at 09:41 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You do not have X declared locally in your function.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Thanks got it up and running now.
    Code:
    #include<stdio.h>#include<math.h>
    
    
    int power_table(int startingint, int endingint, int exponent); //fucntion prototype
    
    
    int main()
    { 
        char line[10];
        int startingint = 1;
        int endingint = 1;
        int exponent = 1;
        int x = 1;
    
    
        printf("Enter the starting integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &startingint);
    
    
        printf("Enter the ending integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &endingint);
        
        printf("Enter the exponent:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &exponent);
        
        power_table(startingint, endingint, exponent);
    }
    
    
    int power_table(int startingint, int endingint, int exponent)
    {
        int x = 1;
        
        printf("x   x^n \n");
        printf("--------\n");
        for(x=startingint; x<=endingint; x++)
        {
        startingint = x;
        exponent = pow(startingint, x);
        
        printf("%d   %d \n", startingint, exponent);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function, function call, for loop
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2011, 04:03 PM
  2. function loop help
    By akahizzle in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2011, 11:15 AM
  3. while loop within a read function
    By aadil7 in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 03:33 PM
  4. Loop Function
    By evangela in forum C Programming
    Replies: 24
    Last Post: 10-06-2009, 08:20 PM
  5. How to loop a function
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-14-2002, 12:52 PM