Thread: printing problem

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    printing problem

    i think the logic in my program is alright but the only poblem im having is saving the values throughout the process and outputting it at once. Its supposed to be a string that is constantly updated, in this case called rule. it keeps giving me a segmentationaly fault for some reaosn and ive not been able to figureout why.
    any help and alternative methods are appreciated!

    the segmentationaly error occours once it reads the 1st r_o value in the second for loop.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <math.h>
    
    
    int main(int argc, char **argv)
    {
        int width = 0;
        int height = 0;
        double r_o = 0;
        double g_o = 0;
        double b_o = 0;
        double r_e = 255;
        double g_e = 0;
        double b_e = 0;
        int r_number = 0;
        int g_number = 0;
        int b_number = 0;
        int counter = 0;
        int Rule =0;
        int rule[Rule];
        int p_count = 0;
        
       
        scanf("&#37;d", &width);
        scanf("%d", &height);
    
        int i = 0;
        int j = 0;
    
        for (i = 0; i<= width; i++){
                        
            rule[Rule]='[';
                        Rule++;
    	for (j = 0; j<=height; j++) {
                
                scanf("%lf", &r_o);
                counter = 0;
                if (r_o < r_e) {
                    r_number = r_e - r_o;
                    
                        while(counter<=r_number) {
                        rule[Rule]='R';
                        Rule++;
                        counter++;
                    }
    
                } else if (r_o > r_e) {
                    r_number = r_o - r_e;
                    while (counter <= r_number) {
                        rule[Rule]='r';
                        Rule++;
                        counter++;
                    }
                }
    
    	    scanf("%lf", &g_o);
                counter = 0;
                if (g_o < g_e) {
                    g_number = g_e - g_o;
                    while (counter <= g_number) {
                         rule[Rule]='G';
                        Rule++;
                        counter++;
                    }
    
                } else if (g_o > g_e) {
                    g_number = g_o - g_e;
                    while (counter <= g_number) {
                        rule[Rule]='g';
                        Rule++;
                        counter++;
                    }
                }	
    
    	    scanf("%lf", &b_o);
                counter =0;
                if (b_o < b_e) {
                    b_number = b_e - b_o;
                    while (counter <= b_number) {
                        rule[Rule]='B';
                        Rule++;
                        counter++;
                    }
    
                } else if (b_o > b_e) {
                    b_number = b_o - b_e;
                    while (counter <= b_number) {
                        rule[Rule]='b';
                        Rule++;
                        counter++;
                    }
                }
    
        rule[Rule]='F';
                        Rule++;
            }
     rule[Rule]=']';
                        Rule++;
    rule[Rule]='+';
                        Rule++;
    rule[Rule]='f';
                        Rule++;
    rule[Rule]='-';
                        Rule++;
        }
    
    
       for(p_count = 0; p_count <=Rule;p_count++) {
          printf("%c",rule[p_counter]);
        }
    
    
     return(0);
    }
    dont be harsh..im fairly new to this stuff lol
    Last edited by piyush_v; 06-07-2007 at 03:30 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int Rule =0;
    > int rule[Rule];
    This isn't even legal C.
    What you've basically got here is
    int rule[0];
    which simply has no space for storing anything.

    Perhaps
    int rule[1000];
    and make sure that Rule keeps within bounds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try flushing stdout here:
    Code:
       for(p_count = 0; p_count <=Rule;p_count++) {
       {
          printf("&#37;c",rule[p_counter]);
          fflush(stdout);
       }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    thanks guys. it seems to be working with a few tweaks.
    also,
    say i want to declare an array size of (500X400X3X6) ie large array size it doesnt let me do this the normal way and when i compile it it gives me an error saying array size too big>?
    question being: how do i declare very large arrays?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try using malloc() to dynamically allocate memory. Check return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Problem Printing to File
    By jamez05 in forum C++ Programming
    Replies: 10
    Last Post: 09-26-2006, 11:20 AM
  3. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM