LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   do-while loop not working as expected (https://www.linuxquestions.org/questions/programming-9/do-while-loop-not-working-as-expected-4175721722/)

however 02-05-2023 03:55 PM

do-while loop not working as expected
 
hello everyone,
on my path to better learning C, I wanted to make a do-while loop that asked the user to input.
Code:

int main(void)
{
        char name[20];
        do
        {
                printf("What is your name? [Type zzz to stop] - ");
                gets(name);
        }
        while(name != "zzz");
}

But the loop doesn't stop when i input zzz.

What am i missing? Could anyone be so kind to point me to the right direction?

Thanks in advance

Michael Uplawski 02-05-2023 04:03 PM

Quote:

Originally Posted by however (Post 6409359)
hello everyone,
on my path to better learning C, I wanted to make a do-while loop that asked the user to input.
Code:

int main(void)
{
        char name[20];
(...)
while(name != "zzz");
}


Find the documentation that explains what a string is, what an array is and what you compare in your above while-statement.
You define a String, then you compare the *String* to some *content* (this does not make sense in C-like languages).
What you want is compare the *content* of the String to another supposed *content*. There are functions in C and C++ for this very task. You have to use them instead of your above construct with "!=".

Other languages handle this differently. If you have experience with other programming-universes, do as if you had never heard about them.

however 02-05-2023 04:15 PM

Could i solve this by scan_f the user's name as a "userInput" sting? And then compare it to zzz while expression?
Or, maybe even simplier, just declare a string instead of a char?

michaelk 02-05-2023 04:19 PM

Go back and look at your playgame function in your puzzle program.

Michael Uplawski 02-05-2023 04:21 PM

Quote:

Originally Posted by however (Post 6409363)
Could i solve this by scan_f the user's name as a "userInput" sting? And then compare it to zzz while expression?
Or, maybe even simplier, just declare a string instead of a char?

You did not like my answer, above? What is it that has not met your requirements? You compare something. So use a function which exists for this very purpose.

however 02-05-2023 04:24 PM

Quote:

Originally Posted by michaelk (Post 6409364)
Go back and look at your playgame function in your puzzle program.

Didnt think about that )
Anyway, pretty late here so i will have a lokk tmrw, or maybe nxt wkend ))

Thank you

however 02-05-2023 04:27 PM

Quote:

Originally Posted by Michael Uplawski (Post 6409365)
You did not like my answer, above? What is it that has not met your requirements? You compare something. So use a function which exists for this very purpose.

Apologies. I always appreciate all answers but it takes time to process the technicalities.

What would that function be.

michaelk 02-05-2023 04:29 PM

The code that checks the input for the coordinates can be modified to fit your do while loop.

however 02-05-2023 04:48 PM

Quote:

Originally Posted by michaelk (Post 6409369)
The code that checks the input for the coordinates can be modified to fit your do while loop.

I remember it took me weeks to get those lines of code for those coordinates. I thought of doing something more basic to practice with the basics.

I was following this example
Code:

#include <stdio.h>
int main() {
  double number, sum = 0;

  // the body of the loop is executed at least once
  do {
    printf("Enter a number: ");
    scanf("%lf", &number);
    sum += number;
  }
  while(number != 0.0);

  printf("Sum = %.2lf",sum);

  return 0;
}

But i guess i didnot consider the difference between char, strings and array as already suggested

however 02-05-2023 11:35 PM

Quote:

Originally Posted by Michael Uplawski (Post 6409365)
You did not like my answer, above? What is it that has not met your requirements? You compare something. So use a function which exists for this very purpose.

strcmp

found it!
thank you!

[got up at 5am this morning to get this out of my head :D]

however 02-06-2023 12:30 AM

Programming is frustrating! I still can't get this to work.

when i input zzz the loop won't stop

what else am i missing?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        char name[20];
        char stop_name[]="zzz";
        int check;

        do
        {
                printf("What is your name? [Type zzz to stop] - ");
                fgets(name, 20, stdin);
                check=strcmp(name, stop_name);
        }
        while(check !=0);

        return 0;
}

*by the way, i am happy to report that I have started using external editor and manually compile/execute (kate, gcc, ./) instead of using an IDE*

pan64 02-06-2023 12:37 AM

would be nice to read the documentation (man fgets):
Quote:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

however 02-06-2023 12:45 AM

Quote:

Originally Posted by pan64 (Post 6409413)
would be nice to read the documentation (man fgets):

Thank you for the info however, perhaps this "If a newline is read, it is stored into the buffer." is a little too much to understand for someone who is trying to learn. I would need more explanation or an example so that i can start registering and processing.
Does it mean that I have to reduce the size of the char buffer?

however 02-06-2023 12:57 AM

Gotcha!
I should use 'scanf' instead of 'fgets'

I can start my normal day now.

Thank you very much indeed everyone.

pan64 02-06-2023 01:02 AM

That means not only zzz is stored, but the newline entered too. That's why the result was not equal to zzz ( but zzz\n )

Quote:

Originally Posted by however (Post 6409411)
Programming is frustrating! I still can't get this to work.

In my case I used to say I don't understand something, I need to learn. What is really frustrating for me: I don't know what's going on.


All times are GMT -5. The time now is 06:53 AM.