5つの文字列のうち3つをランダムに選択して画面に表示する機能を考案しようとしています。ここに私がこれまで持っているものがあります。それは実行されますが、画面には何も印刷されません。C++ランダムに文字列を選択します
#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モード
私は前自動で働いたことはないのでは許可されていません「のため」範囲がベース。だから、これについての助けに感謝します。
なぜ、おお、なぜ、人々はそれが一生懸命ロバのボールを吸うとき、 'rand'を使用して主張します。すでに ''のものを使用してください。* please *。 –
これはコンパイルしないでください。この関数では、 'random'はループのローカルです。ループの外側に戻すことはできません。ループの外側で 'random 'を宣言し、ループの中から型を削除します。 – Carcigenicate
@JesperJuhl:おそらく ''には面倒なインターフェースがあり、 'rand()'はほとんどの人のケースでうまく動作するでしょう。 –