This question already has answers here:
Why is "a" != "a" in C?
(11 answers)
Closed 7 years ago.
Given below is a piece of code that does not do what I want
do
{
printf("inserisci un nome: ");
scanf("%29s", s);
} while (s!="*");
My aim is to exit from the cycle if the string entered is "*".
Why doesn't it work?
What should I modify?
Take a look at strcmp to compare strings, != will not do what you want.
In that case != will compare the variable s (a pointer to the first element of the array s) with the string "*". That is why it was not working properly.
Related
This question already has answers here:
scanf error handling C
(2 answers)
Closed 1 year ago.
scanf("%x", &hexa_decimal_num);
I am trying to deal with the case of entering something wrong like "ZZ" and printing "Error!".
int x = scanf("%x", &hexa_decimal_num);
if (x == 0)
{
printf ("Error!");
}
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 1 year ago.
I am just starting to learn C coding ,I am trying to write a program that keeps scanning names and once it scans a specific name lets say for example "John" it stops Please correct me
#include <stdio.h>
int main()
{
char name[20];
printf("enter a name:");
for(;;)
{
scanf("%s",name);
if (name='John')
{
break;
}
}
You can't compare two string pointers with != or ==.
you need to use strcmp, for example:
while (strcmp(check,input) != 0)
strcmp compares the string pointed to, by str1 to the string pointed to by str2.
This question already has answers here:
NULL arg allowed to sscanf?
(6 answers)
pointer segmentation fault at scanf [duplicate]
(3 answers)
Closed 2 years ago.
float *salaire;
salaire = NULL;
scanf("%f", salaire);
printf("\nLe salaire : %f", *salaire);
You need to identify the place where you can store this float value and null means no space exist so you should allocate space for it. I recommend replace second line by :
salaire=malloc(sizeof(float));
This question already has answers here:
Null terminated string in C
(5 answers)
Closed 4 years ago.
Should I always set the last value of a char array to '\0'?
char search_for[80];
search text here
printf("Search for: ");
scanf("%79s", search_for);
search_for[strlen(search_for) - 1] = '\0';
This is a example from a C book.
When using scanf with the %s format it adds it for you, one huge logical error you have though is using strlen when you're not sure that your string is null terminated.
This question already has answers here:
Why does strcmp() return 0 when its inputs are equal?
(7 answers)
Is it a bad practice to use an if-statement without curly braces? [closed]
(15 answers)
Closed 4 years ago.
I have method add, works good, also I have method view and it's work
in this method
void search(){
FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
printf(" Enter the name:... ");
scanf("%s",na);
while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s \n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`
I don't know what's the wrong!
I have a struct and inside it i have an array of name
I ask user to enter names, then write it in a file
if I search for specific name
the method prints the next name of exact name !
what should I do?
***Record is a struct but I use typedef to rename
You don't have the printf("%s %s %s \n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.
In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.