c++程序接受int和打印*
C++ program to accept int and print *
我正在自学c++,我正在考虑一个简单的程序来适应语法。
int main(){
int num;
cout << "Enter a positive number:";
cin >> num;
printStar(num);
}
void printStar(int num){
.............................
}
这样printStar函数接受一个整数并打印*
,例如接受3并打印***
或接受6并打印******
或接受2并打印**
。我正在考虑使用for或while循环,并得到它做任何更好的想法建议?
您可以使用std::string
:
或STL的fill_n
:
您可以使用cout.fill
:
cout.fill('*');
cout.width(num);
cout << ' ' << endl;
注意,这会使很多东西混乱,所以你应该捕获并重置填充和宽度:
char oldfill = cout.fill('*');
streamsize w = cout.width();
cout.fill('*');
cout.width(num);
cout << ' ' << endl;
cout.width(w);
cout.fill(oldfill);
因为这是为了你自己的学习,我没有给出一个解决方案,一个提示是使用for或While循环。如果它能工作,那很好,否则就把你的代码和问题贴出来。
入门链接
相关文章:
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- c++:如何打印出一个强制转换的 int 值?
- 打印"矢量<对<int,int>>"
- C++:根据int值的基数使用条件格式打印
- 当我们声明 cin 为 int 并从 cin 中获取输入并在 cout 中打印 cin 时会发生什么?
- 将char类型指定给int以打印相应的ASCII字符
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 是否打印指向已定义 int 的成员指针
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- uint8_t在转换为无符号 int 时未打印正确的值
- 如何在<int> c++ 中将矢量打印到终端
- 如何在C++中打印矢量<map<string,int> >
- 我如何转换一个基于动态的,基于指针的int堆栈,以便将其打印成字符串
- 随机打印出int的大小,不知道为什么
- 从指针打印动态int阵列中获取怪异的字符
- 在GDB中打印int ** x
- C++。为什么 std::cout << char + int 打印 int 值?
- 如何在C++中打印int的ASCII值
- 打印int指针的值