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

Click For Summary
The discussion revolves around a coding issue in a do-while loop for a crossword puzzle program, where the exit condition using string comparison is not functioning as intended. The original code incorrectly uses '==' for string comparison, which leads to errors, and participants recommend using the strcmp function from string.h instead. Additionally, there are suggestions to restructure the input process to avoid complications with multiple inputs in a single scanf call. The need for proper variable initialization and declarations is emphasized, as well as the importance of providing complete code for effective troubleshooting. The thread highlights common pitfalls in C programming related to string handling and loop control.
anonim
Messages
39
Reaction score
2
Homework Statement
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:
Physics news on Phys.org
Use strcmp to compare strings. Using '==' does not do what you think it does. It compares addresses.
 
  • Like
Likes 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:
  • Like
Likes phinds
(I added code tags to the OP)
 
  • Like
Likes sysprog
I think that you need to import string.h to use strcmp. Not sure, though.
 
archaic said:
I think that you need to import string.h to use strcmp.
Yes, but technically you include this header.
C:
#include <string.h>
 
sysprog said:
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. :)
 
berkeman said:
(I added code tags to the OP)
Thanks:)
 
  • Like
Likes berkeman
  • #10
archaic said:
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.
 
  • #11
Mark44 said:
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.
 
  • #12
anonim said:
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?
 
  • Like
Likes sysprog
  • #13
anonim said:
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.
 
  • #14
Mark44 said:
Yes, but technically you include this header.
C:
#include <string.h>
Yes. Java has taken over my mind for now. o_O
 
  • #15
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.
 
  • Like
Likes sysprog
  • #16
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>.
 
  • Like
Likes sysprog
  • #17
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?
 
  • Like
Likes sysprog and FactChecker
  • #18
Klystron said:
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.
Klystron said:
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.
 
  • #19
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##
 
  • #20
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 Langauge Insights article.
 
  • #21
Mark44 said:
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#. :oldconfused:
 
  • #22
jtbell said:
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#. :oldconfused:
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.
 
  • Like
Likes sysprog

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K