Thread: variable crash

  1. #1
    Unregistered
    Guest

    variable crash

    I had a working program that displayed a hollow rectangle from *. I wanted the the user to be able to choose what symbol the rectangle would be drawn from. My attempt killed the program.
    I knew it was too easy to work. what can be don to fix this?


    Code:
    int main(void)
    {
    	int inner, outer;// loop control varibles
    	int length, width; 
    	int icon;// was *
    	printf("This program draws a hollow rectangle using a character of your choice\n\n");
    
    	printf("Choose character for display\n ");
    	scanf("%d", &icon);
    	printf("Enter length: ");
    	scanf("%d",&length);
    	printf("Enter width: ");
    	scanf("%d",&width);
    
    
    	for(inner = 0; inner < width; inner ++)// prints top row of the icon
    		
    		printf("icon");// was *
    
    	    printf("\n");
    
    	for(inner = 0; inner < length; inner ++)// prints both sides starting with left
    	{ 
    		printf("icon");//was*
    		
    	for(outer = 0; outer < (width-2); outer ++)// prints spaces to where the right column should be 
    		 
    		printf(" ");
    		
    		printf("icon");// prints an icon in the right column                             //was*
    
    		printf("\n");
    	}// end for
    	for(inner = 0; inner < width; inner ++)// prints bottom row of icon
    		
    		printf("icon");//was*
    
    	    printf("\n\n");
    		
    }// end main

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    you've got the right idea... just use a char for the output character instead of an int, and scanf a character... [or you could get the ascii character from a numeric input if you wanted to...] also, i take it you are aware of the usage of printf in a formatting style since you are using scanf correctly... aside from that, use code tags, they can help us diagnose your problem much better... i've edited your post implementing them...
    hasafraggin shizigishin oppashigger...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int icon;// was *
    char icon;

    > scanf("%d", &icon);
    scanf("%c", &icon);
    This reads a single character

    > printf("icon");// was *
    printf("%c",icon);// was *

    Change all of these

    One more thing, add
    return 0;
    to the end of the main function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM