我正在尝试设计一个函数,随机选择五个字符串中的三个并在屏幕上显示它们。这是我到目前为止所拥有的。它运行但没有任何东西打印到屏幕上。
#include "BoxOfProduce.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>
using namespace std;
BoxOfProduce::BoxOfProduce()
:choices{""}, bundles{""}
{
}
vector<string> BoxOfProduce::randomize()
{
srand(time(0));
string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
vector<string> random;
for(int i = 0; i < 3; i++)
{
random.push_back(choices[rand() % 5]);
}
return random;
}
#ifndef BOXOFPRODUCE_H
#define BOXOFPRODUCE_H
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class BoxOfProduce
{
public:
BoxOfProduce();
string getBundles();
void setBundles(string b);
vector<string> randomize();
private:
string bundles[3];
const string choices[5];
string random;
};
#endif // BOXOFPRODUCE_H
#include <iostream>
#include "BoxOfProduce.h"
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>
using namespace std;
int main()
{
srand(time(0));
BoxOfProduce bo;
bo.randomize();
auto vector<string> randomResult = bo.randomize();
for (const auto& result : randomResult){
cout << result << endl;
}
}
我现在更新了我的代码,但仍然没有打印输出。虽然我收到一个错误: 错误:C ++ 98模式中不允许使用基于范围的'for'循环
之前我从未使用 auto 。所以对此有任何帮助将不胜感激。
答案 0 :(得分:1)
您的代码不应该编译。 g ++发出以下错误:
return random;
error: could not convert ‘random’ from ‘long int (*)()throw ()’
random
变量是您身体的本地变量。你应该给它一个更大的范围:
string random;
for(int i = 0; i < 3; i++)
{
random = choices[rand() % 5];
}
return random;
要生成3个结果,您需要返回一个像
这样的字符串向量#include<ctime>
#include<cstdlib>
#include<string>
#include<memory>
#include<vector>
#include<iostream>
using namespace std;
std::vector<string> randomize()
{
srand(time(0));
string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
std::vector<string> random;
for(int i = 0; i < 3; i++)
{
random.push_back(choices[rand() % 5]);
}
return random;
}
int main()
{
srand(time(0));
randomize();
std::vector<string> randomResult = randomize();
for (std::vector<string>::const_iterator iter = randomResult.begin(), iterEnd = randomResult.end();
iter != iterEnd; ++iter)
cout << *iter << endl;
return 0;
}
答案 1 :(得分:0)
您提供的代码甚至不应该编译。试试这个:
string BoxOfProduce::randomize()
{
srand(time(0));
string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
string random;
for(int i = 0; i < 3; i++)
{
random = choices[rand() % 5];
}
return random;
}
int main()
{
srand(time(0));
BoxOfProduce bundle1;
bundle1.randomize();
cout << bundle1.randomize() << endl;
}
大括号{ }
定义范围。在该范围之后,您在其中定义的变量将被销毁。因此,您应该在启动该范围之前定义该变量。
编译的原因可能是你已经在类string random
的主体内部有另一个变量BoxOfProduce
。因此,您可以像我一样在范围之外定义string random
,或者只是在string
之前删除random
,然后编译器将使用类主体中的变量。
答案 2 :(得分:0)
string random
的范围仅在代码中的for循环内
试试这个:
string BoxOfProduce::randomize()
{
srand(time(0));
string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
string random = "";
for(int i = 0; i < 3; i++)
{
random = choices[rand() % 5];
}
return random;
}
int main()
{
srand(time(0));
BoxOfProduce bundle1;
bundle1.randomize();
cout << bundle1.randomize() << endl;
}