• Support PF! Buy your school textbooks, materials and every day products Here!

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

  • #1
29
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:

Answers and Replies

  • #2
FactChecker
Science Advisor
Gold Member
5,591
2,073
Use strcmp to compare strings. Using '==' does not do what you think it does. It compares addresses.
 
  • Like
Likes berkeman and anonim
  • #3
29
2
Thanks for your answer. I tried, but still it does not work.
 
  • #4
1,471
785
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
berkeman
Mentor
57,500
7,524
(I added code tags to the OP)
 
  • Like
Likes sysprog
  • #6
537
153
I think that you need to import string.h to use strcmp. Not sure, though.
 
  • #7
33,641
5,307
I think that you need to import string.h to use strcmp.
Yes, but technically you include this header.
C:
#include <string.h>
 
  • #8
29
2
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
29
2
  • Like
Likes berkeman
  • #10
29
2
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
29
2
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
berkeman
Mentor
57,500
7,524
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
1,471
785
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
537
153
Yes, but technically you include this header.
C:
#include <string.h>
Yes. Java has taken over my mind for now. o_O
 
  • #15
Joshy
Gold Member
256
107
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
33,641
5,307
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
Klystron
Gold Member
690
939
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
33,641
5,307
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.
 
  • #19
1,471
785
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
1,471
785
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
jtbell
Mentor
15,554
3,509
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
33,641
5,307
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

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

  • Last Post
Replies
7
Views
1K
Replies
4
Views
831
  • Last Post
Replies
5
Views
4K
Replies
9
Views
1K
Replies
9
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
9
Views
2K
Replies
10
Views
10K
Replies
1
Views
2K
  • Last Post
Replies
6
Views
2K
Top