Created
December 11, 2019 17:29
-
-
Save NotaPhysicist/8eaf61940afff6271689a87fe72f5dee to your computer and use it in GitHub Desktop.
A program to print a requested character in requested rows and columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* chline.c -- A program to print a requested character in requested rows and columns */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
char getcharacter(void); | |
int getint(void); | |
void printchar(char character, int rows, int cols); | |
int main(void) | |
{ | |
char character; | |
int rows = '\0'; | |
int cols = '\0'; | |
/* get input from user */ | |
printf("Enter the character to print: "); | |
character = getcharacter(); | |
printf("Enter the number of rows: "); | |
rows = getint(); | |
printf("Enter the number of columns: "); | |
rows = getint(); | |
/* output the grid of chars */ | |
printf("character = %c", character); | |
printchar(character, rows, cols); | |
return EXIT_SUCCESS; | |
} | |
void printchar(char character, int rows, int cols) | |
{ | |
int i, j; | |
for (i = 0; i < rows; i++) | |
{ | |
for (j = 0; j < cols; j++) | |
{ | |
putchar(character); | |
} | |
putchar('\n'); | |
} | |
} | |
char getcharacter(void) | |
{ | |
char character; | |
character = getchar(); | |
while (getchar() != '\n' ) | |
{ | |
continue; | |
} | |
return character; | |
} | |
int getint(void) | |
{ | |
int input; | |
char ch; | |
while (scanf("%d", &input) != 1) | |
{ | |
while ((ch = getchar()) != '\n') | |
{ | |
putchar(ch); | |
} | |
printf(" is not an integer.\n"); | |
printf("Please enter an integer value: "); | |
} | |
return input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment