ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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?
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.
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.
#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
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 ]
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.