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()'가 대부분의 사람들에게 잘 작동하기 때문일 것입니다. –