I’m doing the cs50 scrabble problems from probl set 2 and my ‘point’ function doesn’t add point correctly. the function keep ignoring all the ‘if’ and ‘else if’ statement even when there are char that corrospond with that ‘if’ statement.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int score1 = 0;
int score2 = 0;
int point(string ply, int score);
void compare(int pl1, int pl2);
int main(void)
{
string ply1 = get_string("Player 1: ");
string ply2 = get_string("Player 2: ");
point(ply1, score1);
point(ply2, score2);
point(ply1, score1);
printf("%cn", ply1[0]);
}
//calculate point
int point(string ply, int score)
{
for(int i = 0; i < strlen(ply); i++)
{
if(ply[i] == ('a'|'e'|'i'|'l'|'n'|'o'|'r'|'s'|'t'|'u'))
{
score += 1;
printf("yesn");
}
else if(ply[i] == ('d'|'g'))
{
score += 2;
}
else if(ply[i] == ('b'|'c'|'m'|'p'))
{
score += 3;
}
else if(ply[i] == ('f'|'h'|'v'|'w'|'y'))
{
score += 4;
}
else if(ply[i] == 'k')
{
score +=5;
}
else if(ply[i] == ('j'|'x'))
{
score += 8;
}
else
(
score += 10
);
}
return score;
}
//compare score
void compare(int pl1, int pl2)
{
if(pl1 < pl2)
{
printf("Player 2 won!n");
}
else if(pl1 > pl2)
{
printf("player 1 won!n");
}
else
{
printf("tien");
}
}
i run this in the cs50 debugger and when it came to the if function, it doesn’t add anything to the score but instead just skip over.
New contributor
Lanvo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.