2016-03-02 30 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 –