如何根据输入向右或向左移动*?[C++]
How to move * to the right or left, based on input? [C++]
所以,我想写一个 c++ 程序,这样它就可以移动 *,根据用户输入,用户将能够根据需要多次向左或向右移动它。
cout << "*" << endl;
cout << Enter: l (Move Left) or r (Move Right): << endl;
cin >> c;
while (c == 'k' || c == 'l'){
if(c == 'k'){
这就是我目前所拥有的,我不知道如何进行。提前感谢您的任何建议/帮助。
试试这个:
#include <iostream>
using namespace std;
int main()
{
char c='x';
int position = 0; //starts from the left most position
int temp =0;
cout << "*" << endl;
cout << "Enter: l (Move Left) or r (Move Right) or x (exit): " << endl;
cin >> c;
while (c != 'x')
{
if((c == 'r')||(c=='l'))
{
if(c == 'r')
{
position++;
}
else if(c == 'l')
{
if(position==0)
position=0;
else
position--;
}
temp=position;
while(temp--!=0)
{
cout << " "; //print space
}
cout << "*" <<endl;
}
else
{
cout << "Wrong character enteredn" << endl;
}
cout << "Enter: l (Move Left) or r (Move Right) or x (exit): "<< endl;
cin >> c;
}
return 0;
}
输入:
rrrrrrrlllx
输出:
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
*
Enter: l (Move Left) or r (Move Right) or x (exit):
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 按值 C++ 返回时进行双倍移动
- 移动二维数组中的字符
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- 安全到标准:移动会员?
- OpenVR:向视图方向移动