Created
December 11, 2019 18:31
-
-
Save NotaPhysicist/01186cee8e3ea8236c64d331c38f42ce to your computer and use it in GitHub Desktop.
A program to print a requested character in requested rows and columns using a nested for loop and putchar().
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 using a nested for loop and putchar | |
*/ | |
#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(); | |
/* test that the variable is indeed a character */ | |
printf("main: character = %c\n", character); | |
/* output the grid of chars */ | |
printchar(character, rows, cols); | |
return EXIT_SUCCESS; | |
} | |
void printchar(char character, int rows, int cols) | |
{ | |
int i, j; | |
/* test that the variable is indeed a character */ | |
printf("printchar: character = %c\n", character); | |
putchar(character); | |
for (i = 0; i < rows; i++) | |
{ | |
for (j = 0; j < cols; j++) | |
{ | |
putchar(character); | |
printf("%c", character); | |
} | |
putchar('\n'); | |
} | |
} | |
char getcharacter(void) | |
{ | |
char character; | |
character = getchar(); | |
/* discard any remaining characters in the buffer */ | |
while (getchar() != '\n') | |
{ | |
continue; | |
} | |
return character; | |
} | |
int getint(void) | |
{ | |
int input; | |
char ch; | |
while (scanf("%d", &input) != 1) | |
{ | |
/* discard any remaining characters in the buffer */ | |
while ((ch = getchar()) != '\n') | |
{ | |
/* echo them back to the user */ | |
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