C++一些初学者错误
C++ few beginner errors
还
不是很擅长调试,但我遇到了一些错误。一些预期的"(")"和";"还有"else"没有前面的"if",与cout中的"运算符>>不匹配
我知道这很容易,但仍然试图让我的脚踏进门。谢谢:)
#include <iostream>
#include <cstdlib>
using namespace std;
int main() // guess number game
{
int x;
cout >> "Please enter a numbern";
getline(cin x);
int y = rand();
while x != y
{
if x < y;
cout >> "Go higher";
else;
cout >> "Go lower";
}
}
cout >> "Please enter a numbern";
这是错误的,std::ostreams
只提供插入格式化数据operator<<
。请改用cout << "Please enter a numbern";
。
getline(cin x);
首先,你缺少一个,
,因为getline需要两个或三个参数。但是,由于x
是一个integer
而不是一个std::string
它仍然是错误的。想想看 - 你能在整数中存储文本行吗?请改用cin >> x
。
int y = rand();
虽然这似乎没有错,但存在逻辑错误。 rand()
是一个伪随机数生成器。它使用种子作为起始值和某种算法(a*m + b
)。因此,您必须指定一个起始值,也称为种子。您可以使用 srand()
来指定此项。相同的种子将产生相同的数字顺序,因此请使用类似 srand(time(0))
.
while x != y
if x < y;
使用括号。并删除额外的;
.程序中;
杂散的分号类似于空表达式。
编辑: 工作代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(){
int x;
int y;
srand(time(0));
y = rand();
std::cout << "Please enter a number: ";
do{
if(std::cin >> x){
if(x < y)
std::cout << "Go higher: ";
if(x > y)
std::cout << "Go lower: ";
}
else{
// If the extraction fails, `std::cin` will evaluate to false
std::cout << "That wasn't a number, try again: ";
std::cin.clear(); // Clear the fail bits
}
}while(x != y);
std::cout << "Congratulations, you guessed my number :)";
return 0;
}
试试这个:
void main()
{
int x;
cout << "Please enter a numbern";
getline(cin, x);
int y = rand();
while(x != y)
{
if(x < y)
cout << "Go higher";
else
cout << "Go lower";
}
}
不太熟悉C++但我很确定 while/if 应该看起来像这样
while (x != y)
{
if (x < y)
cout << "Go higher";
else
cout << "Go lower";
}
if 和 while 循环的条件应用括号嵌套。
上面列出的所有内容都是语法错误。这可以通过阅读 c++ 的语法轻松修复
http://www.cs.duke.edu/csed/tapestry/howtoa.pdf
它应该看起来更像这样:
while (x != y)
{
if (x < y)
cout >> "Go higher";
else
cout >> "Go lower";
}
让我们看看所有错误:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() // guess number game
{
int x;
cout >> "Please enter a numbern"; // should be `cout <<` cin uses >>
getline(cin x); // incorrect number of arguments should be 2 or 3 plus x is an int not string could use cin instead
int y = rand();
while x != y // error condition checks should be parentheses like (x != y)
{
if x < y; // error condition should be parentheses also by adding a semicolon here you terminate the statement
cout >> "Go higher";
else; // technically not an error but unintended code is executed cout >> "Go higher" is always executed because of the semi colon ;
cout >> "Go lower";
}
// no return of value, you declared main to return an int so you should
}
试试这个:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() // guess number game
{
int x;
cout << "Please enter a numbern";
cin >> x;
int y = rand();
while (x != y)
{
if (x < y)
cout >> "Go higher";
else
cout >> "Go lower";
}
return 0;
}
相关文章:
- C++错误消息*成员参考.**初学者*
- 您好,我是C++初学者,有这种问题.它给了我倍增的错误
- 我是初学者,无法理解c ++程序中的以下错误
- 初学者的 if 语句语法错误
- 初学者C :为什么此开关语句给我一个错误
- 在C 中的初学者,我面临此问题错误C3867
- 将字符串转换为整数。为什么会出现此错误?我想将 ID(字符串)更改为 IC(整数)。两者都是数组。顺便说一句,我仍然是初学者
- 为什么尝试这样的类代码时会遇到错误?我初学者对不起
- 初学者问题 - 致命错误LNK1561:必须定义入口点
- C++初学者错误:主要表达式错误
- 错误:在没有参数列表的情况下无效使用模板名称“ExTree” ||如果解决这个问题怎么办?(初学者)
- C++初学者.获取代码中的错误
- 关于编译器错误的初学者问题
- 涉及向量的初学者代码,SEIGSEV运行时错误
- C++ 中初学者预期的类名错误
- C++继承和指针错误(初学者)
- 生锈/初学者C++编译器错误
- C++|主函数错误|初学者
- 计算这个字符序列给出的错误(初学者)
- 这里的未定义引用错误初学者