显示基于用户输入的整数的字符

Displaying a character based on integer entered by user

本文关键字:输入 整数 字符 用户 于用户 显示      更新时间:2023-10-16

我相信这对这里的许多人来说都是新手,但我目前正在努力学习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 zerodo 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);