Hi folks,
I have an end of chapter question here, that I believe I have solved, but am not sure if I've understood the question correctly in the first place. What do you think this question is asking?
Devise a function chline(ch, i, j) that prints the requested character in columns i through j. Test it in a simple driver.
I interpreted that to mean the user inputting a character, and then the program makes two columns, printing the character i times in the first column & j times in the second column. I came up with this (tested and works a la my interpretation):
Code:
/* exercise 2 in chapter 9 */
#include <stdio.h>
void chline(char x, int y, int z);
char get_first(void);
int get_int(void);
int main(void)
{
char x;
int y, z;
printf("\nPlease enter a character: ");
x = get_first();
printf("\nNow please enter integer for column x: ");
y = get_int();
printf("\nNow please enter integer for column y: ");
z = get_int();
chline(x, y, z);
return 0;
}
void chline(char x, int y, int z)
{
int i;
printf("\n\tx\t\ty\n");
printf("\t\b---\t\t\b---\n");
for(i = 0; (i < y) || (i < z); i++)
{
if((i < y) && (i < z))
printf("\t%c\t\t%c\n", x, x);
else if((i < y) && (i >= z))
printf("\t%c\n",x);
else
printf("\t \t\t%c\n", x);
}
}
char get_first(void)
{
int ch;
ch = getchar();
while(getchar() != '\n')
continue;
return ch;
}
int get_int(void)
{
int input;
char ch;
while (scanf("%d", &input) != 1)
{
while((ch = getchar()) != '\n')
printf("\n");
putchar(ch);
printf(" is not an integer.\nPlease inter an ");
printf("integer value, such as 25, -178 or 3:");
}
while(getchar() != '\n')
continue;
return input;
}
Do you think this is good or am I barkin' up the wrong tree?