2013-05-02 74 views
-5

如何在代碼中插入rand/srand使其以隨機順序提出問題? 我對rand和srand不是很熟悉。我需要的是將srand和/或rand放入開關盒中,但無法理解如何使其工作。如何實現rand/srand? C++

#include <iostream> 
    #include <iomanip> 
    #include <fstream> 
    #include <cstdlib> 
    #include <string> 
    #include <ctime> 
    using namespace std; 
    struct highscore{ 
    int svar; 
    }; 


    int main(){ 
     ofstream highscore;    
bool fail = true; 
int lost, fraga, svar = 0; // these are for the Start option 
int fraga2; // for srand/rand 
int load ,loadh, loadg, tillbaka; // these are for the loading option 
string line; // also loading option 
char val; 

do{ 
    cout << " Menu" << endl; 
    cout << " 1. Start" << endl; 
    cout << " 2. Load" << endl; 
    cout << " 3. Credits" << endl; 
    cin >> val; 
    switch(val){ 

      case '1': 

        highscore.open("Highscore.txt", ios::app); 
        if (!highscore){ 
        cout << "file could not open"; 
        exit (1); 
        } 
        srand(fraga2(time(0)); 
        for (int fraga = 0; fraga< 500; fraga++){ 
        cout << "WELCOME TO THE CISCO QUIZ GOOD LUCK" << endl; 
        cout << "how many layers are there in the OSI model?" << endl; 
        cout << "1. 4" << endl; 
        cout << "2. 5" << endl; 
        cout << "3. 7" << endl; 
        cin >> fraga; 
        switch(fraga){ 
            case 1: 

             cout << "Game Over!" << endl; 
             cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
             cin >> lost; 
             if(lost == 2){ 
               fail = false; 
               } 
             break; 

            case 2: 

             cout << "Game Over!" << endl; 
             cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl; 
             cin >> lost; 
             if(lost == 2){ 
               fail = false; 
               } 
             break; 

            case 3: 
             svar = svar + 5; 
             fraga = fraga + 1; 
             break; 
            default: 
            cout << "error" << endl; 
             } 

             if(svar == 5){ 
              cout << "what is the full name of OSPF?" << endl; 
              cout << "1. Open Shortest Path First" << endl; 
              cout << "2. Online Shooting Pro Fishing" << endl; 
              cout << "3. On Short Path First" << endl; 
              cin >> fraga; 
              switch(fraga){ 

                  case 1: 
                  svar = svar + 5; 
                  fraga = fraga +1; 
                   break;                    

                  case 2: 
                   cout << "Game Over!"<< endl; 
                   cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl; 
                   cin >> lost; 
                   if(lost == 2){ 
                    fail = false; 
                    } 
                   break; 


                  case 3: 
                   cout << "Game Over!" << endl; 
                   cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl; 
                   cin >> lost; 
                   if(lost == 2){ 
                    fail = false; 
                   } 
                   } 
                   }   
        if(svar == 10){ 
        cout << "What does TTL mean?" << endl; 
        cout << "1. Twin Twitched Life" << endl; 
        cout << "2. Two Time Lives" << endl; 
        cout << "3. Time To Live" << endl; 
        cin >> fraga;         
         switch(fraga){ 
         case 1: 
          cout << "Game Over!" << endl; 
          cout << "Do you wish to restart? (answer with yes, YES or Yes)"<< endl; 
          cin >> lost; 
            if(lost == 2){ 
            fail = false; 
             } 
         break; 


         case 2: 
         cout << "Game Over!" << endl; 
         cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
         cin >> lost; 
          if(lost == 2){ 
           fail = false; 
            } 
         break; 


         case 3: 
         svar = svar + 10; 
         fraga = fraga +1; 
         break; 
         } 
         } 
         if(svar == 20){ 
           cout << "What is the first command you enter on a Cisco router?" << endl; 
           cout << " 1. enable"<< endl; 
           cout << " 2. disable" << endl; 
           cout << " 3. enter" << endl; 
           cin >> fraga; 

           switch(fraga){ 
              case 1: 
               svar = svar + 5; 
               fraga = fraga +1; 
               break; 

              case 2: 
               cout << "Game Over!" << endl; 
               cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
               cin >> lost; 
                if(lost == 2){ 
                 fail = false; 
                   } 
               break; 
              case 3: 
               cout << "Game Over!" << endl; 
               cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
               cin >> lost; 
                if(lost == 2){ 
                fail = false; 
                   } 
               break; 
               }           
               } 
               cout << svar; 
            if(svar == 25){ 
              cout << "which protocol is the most prefered?" << endl; 
              cout << " 1. RIP" << endl; 
              cout << " 2. EGRP" << endl; 
              cout << " 3. EIGRP" << endl; 
              cin >> fraga; 
              switch(fraga){ 
                 case 1: 
                   cout << "Game Over!" << endl; 
                   cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
                   cin >> lost; 
                    if(lost == 2){ 
                     fail = false; 
                       } 
                   break; 


                 case 2: 
                   cout << "Game Over!"<< endl; 
                   cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl; 
                   cin >> lost; 
                   if(lost == 2){ 
                    fail = false; 
                      } 
                   break; 


                 case 3: 
                  svar = svar + 15; 
                  fraga = fraga +1; 
                  cout << "You have completed the game press 1 to exit or if you wish to do it again or check highscores press 2" << endl; 
                  cin >> lost; 
                  if (lost == 1){ 
                     fail = false; 
                     } 
                  else{ 
                    fail = true; 
                    } 
                  break; 
                 } 
                } 
               }               
               highscore <<"Score: " << svar << '\n';     
              highscore.close(); 
              cin.ignore(10,'\n'); 

              cin.get(); 

    case '2': 
     cout << "type 1 to load game or type 2 to load highscores" << endl; 
     cin >> load; 
     if (load == 1){ 
       cout << "in progress"; 
       } 
     else if(load == 2){ 
      ifstream loadh ("highscore.txt"); 
      if (loadh.is_open()) 
      { 
       while (loadh.good()) 
       { 
       getline (loadh,line); 
         cout << line << endl; 
        } 
        cout << "do you wish to return to the menu press 1 else press any number" << endl; 
        if (tillbaka != 1){ 
           fail = false; 
           } 

       highscore.close(); 
           } 
      } 
      cin.ignore(10,'\n'); 

cin.get(); 
     break; 

    case '3': 
     cout << "made by trickjay the most awesome guy on the planet!!!" << endl; 
     cin.ignore(10,'\n'); 

     cin.get(); 
     break; 



    } 

    }while(fail); 
    } 
+1

也許你應該閱讀這些功能的文件,如果你不知道他們在做什麼。 – 2013-05-02 08:05:43

+0

首先閱讀手冊。 – 2013-05-02 08:05:54

+1

閱讀本手冊後,製作一個小程序,顯示您試圖實現的目標。然後,將其合併到您的代碼中。如果您對小程序有特殊疑問,請回來。 – stefaanv 2013-05-02 08:15:11

回答

0
int num; 
for(int i=0; i<3 ; i++) { 
    num = rand() % 3; 
    switch(num) { 
    case 0: askone(); break; 
    case 1: asktwo(); break; 
    case 2: askthree(); break; 
    } 
} 

這將給予相同的順序每次程序運行。如果你想要更少的確定性,請檢查種子。你也可以添加一些機制來避免重複的問題。

無關,您應該考慮減少程序中的縮進。例如,通過將一些子任務放入函數中。

此外,您可以將外部的do-while循環更改爲正常的while循環,而無需更改程序行爲。它會使你的代碼更清晰。

+0

好的,謝謝,我會嘗試 – TrickJay 2013-05-02 09:24:39

0

如果您使用的是支持C++ 11,那麼你有新的random方法編譯:

std::random_device rd; 

std::mt19937 e2(rd()); 
std::uniform_int_distribution<int> dist(0, 2); 

int num; 
for(int i=0; i<3 ; i++) { 
    num = dist(e2); 
    switch(num) { 
    case 0: askone(); break; 
    case 1: asktwo(); break; 
    case 2: askthree(); break; 
    } 
} 

對於這種情況,可能不會有很大的差異,但在長期短期內不使用std::rand有好處,因爲它不是線程安全的,並且不保證rand生成的隨機序列的質量。

+0

好吧,我會查找它,看看哪個最適合代碼。 – TrickJay 2013-05-02 09:48:17

2
 
// Returns 100% guaranted random number based on timestamp 
int randNumber() { 
    return 213213; 
} 

您還可以實現像

 
// This works too 
int randNumber() { 
    return rand(); 
} 
+0

好的答案!我認爲這一個值得接受更多...... – holtaf 2014-02-25 11:43:32