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

In summary, the conversation is about a code for creating a crossword puzzle, in which the user is asked to enter coordinates and the corresponding word. The user is also given the option to exit the program by typing "Exit". However, the current code is not working as expected and the conversation involves troubleshooting and suggestions for improvement. The use of the header file "string.h" and the function "strcmp" is recommended for string comparison. The code fragment provided includes a loop, but it is suggested to use a separate loop for the input and to process the word at the specified location. There are also some issues with the logic and handling of the word "Exit" in the current code. Finally, there is a request for the full code and relevant
  • #1
anonim
40
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
  • #2
Use strcmp to compare strings. Using '==' does not do what you think it does. It compares addresses.
 
  • Like
Likes berkeman and anonim
  • #3
Thanks for your answer. I tried, but still it does not work.
 
  • #4
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
  • #5
(I added code tags to the OP)
 
  • Like
Likes sysprog
  • #6
I think that you need to import string.h to use strcmp. Not sure, though.
 
  • #7
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>
 
  • #8
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. :)
 
  • #9
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

1. Why is my do-while loop not exiting when I use the exit statement?

The exit statement in a do-while loop only exits the loop itself, not the entire program. If you want to exit the entire program, you can use the exit() function or throw an exception.

2. How can I make my do-while loop exit based on a specific condition?

You can use a conditional statement, such as an if statement, inside the do-while loop to check for the condition and use the exit statement to break out of the loop if the condition is met.

3. Can I use a break statement to exit a do-while loop?

Yes, you can use a break statement to exit a do-while loop. However, the break statement will only exit the loop it is placed in, not the entire program.

4. What is the difference between using exit and break in a do-while loop?

The exit statement exits the entire program, while the break statement only exits the loop it is placed in. Additionally, the exit statement can be used outside of a loop, while the break statement can only be used inside a loop.

5. Is there a way to make my do-while loop exit after a certain number of iterations?

Yes, you can use a counter variable and a conditional statement to check for the desired number of iterations. Once the counter reaches the specified number, you can use the exit statement to break out of the loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
873
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
9
Views
947
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top