c++ - 选择两个字符串之一输出?

标签 c++

我想知道如何让程序随机输出两个字符串之一。 很难解释,但这是我的例子:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string age;

cout << "How old are you?" << endl;
cin >> age;

if (age < 0)
{
cout << "Invalid age" << endl;
}
     if (0 >= age && age <<= 3)
     {
     cout << "Oh you're just a baby" << endl;
     //               OR (random)
     cout << "Time to take a nap!" << endl;
     }

return 0;
}

我想编程输出“哦,你只是个婴儿”或“该睡午觉了!”每当用户输入 0 到 3 之间的数字时随机出现。有人可以解释一下吗?

最佳答案

尝试:

#include <cstdlib>
...
...
...
/* initialize random seed: */
srand(time(NULL));

/* generate secret number: */
int ss = rand() % 2;
if (ss) {
    cout << "Oh you're just a baby" << endl;
} else {
    cout << "Time to take a nap!" << endl;
}

关于c++ - 选择两个字符串之一输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513875/

相关文章:

c++ - 如何将 c 数据类型转换为 neon 数据类型

c++ - 获取返回的 multimap 迭代器的内容

javascript - 蜘蛛猴: Why JS array is not inheriting default properties like length,拼接等

c++ - QRegExp 用于从代码中提取单行注释

c++ - 如何转换嵌套的 C++11 绑定(bind)表达式

c# - 调用 SysFreeString() 时出现堆损坏错误

c++ - 类的对象(使用单/多继承)有多少个 vptr?

c++ - 来自数组的随机字符串

c++ - 斐波那契堆的 STL?

c++ - ODBC Execute/Fetch of SQL 2005 存储过程结果集不能使用表@variable