This is a C program that I'm making, just to see if I can. It is supposed to make a 'button' with ASCII borders. I compiled it with GCC, with no errors/warnings, and when I run it all is prints is "Segment Fault". What does that mean?
This is a C program that I'm making, just to see if I can. It is supposed to make a 'button' with ASCII borders. I compiled it with GCC, with no errors/warnings, and when I run it all is prints is "Segment Fault". What does that mean?
Here's the code...
Code:#include <stdio.h> int make_button(char *name) { int length; int length_count; length = strlen(*name); length_count = 0; /* Print Top */ printf("╔");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╗"); /* Print Middle */ printf("║"); printf(" "); while(length_count <= length) { printf("%s",*name);length_count++; } printf(" "); printf("║"); length_count = 0; /* print bottom */ /* Print Top */ printf("╚");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╝"); return 0; } int main() { make_button("Hello"); printf("\n\n"); return 0; }
Try this.
Code:#include <stdio.h> #include <string.h> int make_button(char *name) { int length; int length_count; length = strlen(name); length_count = 0; /* Print Top */ printf("╔");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╗"); /* Print Middle */ printf("║"); printf(" "); printf("%s",name); printf(" "); printf("║"); length_count = 0; /* print bottom */ /* Print Top */ printf("╚");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╝"); return 0; } int main() { make_button("Hello"); printf("\n\n"); return 0; }
Last edited by swoopy; 09-26-2003 at 09:23 PM.
In your function you keep referring to *name. But since name is a pointer you don't need the *. You're referencing incorrectly.Originally posted by kinghajj
Here's the code...
Code:#include <stdio.h> int make_button(char *name) { int length; int length_count; length = strlen(*name); length_count = 0; /* Print Top */ printf("╔");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╗"); /* Print Middle */ printf("║"); printf(" "); while(length_count <= length) { printf("%s",*name);length_count++; } printf(" "); printf("║"); length_count = 0; /* print bottom */ /* Print Top */ printf("╚");length_count++; while(length_count <= (length-1)) { printf("═");length_count++; } length_count = 0; printf("╝"); return 0; } int main() { make_button("Hello"); printf("\n\n"); return 0; }
1) printf() returns the number of characters output so you could use
length_count += printf("═");
to keep track of the characters output.
2) the width of your button should be
length = strlen(name) + 2;
to take into account the spaces you placed on each side of the label
3) for the middle section:
you need to remove the loop, and to make it easy, one print statement should do it::Code:/* Print Middle */ printf("║"); printf(" "); while(length_count <= length) { printf("%s",*name);length_count++; } printf(" "); printf("║");
Code:/* Print Middle */ printf("║ %s ║", name);
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
hi
why don't you try to use "ncurses". You can create boxes, windows etc.
man ncurses