/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:error.cpp
*作 者:惠睿
*完成日期:2014年11月6日
*版 本 号:v1.0
*
*问题描述:补全程序,输出星号图。
*/
#include <iostream>
using namespace std;
void printchs (int m,char ch)
{
for (int j=1;j<=m;++j)
cout<<ch;
}
int main( )
{
int n=6; //n代表要输出的行数
int i;
//通过在下面的循环里调用printchs函数,输出右面的图
for(i=1; i<=n; ++i)
{
printchs(n-i,' ');
printchs(2*i-1,'*') ;
cout<<endl;
}
return 0;
}
运行结果:
知识点总结:在定义printchs函数时,printchs前一定要加void;在输出时,既然已经把空格和星号定义成ch,就直接写cout<<'ch';
如果写成:
cout<<" ";
cout<<"*";
那么将会输出不一样的结果。