C++矩形的高度和宽度在3到20之间,程序检查非法条目
C++ height & width of rectangle between 3 to 20 with program checks for illegal entries
我正在尝试编写一个程序,允许用户使用任何给定字符选择矩形的高度和宽度(3 到 20 之间)。我需要输入非法条目,如果超出范围,则允许保持尝试输入。 下面的程序有效,但仅在小于 3 或大于 20 的情况下。我怎样才能让这个程序同时使用这两个条件?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int height, width;
char ch;
do {
cout<<"Enter desired height (3 to 20): ";
cin>>height;
if (height>3)
cout<<"";
else if (height<3)
cout<<"Illegal entry."<<endl;
} while (height<3);
do {
cout<<"Enter desired width (3 to 20): ";
cin>>width;
if (width>3)
cout<<"";
else if (width<3)
cout<<"Illegal entry. "<<endl;
} while (width<3);
cout<<"What character would you like for your rectangle? ";
cin>>ch;
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
cout<<ch;
}
cout<<endl;
cout<<endl;
}
return 0;
}
如何使用这两个条件使该程序工作?
你想使用逻辑和&&
和逻辑或||
if ( (height>3) && (height < 20) )
cout << "";
else if (height<3 || height > 20)
cout << "Illegal entry." << endl;
相关文章:
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 用C++程序计算圆锥体的体积、球体的体积、八边形的面积和两点之间的距离
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 是否允许类在程序中的不同翻译单元之间具有不同的定义?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- MSVC 2010 编译应用程序和 MSVC 2019 编译应用程序之间的行为差异
- OpenCV 函数 cv::remap() 的执行时间更长,当程序在两者之间进入睡眠状态时
- 如何使用 ALSA API 在应用程序中在单声道和立体声之间切换声音输出?
- 在C++中的程序之间共享指针
- 指针和程序性能之间有什么关系吗?
- 播放器未在程序中的"X"和"O"之间切换
- 使用命名管道在 C# 和C++应用程序之间进行持续通信
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- 为什么这个C++程序在代码::块和在线 IDE 之间返回不同的结果?
- C++程序和未识别程序之间的双向通信
- CPP 程序二查找两个给定日期之间的天数,conol CMD
- C# 和C++应用程序之间的通信
- 相同的 c 和 c++ 程序之间的巨大速度差异
- Electron应用程序和Win32本机桌面应用程序之间的进程间通信