How to save entered strings and integers in C -
can me on code. should when enter "create 6 5" supposed create 6x5 rectangular shape *'s, not show yet unless entered "show".
this code i'm working on, not correct yet, still figuring out how it.
i'm new in programming.
char s[100]; char *s1 = "show"; char *s2 = "create"; int main() { int zwidth = 0, zheight = 0, zloop = 0, aloop = 0; { scanf("%s %d %d", &s, &zwidth, &zheight); } while (strcmp(s1, s) != 0); if(strcmp(s2, s) == 0) { //to draw first horizontal line for(zloop = 0; zloop < zwidth; zloop++) printf("*"); printf("\n"); //drawing vertical lines for(zloop = 2; zloop < zheight; zloop++) { printf("*"); for(zloop = 0; aloop < zwidth-2; aloop++) printf("*"); printf("*\n"); } //last horizontal line for(zloop = 0; zloop < zwidth; zloop++) printf("*"); printf("\n"); } return 0; }
#include <stdio.h> enum command_state { show, create, quit, help, invalid }; char command_line[100]; const char *show = "show"; const char *create = "create"; const char *quit = "quit"; const char *help = "help"; const char *qmark = "?"; enum command_state command(void){ if(fgets(command_line, sizeof(command_line), stdin) != null){ char operation[16]; sscanf(command_line, "%15s", operation); if(!strcmp(operation, show)) return show; else if(!strcmp(operation, create)) return create; else if(!strcmp(operation, quit)) return quit; else if(!strcmp(operation, help) || !strcmp(operation, qmark)) return help; else return invalid; } return quit;//input eof } int main(void){ int rwidth = 0, rheight = 0, aloop = 1; while(aloop){ switch(command()){ case show : if(rwidth == 0 || rheight == 0) printf("execute create required before\n"); else { int h, w; for(h = 0; h < rheight; h++) { for(w = 0; w < rwidth; w++) printf("*"); printf("\n"); } } break; case create : if(2!=sscanf(command_line, "create %d %d", &rwidth, &rheight) || rwidth < 1 || rheight < 1){ rwidth = rheight = 0; printf("invalid create command\n"); } break; case quit : aloop = 0; break; case : printf( "help or ? : this\n" "create w h: set size of rectangle width , height\n" "show : draw filled rectangle\n" "quit : end of program\n" ); break; default : printf("invalid command\n"); } } return 0; }