用户输入的简单c++计数器程序
User input in simple C++ counter program
我试图在c++中制作一个简单的计数器程序,该程序将增加用户输入的变量(通过按某个键指定)。下面是我想到的:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int variable;
char userInput;
variable = 0;
cout << "To increment variable one, press a n";
do
{
variable = variable++;
cout << "variable++ n" ;
} while (userInput = "a");
}
我在这个网站上仔细阅读了几个相关的线程,相信这应该是可行的。然而,我得到了几个错误,包括对变量的操作没有定义,以及从"const char"到"char"的"无效转换"。"
在循环中添加cin:
cin>>userInput;
和更改
variable = variable++;
variable++; // or variable = variable + 1;
next you while条件应该像这样:
while (userInput=='a');
所以你的整个程序看起来像这样:
#include <iostream>
using namespace std;
int main()
{
int variable;
char userInput;
variable = 0;
cout << "To increment variable one, press a n";
do
{
cin>>userInput;
variable++;
cout << "variable++ n" ;
} while (userInput=='a');
return 0;
}
在while循环中需要一个std::cin,如
cin >> userInput;
此外,下面的行
variable = variable++;
将产生未定义行为。阅读更多关于序列点的信息以更好地理解。
试题:
variable = variable + 1;
最后在while循环中断条件下,代替赋值操作
while (userInput = "a");
使用比较:
while (userInput == "a");
你需要使用
std::cin >> userInput; // each time you go through the `do` loop.
同样,你只需要使用
variable++; // on the line.
也!使用cout << variable << "n";
最后,使用
} while(strncmp((const char*)userInput,(const char*)"a",1));
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 给定程序计数器,在共享库中查找源代码行
- C 中的单词计数器程序
- 程序执行时间计数器
- 用户输入的简单c++计数器程序
- 在堆栈程序中实现一个计数器,用于计算对象移动的次数
- 当计数器在循环中使用时,c++程序会崩溃
- 找出字计数器程序输出为0或1的原因