2016-03-02 32 views
-2

我必須使用2個數組做一個拼字遊戲得分遊戲。第一個數組保存用戶輸入的單詞,第二個數組保存每個字母的值。最後需要一個函數來計算分數。我無法將用戶值分配給第二個數組以獲得分數並獲得該函數的正確代碼。c + +使用數組的拼字遊戲

#include <iostream> 
#include <stdlib.h> 
#include <cmath> 
#include <ctime> 

using namespace std; 

int scoreCalculator(int total); 
char userWord; 
int points; 
int total=0; 
int main() 
{ 

char userWord[11]; 
for (int i=0; i<11; i++) 
{ 
    userWord[i]='\0'; 
} 

cout<<"Enter your word less than 10 letters: "<<endl; 
cin>>userWord; 
cout<<"Here is the word you inputted: "<<userWord<<endl; 

int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; 

for(int j=0; j<26; j++) 
{ 
    userWord[i] 
} 

cout<<"Here is your score: "<<scoreCalculator(total)<<endl; 

} 
int scoreCalculator(int total) 
{ 

total+=scrabblePoints[j]; 

} 

這是我到目前爲止並在那裏停留在

+1

IM和什麼是您所遇到的麻煩嗎? – NathanOliver

+1

請確保在發佈代碼時確保代碼的縮進和可讀性正確。 – crashmstr

+3

請重新考慮你對壞習慣的使用['using namespace std;'](http://stackoverflow.com/q/1452721/1171191)和['endl'](http://chris-sharpe.blogspot.co。英國/ 2016/02 /爲什麼 - 你 - 不應該使用-stdendl.html)。 – BoBTFish

回答

0
#include <iostream> 

int main() 
{ 
    std::string input; 
    std::cin>>input; 

    // fill this with scrable values 
    int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; 

    int ans = 0; 
    for(int i=0;i<input.size();i++) 
    { 
     ans += scrable_values[input[i]-97]; 
    } 
    return ans; 
} 
+0

從每個字符的asci值中減去97,可以將它編入數組中。 –

+0

從* ASCII *值中減去'a'將使索引爲零。請注意使用使字符易於閱讀的字符字面值。 –

+0

雖然我不知道。我減去數字比減去字符更容易。此外,通過這種方式可以保存一個角色。#codeGolf –