# Do-while loop-> exit not working (making a crossword puzzle)

• Comp Sci

Making crossword

## Relevant Equations:

-
Hello!
My homework is making a crossword.
I write a code like this:

C:
do{
//code
printf("Please enter the coordinate and the word: \n");
scanf(" %c%d %s",&row2,&column2,word2);
if(row2=='E' && word2=="xit") exit(0);
//code
}while(count2!=10);
But this is not work. Count2 increase when the word is found. I asked the user write command like this-> E7 hello, and I check the word. If the user write Exit, I want to exit the program.

Last edited by a moderator:

Related Engineering and Comp Sci Homework Help News on Phys.org
FactChecker
Gold Member
Use strcmp to compare strings. Using '==' does not do what you think it does. It compares addresses.

berkeman and anonim
Thanks for your answer. I tried, but still it does not work.

Please put your code inside [code /code] tags, and show the actual input and output -- saying that it doesn't work is not sufficient if you want us to be of assistance in diagnosis and correction regarding a problem.

Last edited:
phinds
berkeman
Mentor
(I added code tags to the OP)

sysprog
I think that you need to import string.h to use strcmp. Not sure, though.

Mark44
Mentor
I think that you need to import string.h to use strcmp.
Yes, but technically you include this header.
C:
#include <string.h>

Please put your code inside [code /code] tags, and show the actual input and output -- saying that it doesn't work is not sufficient if you want us to be of assistance in diagnosis and correction regarding a problem.
You are right. I don't use this place too much. Please forgive me for my inexperience. :)

(I added code tags to the OP)
Thanks:)

berkeman
I think that you need to import string.h to use strcmp. Not sure, though.
I tried to use strcmp. When I used this, I get a segmentation fault.

Yes, but technically you include this header.
C:
#include <string.h>
Yes, string.h is written my code. I tried to use strcmp. When I used this, I get a segmentation fault.

berkeman
Mentor
Yes, string.h is written my code. I tried to use strcmp. When I used this, I get a segmentation fault.
Can you post your updated code, the input, and the error you are seeing?

sysprog
You are right. I don't use this place too much. Please forgive me for my inexperience. :)
Welcome aboard PF, @anonim -- please don't be too concise when you're trying to fix something in a program -- we need the input, the code, and the output -- thanks.

Yes, but technically you include this header.
C:
#include <string.h>
Yes. Java has taken over my mind for now.

Joshy
Gold Member
If you're okay with using string.h, then I would recommend taking a look at the strcmp function. There is still something slightly less trivial about using it compared to Boolean operations.

If you're stuck I would recommend taking a look at something like this:

C:
char[] myString = "hat";
printf("True results of strcmp: %d\n", strcmp(myString, "hat"));
printf("False results of strcmp: %d\n", strcmp(myString, "cat"));
printf("Boolean: %d\n", strcmp(myString, "hat"));

If your class doesn't allow string.h, then I would go for a loop and compare each index. Have it loop through the string of characters and if all of the characters match one by one make another variable something like stringChecker and set it to true or false; use it as your while condition.

sysprog
Mark44
Mentor
From post #1:
C:
do{
//code
printf("Please enter the coordinate and the word: \n");
scanf(" %c%d %s",&row2,&column2,word2);
if(row2=='E' && word2=="xit") exit(0);
//code
}while(count2!=10);
I recommend that you change this loop completely, as you're trying to do too many things with one call to scanf(). Instead of trying to input the row, column, and word, or the string "Exit" all with one call to scanf(), consider doing something like this:
C:
do{
printf("Please enter the coordinate and the word: \n");
scanf(" %c%d %s",&row2,&column2,word2);
//code to process the word at the specified location
printf("Continue? Type Y or y ";
response = getchar();
}while(toupper(response) == 'Y');
Another problem with your code, besides the already mentioned fact that you can't compare strings for equality, is that if a user types "exit" or any other variation of "Exit", your logic fails.

For toupper(), you'll need to #include <ctype.h>.

sysprog
Klystron
Gold Member
Are you all seeing code I do not? The loop control variable 'count2' has not even been declared much less initialized or incremented, among other variables in the fragment. One presumes a random memory location does not equal 10. Can the OP include main() and relevant subroutines?

The thread was tagged with C and objective C, not C++; correct?

sysprog and FactChecker
Mark44
Mentor
Are you all seeing code I do not? The loop control variable 'count2' has not even been declared much less initialized or incremented, among other variables in the fragment.
The posted code was only a fragment, with no declarations shown of the variables being used.
The thread was tagged with C and objective C, not C++; correct?
The "C" code tag also includes C++; there is no separate tag for C++ or C#, nor is there one for Objective C.

Mark44 said:
The posted code was only a fragment, ##\dots##
Yeah I was mildly annoyed about that -- people want you to assist them in fixing a problem, but showing you the full code is out of the question ##\dots##

Hey by the way @Mark44, sorry about this being rather off-topic (if the OP returns and wants to go back on point I'll do my best to go along with that) -- my nephew loved your AVX-512 Assembly Language Insights article.

jtbell
Mentor
The "C" code tag also includes C++; there is no separate tag for C++ or C#, nor is there one for Objective C.
Correct, for formatting code with the "code" tag, "C" is the option that covers all those languages.

However, the OP also added thread tags which you can see at the top of the thread: "#c", "c code" and "c language". I think the latter two make clear that he's using C, not C++. I guess "#c" is supposed to be a hashtag. Unless he mistyped "c#" in which case he should explain whether he's really using C or C#.

Mark44
Mentor
However, the OP also added thread tags which you can see at the top of the thread: "#c", "c code" and "c language". I think the latter two make clear that he's using C, not C++. I guess "#c" is supposed to be a hashtag. Unless he mistyped "c#" in which case he should explain whether he's really using C or C#.
The original post did not have code tags; these were added by @berkeman. Based on the use of printf() and scanf() in the fragment, the OP is almost certainly writing C code, although one can write C++ code using the C standard library routines. The code is definitely not C#, as it doesn't provide support for printf() and scanf().

At any rate, and to get back on track, we haven't heard from the OP, @anonim, for three days, so maybe we should let him/her come back and show us the complete program source code.

sysprog