2016-09-04 20 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; 

} 

括弧を{ }スコープを定義します。そのスコープの後、内部で定義する変数はが破棄されたです。したがって、そのスコープを開始する前にその変数を定義する必要があります。

これはおそらく、別の変数string randomがクラスBoxOfProduceの本体の内部にあるということです。したがって、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; 

} 
関連する問題