设计函数chline(ch, i, j),实现指定字符在i列到j列的输出,并用一个简单的驱动程序测试该函数。
#include<stdio.h>
void chline(char ch , int i , int j);
int main(void)
{
int i = 0;
int j = 0 ;
char ch = ' ';
printf("enter the char you want to print:");
ch = getchar();
printf("enter i , j:");
scanf("%d %d",&i,&j);
chline(ch,i,j);
return 0;
}
void chline(char ch , int i , int j )
{
for(int col1 = 1 ; col1 < i ; col1++)
putchar(' ');
for(int col2 = i ; col2 <= j ; col2 ++)
putchar(ch);
putchar('\n');
}
当准备输入i,j时,直接跳出程序了,怎么回事?
没问题啊,你输入错误了吧,输入 i和j时,两个值中间要有空格
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
void chline(char ch, int i, int j);
void chline(char ch, int i, int j)
{
for (int col1 = 1; col1 < j; col1++)
putchar('\n');
for (int col2 = 0; col2 < i; col2++)
putchar(' ');
putchar(ch);
putchar('\n');
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
int j = 0;
char ch = ' ';
printf("enter the char you want to print:");
ch = getchar();
printf("enter i , j:");
scanf_s("%d %d", &i, &j);
chline(ch, i, j);
return 0;
}
getchar输入后还有一个/n,直接进入scanf了,你得先把/n排掉