In: Computer Science
IN C
This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program should continue this behavior until you have received an appropriate integer. Use a loop to trap the user until you get the value that you want as you did in a previous assignment. You should also clear the keyboard buffer after each scanf so that the user can type characters or digits in their response. Once you have a number in the appropriate range, call a function that you write called draw_line that takes two arguments. One argument is the number of characters and the other argument is the character to draw. The function will use a for loop to draw that number of characters on the screen and then will print a newline.
#include<stdio.h>
#include <stdlib.h>
void draw_line(int v,char ch) // draw_line function definition with two arguments- no. of characters and the chracter to draw
{
int i;
for(i=0;i<v;i++)
printf("%c",ch ); // for loop to draw the character desired number of times on the screen
printf("\n"); // printing the new line
}
int main(){ // main function definition
char ch;
int v;
do{
scanf("%c%d",&ch,&v); // taking the character and the no. of character to draw as input from the user
if(v<1||v>79) // checking whether the number is in the range or not
{
printf("The number must be in the range 1 to 79(both included) : ");
fflush(stdin); // clearing the keyboard buffer
continue;
}
draw_line(v,ch); // calling the draw_line function
break; // do-while loop breaks when we get the desired no.
}while(1);
return 0;
}