I have been trying to compare a structure variable and a string variable. But I am getting this error.
#include<stdio.h>
#include<string.h>
int main()
{
struct details {
char number[20];
} det[10];
int inp, i=0;
char aad;
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for(b=0;b<i;b++)
{
if(det[i].number==aad)
{
printf("Hello");
}
}
return 0;
}
Pls try to fix my error
after you look in the comments this will be the answer
#include<stdio.h>
#include<string.h>
#define SIZE 20 // add size for aad
int main()
{
struct details {
char number[20];
} det[10];
int inp, i = 0;
char aad[SIZE]; // must be array (can be with pointer, use malloc)
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for (b = 0; b < i; b++)
{
if (strcmp(det[b].number, aad) == 0) // strcmp to compare strings
{
printf("Hello");
}
}
return 0;
}
Related
I'm trying to create an array of Product structs and then print the name and code of each Product in the array, but I keep getting a segmentation fault. I have tried to insert each value without a loop and then printing, and it works, but I'd like to automate it. The function fill_products fills the products array according to the user's input, and the select_products prints each name-code pair for the entire array.
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int code;
char *name;
float price;
} Product;
void select_products(Product *products, int len)
{
int i;
printf("%-30s%s\n", "Name", "Code");
for (i = 0; i < len; i++)
{
printf("%-30s%d\n", products[i].name, products[i].code);
}
return;
}
void fill_products(Product *products, int len)
{
int i, code;
char *name;
float price;
for (i = 0; i < len; i++)
{
printf("Insert product name (%d / %d): ", i + 1, len);
scanf("%s", &name);
printf("Insert product price (%d / %d): ", i + 1, len);
scanf("%f", &price);
products[i].code = i;
products[i].name = name;
products[i].price = price;
}
return;
}
int is_alloc(Product *products)
{
if (products == NULL)
{
printf("Error: memory allocation unsuccessful.\n");
}
return products != NULL;
}
int main(void)
{
int len, n_bytes;
Product *products;
printf("Insert length of array: ");
scanf("%d", &len);
n_bytes = sizeof *products * len;
products = malloc(n_bytes);
if(!is_alloc(products))
{
exit(0);
}
fill_products(products, len);
select_products(products, len);
free(products);
return 0;
}
I keep getting a segmentation fault.
Please enable compiler warnings, and pay attention to them.
This code:
char *name;
...
scanf("%s", &name);
is bogus and doesn't do at all what you intend.
You must either allocate space for name separately (and then not forget to free() it), or make that space available in the Product structure like so:
typedef struct
{
int code;
char name[100];
float price;
} Product;
(this assumes there is a reasonable limit on name length).
I created the following code, it compiles but it doesen't run properly. It shows some imput witch is correct, but after that, instead of printing according to the line printf(prod[0].dr[0]); it enters an "not responding" state. If you instead write printf(prod[0].dr); it works perfectly. I need to be able to print each character individualy (so I need something like printf(prod[0].dr[0]); that works)
If the question is to broad, please comment and I will try to specify every detail.
Thanks!
#include <stdio.h>
FILE *f, *g;
struct productie
{
char st, dr[20];
int realizabil;
}prod[30];
int citire(FILE *f){
char sir[100];
int i=0,j;
while(!feof(f))
{
fgets(sir,100,f);
prod[i].st=sir[0];
for(j=3;j<strlen(sir);j++)
prod[i].dr[j-3]=sir[j];
i++;
}
return i;
}
int exista(char sir[],char c)
{
int i;
for(i=0;i<strlen(sir);i++)
{
if(c==sir[i])
return 1;
}
return 0;
}
void neterminale(struct productie p[],int n, char N[])
{
int k=0;
int i,j;
for(i=0;i<n;i++)
{
if(isupper(p[i].st) && !exista(N,p[i].st))
N[k++]=p[i].st;
for(j=0;j<strlen(p[i].dr);j++)
if(isupper(p[i].dr[j]) && !exista(N,p[i].dr[j]))
N[k++]=p[i].dr[j];
}
N[k]='\0';
}
void terminale(struct productie p[],int n, char T[],char N[])
{
int k=0;
int i,j;
for(i=0;i<n;i++)
{
if(!exista(N,p[i].st) && !exista(T,p[i].st)&& p[i].st!='$')
T[k++]=p[i].st;
for(j=0;j<strlen(p[i].dr);j++)
if(!exista(N,p[i].dr[j]) && !exista(T,p[i].dr[j]) && p[i].dr[j]!='$')
T[k++] = p[i].dr[j];
}
T[k]='\0';
}
void afisare(FILE *g,int n){
int i;
for(i=0;i<n;i++)
fprintf(g,"%c -> %s",prod[i].st, prod[i].dr);
}
int main(){
char N[30];
char T[30];
int i,j,n;
f=fopen("in.txt","r");
n=citire(f);
neterminale(prod,n,N);
printf("\nNeterminalele sunt:{");
for(i=0;i<strlen(N);i++)
printf("%c ",N[i]);
printf("}");
terminale(prod,n,T,N);
printf("\nTerminalele sunt:{");
for(i=0;i<strlen(T);i++)
{
printf(" %c ",T[i]);
}
printf("}");
printf(prod[0].dr[0]); //AT THIS LINE IS THE PROBLEM
g=fopen("out.txt","w");
afisare(g,n);
fclose(f);
fclose(g);
return 0;
}
The in.txt :
S->aS
S->a
S->$
S->AB
A->b
B->c
If you want to print a string directly, you need to provide a pointer to the first character, not the first character itself:
printf(&(prod[0].dr[0]));
or
printf(prod[0].dr);
But you can't limit this to a single char (there is no nprintf function).
so basically I want to create a program that asks for an username and a password in order to enter the actual program. I tried doing something like that but when I type the first username and password it doesn't let me go through. On the other hand when I type the second username and password it does work. Can someone explain me why?
#include<stdio.h>
#include<string.h>
#define MAX 100
#define LEN 40
int names(char listName[][LEN]);
void pass(char listPass[][LEN]);
int main()
{
char name[LEN];
char password[LEN];
char listName[MAX][LEN];
char listPass[MAX][LEN];
int i;
names(listName);
pass(listPass);
printf("Enter username: ");
scanf("%s", name);
printf("Enter password: ");
scanf("%s", password);
for(i = 0; i< 2; i++)
{
if (strcmp(listName[i], name) == 0 && strcmp(listPass[i], password) == 0)
{
printf("Access granted\n");
break;
}
else
{
printf("Access denied\n");
break;
}
}
getch();
}
int names(char listName[][LEN])
{
int i;
strcpy(listName[i], "Vince");
strcpy(listName[i], "Jeremy");
}
void pass(char listPass[][LEN])
{
int i;
strcpy(listPass[i], "aBc2xyz8");
strcpy(listPass[i], "fa7saC12");
}
Compile with warnings enabled and you'd see that the variable i is used uninitialized here:
int names(char listName[][LEN])
{
int i;
strcpy(listName[i], "Vince");
strcpy(listName[i], "Jeremy");
}
void pass(char listPass[][LEN])
{
int i;
strcpy(listPass[i], "aBc2xyz8");
strcpy(listPass[i], "fa7saC12");
}
What you have now is undefined behaviour, i.e. anything can happen. More likely you'd just get a random crash.
You meant strcpy(listName[0], "Vince");, strcpy(listName[1], "Jeremy");
However, what you'd really want to do is to define these variables *out of main, and initialize them there. Also, allow for varying length passwords and usernames by using an array of pointers to char. For example:
char *listName[MAX] = { "Vince", "Jeremy" };
char *listPass[MAX] = { "aBc2xyz8", "fa7saC12" };
int main(void) { ...
Or even better, use a structure for user data:
struct user_data {
char *username, *password;
};
struct user_data users[MAX] = {
{ "Vince", "aBc2xyz8" },
{ "Jeremy", "fa7saC12" }
};
In names() and pass() you use i uninitialized, both strcpy operations are going to copy to the same array index.
Following code is not what you intended:
int names(char listName[][LEN])
{
int i;
strcpy(listName[i], "Vince");
strcpy(listName[i], "Jeremy");
}
void pass(char listPass[][LEN])
{
int i;
strcpy(listPass[i], "aBc2xyz8");
strcpy(listPass[i], "fa7saC12");
}
You should initialize i and increment it between the statements, or simply use:
int names(char listName[][LEN])
{
strcpy(listName[0], "Vince");
strcpy(listName[1], "Jeremy");
}
void pass(char listPass[][LEN])
{
strcpy(listPass[0], "aBc2xyz8");
strcpy(listPass[1], "fa7saC12");
}
I am trying to dynamically allocate a contiguous block of memory, store some integer value and display it.
#include<stdio.h>
#include<conio.h>
void main()
{ int i;
int *ptr;
ptr=(void *)malloc(sizeof(int)*5); //allocation of memory
for(i=0;i<5;i++)
{ scanf("%d",&ptr[i]);
}
for(i=0;i<5;i++)
{ printf("%d",*ptr[i]); //error is found here``
}
} }
ptr[i] means the value at address (ptr+i) so *ptr[i] is meaningless.You should remove the *
Your corrected code should be :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int i;
int *ptr;
ptr=malloc(sizeof(int)*5); //allocation of memory
for(i=0;i<5;i++)
{ scanf("%d",&ptr[i]);
}
for(i=0;i<5;i++)
{ printf("%d",ptr[i]); //loose the *
}
return 0;
} //loose extra }
I have come across this wierd and mysterous (at least to me) error that I am finding a very hard time finding. It gives me an error at the line where I call my function input(student_list1[MAX], &total_entries); where the compiler says:
incompatible type for agument 1 in 'input'
What am I doing wrong here? I sense it something very simple and stupid but I have gone through the code several times now without any avail.
#define MAX 10
#define NAME_LEN 15
struct person {
char name[NAME_LEN+1];
int age;
};
void input(struct person student_list1[MAX], int *total_entries);
int main(void)
{
struct person student_list1[MAX];
int total_entries=0, i;
input(student_list1[MAX], &total_entries);
for(i=0; i<total_entries; i++)
{
printf("Student 1:\tNamn: %s.\tAge: %s.\n", student_list1[i].name, student_list1[i].age);
}
return 0;
} //main end
void input(struct person student_list1[MAX], int *total_entries)
{
int done=0;
while(done!=1)
{
int i=0;
printf("Name of student: ");
fgets(student_list1[i].name, strlen(student_list1[i].name), stdin);
student_list1[i].name[strlen(student_list1[i].name)-1]=0;
if(student_list1[i].name==0) {
done=1;
}
else {
printf("Age of student: ");
scanf("%d", student_list1[i].age);
*total_entries++;
i++;
}
}
}
struct person student_list1[MAX] in the function argument is actually a pointer to struct person student_list1.
student_list1[MAX] you passed is a (out of bound) member of the array struct person student_list1[MAX]. Valid array index shoudl be between 0 to MAX - 1.
Change it to:
input(student_list1, &total_entries);
Note that here the array name student_list1 is automatically converted to a pointer to student_list1[0].
There are many things wrong with the code; this is my attempt at making it somewhat more robust:
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}
input(student_list1[MAX], &total_entries); shoud be input(student_list1, &total_entries);.
In C,
void input(struct person student_list1[MAX], int *total_entries);
equals
void input(struct person *student_list1, int *total_entries);