Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[] = {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 compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
    if (score1>score2)
    {
        printf("Player 1 Wins!");
    }
    else if (score1<score2)
    {
        printf("Player 2 Wins!");
    }
    else
    {
        printf("Tie!");
    }
}

int compute_score(string word);

    // TODO: Compute and return score for string

    int score =0;

    for i ( int i =0; i<strlen(word); i++)
    {
        if (isupper(word[i]))
        {
            score = score + POINTS[word[i]-65];
        }

        if (islower(word[i]))
        {
            score = score + POINTS[word[i]-97];
        }
        return score;

    }


What I have tried:

i have tried adding the bracket in different places but nothing seems to happen
the error is ;
scrabble.c:42:5: error: expected identifier or '('
    for i ( int i =0; i<strlen(word); i++)
    ^
1 error generated.
make: *** [<builtin>: scrabble] Error 1
scrabble/ $ 
Posted
Updated 10-Oct-22 3:31am
v2
Comments
PIEBALDconsult 10-Oct-22 20:44pm    
It may not matter in this particular application, but performance-wise it's better not to call strlen in such a loop.

Quote:
scrabble.c:42:5: error: expected identifier or '('
for i ( int i =0; i<strlen(word); i++)
="" ^
1="" error="" generated.
make:="" ***="" [<builtin="">: scrabble] Error 1
scrabble/ $

You might want to look at that for statement more closely. Go lookup the for loop in C[^] and read it. Look at the format of the statement and what you did wrong is glarily obvious.
 
Share this answer
 
Comments
CPallini 10-Oct-22 14:17pm    
5.
First off, that's not C - or at least not entirely standard C - or C#. It could be C++ which does have the std::string type, but C doesn't.
So start off by working out which language that is, because that's really quite important!

Secondly, when it comes to syntax errors it's important to look at all the information - starting with "what line in my code is throwing up the error?".
And we don't have that info, and since that won't compile as C, C#, or even C++ using an online compiler and we have no idea what you are using we can;t help you fix the problem directly.

It's also important to note that you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

Quote:
i had removed that i that time only when i posted this because i realised it

but now its
#for (int i = 0; i < strlen(word); i++)#
and still no clue i dont see any clear error here please help


If you had followed the link I gave you, it should have been pretty obvious at some point over the last month ...

Look at the whole function declaration that the for loop is part of, particularly the function definition line. What's wrong here:
C
int compute_score(string word);

    // TODO: Compute and return score for string

    int score =0;

    for i ( int i =0; i<strlen(word); i++)
    {
Do you see the problem?

I do.
Hint: what does a semicolon do?

Honestly, you will get syntax errors all the time: you have wasted a month on a semicolon that you should be able to spot yourself! Follow the link, and think about error messages - they really are trying to help you. :D
 
Share this answer
 
v2
Comments
Member 15792913 10-Oct-22 8:50am    
hey i did try that it is not getting better i tried all methods but still the error is showing i have updated with what error is coming
OriginalGriff 10-Oct-22 9:26am    
And if you look at the line it's complaining about, how does that differ from the expected C for loop syntax?
Hint: What's the "i" doing there?

Seriously, try to fix your own syntax errors - it'll save you huge amounts of time in the future! The link I gave you explains how.
Member 15792913 27-Nov-22 5:29am    
i had removed that i that time only when i posted this because i realised it

but now its
#for (int i = 0; i < strlen(word); i++)#
and still no clue i dont see any clear error here please help
OriginalGriff 27-Nov-22 5:55am    
Answer updated.
In addition to what others have said, the second occurrence of this
C++
int compute_score(string word);
needs to define (that is, implement) this function, not declare it. You've already declared it before main, so now it needs to be
C++
int compute_score(string word)
{
   // your code
}
Assuming that this is C++, a string is an object, and two things that you should do whenever possible are
(a) pass an object parameter by reference, to avoid the overhead of creating a copy of it: string& word
(b) make a parameter const if a function doesn't change it: const string word

Both of those things apply here, so a better signature for this function is
C++
int compute_score(const string& word)
{
   // your code
}
Also, strlen(word) isn't going to work: strlen is for C strings (char*), not C++ strings. You want word.size() instead.
 
Share this answer
 
v2
Your code appears to be pure C rather than C#. So, there is no such type as string in C, you need to use char*. You also have not coded the get_string function.
 
Share this answer
 
v2
Comments
OriginalGriff 10-Oct-22 8:37am    
I suspect a copy'n'paste'n'hope exercise is going on here ... :D
Richard MacCutchan 10-Oct-22 8:40am    
Most likely; but to be fair, most of the logic looks OK.
CPallini 10-Oct-22 14:20pm    
THe OP is using the CS50 library, see
https://manual.cs50.io/3/get_string
Richard MacCutchan 11-Oct-22 3:42am    
Thanks, I had never heard of that.
CPallini 11-Oct-22 4:01am    
Neither did I. :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900