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

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to a do-while loop in C for a crossword puzzle application. Participants are addressing problems with string comparison and program flow control, specifically regarding the exit condition for the program.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants suggest using strcmp to compare strings instead of '==' which compares addresses.
  • Others mention the need to include string.h to use strcmp, though there is uncertainty about whether this is necessary.
  • A participant notes that the loop control variable 'count2' has not been declared or initialized, raising concerns about the code fragment's completeness.
  • One participant proposes changing the loop structure to simplify input handling and suggests using toupper for case-insensitive comparisons.
  • Another participant expresses frustration about the lack of complete code provided by the original poster, which hinders troubleshooting.
  • There are discussions about the programming language being used, with clarifications regarding the tags and potential confusion between C, C++, and C#.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper string comparison and the inclusion of necessary headers, but there is no consensus on the overall structure of the code or the specific issues causing the segmentation fault. The discussion remains unresolved regarding the complete solution to the original problem.

Contextual Notes

The original code fragment lacks declarations for several variables, and the input/output context is not fully provided, which complicates the diagnosis of the issue.

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   Reactions: 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   Reactions: phinds
(I added code tags to the OP)
 
  • Like
Likes   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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 language 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   Reactions: 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
2K
  • · 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
4K