2016-09-04 3 views
-1

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 모드

내가 전에 자동 함께 일한 적이을 사용할 수 없습니다 '에 대한'범위를 기반. 그래서 이것에 대한 도움을 주시면 감사하겠습니다.

+3

왜, 왜 오, 사람들이 그렇게 열심히 당나귀 공을 짜증 때'rand'를 사용하여 주장 할. 이미' '을 사용하십시오 - * please *. –

+3

컴파일하지 않아야합니다. 이 함수에서'random'은 루프에 국한되어 있습니다; 그것은 루프 외부로 리턴 될 수 없어야합니다. 루프 외부에서'random '을 선언하고 루프 내부에서 유형을 제거하십시오. – Carcigenicate

+0

@JesperJuhl : 아마도''이 성가신 인터페이스를 가지고 있고'rand()'가 대부분의 사람들에게 잘 작동하기 때문일 것입니다. –

답변

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; 
} 
+0

이것은 절반의 문제에 불과합니다. 이것은 여전히 ​​그들이 원하는 것을하지 않을 것입니다. 그들은 3 개의 값을 반환하기를 원하지만 루프는 현재 각 반복에서 이전 값을 덮어 씁니다. – Carcigenicate

+0

@Carcigenicate 당신 말이 맞습니다. 나는 당신의 제안을 추가합니다. – Franck

+0

여전히 콘솔에 아무 것도 인쇄하지 않습니다. 그냥 빈 콘솔. – Codet

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; 

} 

. 해당 범위 다음에 정의한 변수는 이며,이 값은입니다. 따라서 해당 범위를 시작하기 전에 해당 변수를 정의해야합니다.

아마도 컴파일 된 이유는 BoxOfProduce 클래스 본문에 다른 변수 string random이 이미 있다는 것입니다. 따라서 내가 한 것처럼 string random을 범위 밖에 정의하거나 random 전에 string을 제거한 다음 컴파일러에서 클래스 본문의 변수를 사용합니다.

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; 

} 
관련 문제