Need help getting the while statement to close the program when the user enters
ID: 3684064 • Letter: N
Question
Need help getting the while statement to close the program when the user enters q or Q as the word to be evaluated.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<conio.h>
int scrabbleValue(char* word)
{
int value = 0;
int scrabbleTot = 0;
int index = 0;
while (*(word + index) != '')
{
switch (toupper(*(word + index)))
{
case 'A':
case 'E':
case 'I':
case 'L':
case 'N':
case 'O':
case 'R':
case 'S':
case 'T':
case 'U':
value = 1;
break;
case 'D':
case 'G':
value = 2;
break;
case 'B':
case 'C':
case 'M':
case 'P':
value = 3;
break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
value = 4;
break;
case 'K':
value = 5;
break;
case 'J':
case 'X':
value = 8;
break;
case 'Q':
case 'Z':
value = 10;
break;
}
scrabbleTot += value;
*word++;
}
return scrabbleTot;
}
int main()
{
char *ptr;
char check;
int wordvalue;
char name[100];
printf(" Enter a word :");
scanf("%s", name);
ptr = name;
check = name[0];
wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d ", wordvalue);
while (!strcmp() || !strcmp(name, "Q"))
{
printf(" Enter a word :");
scanf("%s", name);
ptr = name;
wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d ", wordvalue);
}
return 0;
}
Explanation / Answer
In while loop !strmp() is not needed.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<conio.h>
int scrabbleValue(char* word)
{
int value = 0;
int scrabbleTot = 0;
int index = 0;
while (*(word + index) != '')
{
switch (toupper(*(word + index)))
{
case 'A':
case 'E':
case 'I':
case 'L':
case 'N':
case 'O':
case 'R':
case 'S':
case 'T':
case 'U':
value = 1;
break;
case 'D':
case 'G':
value = 2;
break;
case 'B':
case 'C':
case 'M':
case 'P':
value = 3;
break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
value = 4;
break;
case 'K':
value = 5;
break;
case 'J':
case 'X':
value = 8;
break;
case 'Q':
case 'Z':
value = 10;
break;
}
scrabbleTot += value;
*word++;
}
return scrabbleTot;
}
int main()
{
char *ptr;
char check;
int wordvalue;
char name[100];
printf(" Enter a word :");
scanf("%s", name);
ptr = name;
check = name[0];
wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d ", wordvalue);
while (!strcmp(name, "Q")) //!strcmp() is not needed here.
{
printf(" Enter a word :");
scanf("%s", name);
ptr = name;
wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d ", wordvalue);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.