显示基于用户输入的整数的字符
Displaying a character based on integer entered by user
我相信这对这里的许多人来说都是新手,但我目前正在努力学习C++,并深入了解如何将我的知识应用到现实世界中。
问题描述:编写一个程序,使用星号以图形方式描述整数的大小,创建直方图。提示:内部循环处理星号的打印,外部循环处理直到输入零。确保你不接受负数。样品运行:
Enter a positive integer (0 to quit): 5
*****
Enter a positive integer (0 to quit): 8
********
Enter a positive integer (0 to quit): -5
Enter a positive integer (0 to quit): -10
Enter a positive integer (0 to quit): 15
***************
Enter a positive integer (0 to quit): 0
Good Bye!
我的当前代码(我知道这无论如何都不对,但有助于展示我的想法,我知道你需要使用嵌套循环来正确回答这个问题。(:
#include <iostream>
using namespace std;
int main()
{
int ast; //Number of asterisk wanted to be displayed
char asts='*'; //Actual display coressponding to asterisk
cout<<"Enter a positive integar, (0 to quit)"<<endl;
cin>>ast;
while(ast!=0)
{
if(ast>0) // Ensuring no negative integars are entered
{
asts=ast;
cout<<asts<<endl;
cout<<endl;
}
else //Display if negative or other invalid data is entered
cout<<"Invalid Data, negative values are not accepted, try a positive integar or 0 to quit"<<endl;
cout<<"Do you want to continue? If so, enter another integar (0 to quit)"<<endl;
cin>>ast;
}
cout<<"Thank you for using the program."<<endl;
return(0);
}
感谢您提前提供的帮助!-Colin
基本思想是(使用伪代码(:
get number of asterisks
while number is not zero:
if number is negative:
output error message
else:
do number times:
output *
output newline
get number of asterisks
嵌套循环有while number is not zero
和do number times
。
然而,作为一名开发人员,您应该学习的最早技能之一是将任务正确分配给特定的代码片段(方法、函数、库等(。特别是,将代码模块化是一个好主意,这样每个部分都有一个明确的目的,然后用这些部分构建"上层"。
为此,这就是我处理这个问题的方式。首先,定义在这种情况下有用的函数。
从getAsterCount()
开始,它负责询问用户他们想要多少个星号,并验证它是否有效(一次又一次地询问,直到它有效为止(。例如:
#include <iostream>
int getAsterCount() {
// Until we get valid response.
for(;;) {
int count;
// Get value, checking for error, forcing exit if so.
std::cout << "Enter the number of asterisks, zero to exit: ";
if (! (std::cin >> count)) {
std::cout << "*** ERROR: could not read numbern";
return 0;
}
// Any non-negative value is allaowed.
if (count >= 0) {
return count;
}
std::cout << "That was less than zero, try again.n";
}
}
一旦到位,您就再也不用担心用户为该程序提供无效信息了,因为该函数会捕获它(您可以在任何地方调用它,如下面的main()
函数所示。
接下来,提供一个函数,该函数将根据您现在拥有的计数,实际输出星号:
void outputAster(int count) {
// Simple loop for asterisks then new line.
for (int i = 0; i < count; ++i) {
std::cout << '*';
}
std::cout << 'n';
}
有了它,您的主代码在概念上变得简单多了:
int main() {
int count = getAsterCount();
while (count > 0) {
outputAster(count);
count = getAsterCount();
}
std::cout << "Thank you for using the program. Now get off my lawn.n";
}
现在您正在尝试做:
asts=ast;
这没有多大意义,因为这只会将输入的整数分配给asts
。
要打印*
中的幅度,您需要从0
循环到输入的整数,并每次打印一个星号:
for(int i = 0; i < ast; i++) {
std::cout << "*";
}
std::cout << "n";
输出:
Enter a positive integar, (0 to quit)
5
*****
Do you want to continue? If so, enter another integar (0 to quit)
3
***
Do you want to continue? If so, enter another integar (0 to quit)
5
*****
Do you want to continue? If so, enter another integar (0 to quit)
0
我喜欢一种有点不同的方法,没有循环。
char asts[] = “*********n”;
在检查输入值是否在范围内后,只需写出字符串的尾部:
std::cout << (asts + 9 - ast);
相关文章:
- 如何打印不带空格的输入整数?
- 编写一个程序,输入整数 n 并打印该数字的字符数(符号除外)
- 如何在不中断剩余代码的情况下仅C++中输入整数
- 如何在输入整数时中断 for 循环
- C++ 在 CIN 双精度时挂起,但如果输入整数,则继续
- 我如何使用CIN输入整数,每个数字都被逗号分隔
- 如何输入整数并将其从最高到最低排序
- 如何创建一个程序,用户输入整数并输出90度数字金字塔
- 使用对输入整数的文件输入
- 使用户输入整数字符串,然后输出不同的字符串
- 从 fstream 到 2d 矢量的输入整数<矢量<int>> C++
- 用户输入整数后跟垃圾
- 输入整数直到 C/C++ 中的换行符
- 输入整数的最快方法
- C++循环 - 输入整数,直到用户退出
- 尝试输入整数行并在以矢量作为存储的函数的插入排序类型中排序
- 如何使C++程序在用户输入整数时抛出错误,而需要字符串
- 当要求输入整数并使用isdigit检查时,将char转换为int
- 如何通过指针输入整数数组并打印出数组和数组的大小(C++)
- 如果打印后输入整数,则C++分段故障,但其他情况下可以