#include <iostream>
#include <string>
using namespace std;
int score(string s);
char scrabbleLetters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int scrabblePoints[] = {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};
int main()
{
string sWord;
cout << "Enter the scrabble word you'd like to score.";
cin >> sWord;
cout << "You scored " << score(sWord)<< " points for that word!";
}
int score(string s)
{ int points = 0;
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < scrabbleLetters.length(); j++)
{
if (s[i] == scrabbleLetters[j])
points += scrabblePoints[j];
}
}
return points;
}
我似乎无法弄清楚为什么我的代码没有编译。该程序应该向用户询问一个单词,然后根据每个字母的点数对单词进行评分。拼字游戏点计数器C++
我收到的当前错误是:“错误:'scrabbleLetters'中的成员'length'的请求,它是非类类型'char [26]'|”
堆栈段并不意味着对C++,仅供参考。 – 2014-11-05 20:51:06
这个用例真的需要'std :: map'而不是保留两个平行的数组。 –
CoryKramer
2014-11-05 20:51:43
您似乎将Java与C++混为一谈。重新阅读你的教科书介绍数组的章节。它应该包含演示如何在循环中使用它们的代码。 – 2014-11-05 20:52:31