嘿,大家我試圖讓一個數組處理矢量,所以我的代碼每次運行程序時都會停止出現字母「word」。我認爲我需要用矢量做些事情,但我已經閱讀了一些指南,但是如果有人能夠幫助我完成那些很棒的步驟,我會感到非常困惑? :)如何聲明一個具有向量的數組
編輯:基本上我試圖讓我的向量與playGame()函數一起工作;這樣我可以顯示不同的詞,而不是僅僅具有相同的字,每次又名「字」
這裏是我當前的代碼上來的:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int playGame(string word);
string array[]= { "apple", "banana", "orange", "strawberry" };
vector<string> word (array, array+4);
int main()
{
int choice;
bool menu = true;
do{
cout <<"Please select one of the following options: \n";
cout << "1: Play\n"
"2: Help\n"
"3: Quit\n";
cout << "Enter your selection (1, 2 and 3): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
switch (choice)
{
case 1:
cout << "You have chosen play\n";
//int playGame(string word);
playGame("word");
break;
case 2:
cout << "You have chosen help\n";
cout << "Here is a description of the game Hangman and how it is played:\nThe word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
break;
case 3:
cout << "You have chosen Quit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 3!\n";
}
}while(choice!=3);
getchar();
getchar();
cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;
}
請更具體。你想通過「使用矢量獲得數組」來達到什麼目的?此外,請只顯示*相關*部分代碼。像這樣這是一個猜謎遊戲,你可能意味着什麼 – codeling
這是否更有幫助?對不起,我是新手,想弄清楚哪些代碼是相關的。 – user2857301
你的代碼'vector word(array,array + 4);'是聲明一個數組副本的向量的正確方法。所以如果你有麻煩,那麼它一定是在別的地方。我根本不清楚的是爲什麼你認爲從數組切換到矢量會有所幫助。也許你會解釋爲什麼你會這麼想。 –
john