ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello again,
this is some sort of continuation of a thread I marked as solved a few days ago.
Asking other people and searching the web, i put up together a few more lines to help me inputting some co-ordnates on terminal to locate a word placed in a grid.
Sadly, and contrarily to what i was suggested in the other thread, I am still using an IDE, (KDevelop) as I find it useful for a newbie to locate errors much quicker and at least visualize their location/s.
Now, I came across something really peculiar.
I found out that "_strncmp" is a windows-specific-function whereas "strncasecmp" is POSIX compliant.
Both give me a compile error and the program doesn't execute (it does on VSCode).
But the peculiar thing is that the same function is called in other lines and KDevelop does NOT give an error (I attached a scrnshot to better visualize my query).
Would anyone be so kind to explain why, in a language that a 6yr old could understand?
Thanks
I don't use VScode but there is a c function called strncmp
Code:
int res;
res=strncmp(c1, "exit",4);
if (res==0) // strings match.
...
//for case insensitive. Since the string needs to match exactly so the \n is added.
res=strcasecmp(c1,"exit\n");
if ( res==0)
...
I would keep the play loop in main but I am not writing the program...
I found out that "_strncmp" is a windows-specific-function whereas "strncasecmp" is POSIX compliant.
Both give me a compile error and the program doesn't execute (it does on VSCode).
Do you have #include <strings.h>? You need that for strncasecmp.
Wow! thank you. That looks like a sneaky trick that I would have discovered in months. I guess, by "beginning of the program" you mean with the bunch of "#include<>" stuff, correct?
The following lines (which altogether, ended up being more than 300 and not the 100ish as I was hoping for initially) should the job as I tested the playGame function and it works finding the coordinate of a word. However, when i put it in the whole program it does not behave as it did earlier when i tested it alone.
Could anyone tell me where I have messed things up somewhere?
Code:
// Changing coordinate values to integers
int getIntValue(char x)
{
return x - '0';
}
int getCharIntValue(char x)
{
return toupper(x) - 'A';
}
//Check if the word correct
int checkIfWin(char wordFound[], int win)
{
int i;
for (i = 0; i < 4; i++)
{
if (strncmp(wordFound, fourWords[i], strlen(wordFound)) == 0)
{
win++;
}
}
return win;
}
void playGame()
{
char c1[5], c2[5];
char wordFound[10] = " ";
int i, j, k, l, m, d, x, win = 0;
do
{
printf("\nEnter first coordinate and press Enter: ");
fgets(c1, 5, stdin);
if (strncmp(c1, "EXIT\n", 4) == 0 || strncmp(c2, "EXIT\n", 4) == 0)
{
// WordSearch();
}
printf("Enter last coordinate and press Enter: ");
fgets(c2, 5, stdin);
if (strncmp(c1, "EXIT\n", 4) == 0 || strncmp(c2, "EXIT\n", 4) == 0)
{
// WordSearch();
}
if (c1[1] == c2[1])
{
// Horizzontal Word
d = 0;
memset(wordFound, 0, sizeof(wordFound));
for (i = getCharIntValue(c1[0]); i <= getCharIntValue(c2[0]); i++)
{
wordFound[d] = puzzle[getIntValue(c1[1])][i];
d++;
}
printf("Word Found is '%s'", wordFound);
win = checkIfWin(wordFound, win);
printf("\nWords found are: %d\n", win);
}
else if (getCharIntValue(c1[0]) == getCharIntValue(c2[0]))
{
// Vertical Word
d = 0;
memset(wordFound, 0, sizeof(wordFound));
for (i = getIntValue(c1[1]); i <= getIntValue(c2[1]); i++)
{
wordFound[d] = puzzle[i][getCharIntValue(c1[0])];
d++;
}
printf("Word Found is '%s'", wordFound);
win = checkIfWin(wordFound, win);
printf("\nWords found are: %d\n", win);
}
else
{
j = getCharIntValue(c1[0]);
k = getIntValue(c1[1]);
l = getCharIntValue(c2[0]);
m = getIntValue(c2[1]);
d = 0;
x = 0;
memset(wordFound, 0, sizeof(wordFound));
for (i = j; i <= l; i++)
{
wordFound[d] = puzzle[k + x][j + x];
d++;
x++;
}
printf("Word Found is '%s'",wordFound);
win = checkIfWin(wordFound, win);
printf("\nAmount of words found is: %d\n", win);
}
}
while (win != 4);
printf("\nThe End!\n");
return ;
}
//Create a user-interactive menu in normal/console colours
void mainMenu()
{
printf("\033[0m");
char menuChoice;
do
{
printf("\n~~~ DAILY CROSSWORD ~~~\n");
printf("1. Play\n");
printf("3. Exit\n");
printf("\nWhat would you like to do: \n");
while((menuChoice = getchar()) == '\n');
switch (menuChoice)
{
case '1': displayPuzzle();
printf("\n------------------------");
printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
printf("------------------------\n");
//printf("Enter your co-ordinate\n");
playGame();
break;
}
} while (menuChoice != '3');
}
int main(int argc, char *argv[])
{
srand(time(NULL));
createBlankPuzzle();
displayPuzzle();
fillPuzzleWithWords();
displayWords();
mainMenu();
getchar();
getFourRandomWords();
printf("Thank you for playing today! :)\nGood Bye\n");
return 0;
}
Thank you
To be completely honest, I did not write all of the above (maybe only 25% to adapt it to Linux compiler and change some whistles&bells) but it seems to work OK on VSCode
case '1': displayPuzzle();
printf("\n------------------------");
printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
printf("------------------------\n");
//printf("Enter your co-ordinate\n");
while ((getchar()) != '\n'); //clear buffer
playGame();
break;
case '1': displayPuzzle();
printf("\n------------------------");
printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
printf("------------------------\n");
//printf("Enter your co-ordinate\n");
while ((getchar()) != '\n'); //clear buffer
playGame();
break;
Awesome! thanks a lot that worked like a charm (although I discovered more bugs thanks to it).
On a different note, I can start to see the power of coding, especially in C or C++ but I wish i had started earlier. The learning curve is steeper than climbing the Everest.
When I fix one thing, there are another 2 broken.
Thanks everyone.
edit: ignore my comment. It works nicely now. I was entering the wrong commands (Coord.1 and coord. 2 [enter] instead of coord.1[enter] then coord.2[enter]. Proud of my(ish) very 1st C program
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.