LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2023, 03:55 PM   #1
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Rep: Reputation: Disabled
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
 
Old 02-05-2023, 04:03 PM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,620
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by however View Post
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.

Last edited by Michael Uplawski; 02-05-2023 at 04:07 PM. Reason: orthopedics ... orthogo... ort. Words!
 
1 members found this post helpful.
Old 02-05-2023, 04:15 PM   #3
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-05-2023, 04:19 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,675

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Go back and look at your playgame function in your puzzle program.
 
1 members found this post helpful.
Old 02-05-2023, 04:21 PM   #5
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,620
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by however View Post
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.
 
1 members found this post helpful.
Old 02-05-2023, 04:24 PM   #6
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
 
Old 02-05-2023, 04:27 PM   #7
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Michael Uplawski View Post
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.
 
Old 02-05-2023, 04:29 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,675

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
The code that checks the input for the coordinates can be modified to fit your do while loop.
 
1 members found this post helpful.
Old 02-05-2023, 04:48 PM   #9
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
 
Old 02-05-2023, 11:35 PM   #10
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Michael Uplawski View Post
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 ]

Last edited by however; 02-05-2023 at 11:41 PM.
 
1 members found this post helpful.
Old 02-06-2023, 12:30 AM   #11
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
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*
 
Old 02-06-2023, 12:37 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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.
 
1 members found this post helpful.
Old 02-06-2023, 12:45 AM   #13
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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?
 
Old 02-06-2023, 12:57 AM   #14
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 496

Original Poster
Rep: Reputation: Disabled
Gotcha!
I should use 'scanf' instead of 'fgets'

I can start my normal day now.

Thank you very much indeed everyone.

Last edited by however; 02-06-2023 at 01:02 AM.
 
Old 02-06-2023, 01:02 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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 View Post
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.

Last edited by pan64; 02-06-2023 at 01:05 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] bash loop not working as expected... replica9000 Linux - Software 10 04-24-2011 09:08 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
C: "or"(||) not functioning as expected in while loop mlaich Programming 4 12-04-2005 02:13 AM
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration