注册 登录
编程论坛 C++教室

新手小白求助下,不知道错哪了

wl0421 发布于 2022-07-31 19:42, 1122 次点击

随机生成5道10以内的加法题目,错误提示是: error: ‘end1’ was not declared in this scope
具体代码如下

#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
int main()
{
    srand(time(0));
    cout<<rand()%10<<"+"<<rand()%10<<"="<<end1;
    cout<<rand()%10<<"+"<<rand()%10<<"="<<end1;
    cout<<rand()%10<<"+"<<rand()%10<<"="<<end1;
    cout<<rand()%10<<"+"<<rand()%10<<"="<<end1;
    return 0;
}
3 回复
#2
chenyucheng2022-07-31 20:37
记住,是endl!最后一个是L的小写,而不是阿拉伯数字1
#3
chenyucheng2022-08-01 10:52
修改后代码
以下是引用chenyucheng在2022-7-31 20:37:57的发言:

记住,是endl!最后一个是L的小写,而不是阿拉伯数字1!

程序代码:
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
int main()
{
    srand(time(0));
    cout<<rand()%10<<"+"<<rand()%10<<"="<<endl;//是字母l不是数字1
    cout<<rand()%10<<"+"<<rand()%10<<"="<<endl;//是字母l不是数字1
    cout<<rand()%10<<"+"<<rand()%10<<"="<<endl;//是字母l不是数字1
    cout<<rand()%10<<"+"<<rand()%10<<"="<<endl;//很明显提示"end1",到底有没有认真学
    return 0;
}


[此贴子已经被作者于2022-8-1 10:54编辑过]

#4
rjsp2022-08-01 13:10
我来说个题外话,既然用C++了,那就别用 srand(time(0)) 等

程序代码:
#include <random>
#include <iostream>
using namespace std;

 
int main( void )
{
    std::random_device rd;    // 真随机数引擎
    std::mt19937 gen( rd() ); // 梅森旋转算法
    std::uniform_int_distribution<> dis( 0, 9 ); // 离散均匀分布

    for( size_t i=0; i!=4; ++i )
        cout << dis(gen) << " + " << dis(gen) << " =\n";
}


std::random_device 是一种随机数引擎。真随机数引擎的缺点是 资源比较珍贵,所以一般只用它来设置随机数算法的种子。而你的 time(0) 可并不随机,种子不随机的话,一切都是虚谈。
梅森旋转算法 是一种非常优秀的伪随机数引擎,如果你觉得它还不够,可以用 mt19937_64。比 rand 要强太多。
uniform_int_distribution 是一种均匀概率的分布器。如果你自己设计的话,要考虑很多东西。比如你的 rand()%10 就完全不是 均匀概率,假如你的 RAND_MAX==32767,那么 rand%10 的结果中 0到7的几率 就比 8到9的几率 要高。
1