I need to make a hash table program that will take the first character of a string in a small case letter and modulo the ASCII with the size of the hash table that the user input. Where should I input the size other than in the main?
These are my current codes. In this code, I ask the user to input the size in the main module function but I am confused about how to use it in on every function besides main. Any help is appreciated.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct data
{
char name [100];
char age[2];
struct data *next;
};
struct data *chain[size] = {NULL};
struct data* insert(char name[], char age[]){
struct data *curr = (struct data*) malloc (sizeof(data));
strcpy(curr->name, name);
printf("input name: ");
scanf(" %[^\n]s",strcpy(curr->name, name));
printf("input age: ");
scanf(" %[^\n]s", strcpy(curr->age, age));
curr->next = NULL;
return curr;
};
int main(){
int n;
char name[100];
char age[2];
char firstChar;
int key;
int index = 0;
printf("input the number of hash table: ");
scanf("%d", &n); getchar();
printf("\n");
int option;
do{
printf("=== Option Menu ===\n");
printf("1. insert data\n");
printf("2. delete data\n");
printf("3. search data\n");
printf("4. view data\n");
printf("5. exit\n");
printf("input option: ");
scanf("%d", &option); getchar();
switch(option){
case 1:
insert(name,age);
break;
}
}while(option!=5);
}
can i make the size of hashtable dynamic ?
Yes, you can, and you even need to, since you wrote "I need to make a hash table program … with the size of the hash table that the user input". In order to do that, don't define struct data *chain[size] = {NULL}; but struct data **chain; and after input of n in main do
chain = calloc(n, sizeof *chain);
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char *name;
char *addr;
int age;
int clas;
}*stu;
int main()
{
FILE *fp;
int choice,another;
size_t recsize;
size_t length;
struct student *stu=(struct student *)malloc(sizeof(struct student));
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*)malloc(sizeof(char)*20);
recsize=sizeof(*stu);
fp=fopen("student.txt","a+");
if(fp==NULL)
{
fp=fopen("student.txt","w+");
if(fp==NULL)
{
printf("cannot open the file");
exit(1);
}
}
do
{
fseek(fp,1,SEEK_END);
printf("Please Enter student Details\n");
printf("Student Name: ");
scanf("%s",stu->name);
printf("Address: ");
scanf("%s",stu->addr);
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
fwrite(stu,recsize,1,fp);
printf("Add another Enter 1 ?\n");
scanf("%d",&another);
}while(another==1);
fclose(fp);
free(stu);
}
I have code in C which has a structure Student. I am trying to get all structure members values from user. Memory is allocated for structure and two members *name and *addr. when I try to write those values using fwrite() function in a file Student.txt it shows random output ( ཀའ㌱䔀8䵁ཀའ㈱䔀1䵁 ) like this in a file and it is not in readable form. Please provide me the best way to write members of structure in file using fwrite() function.
You need to use %d instead of %s for ints
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
should be
printf("Class: ");
scanf("%d",&stu->clas);
printf("Age: ");
scanf("%d",&stu->age);
And as pointed out by #David Hoelzer in comments: you're writing the value of the pointers rather than what they contain, change
struct student{
char *name;
char *addr;
to
struct student{
char name[20];
char addr[20];
and delete those lines:
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*) malloc(sizeof(char)*20);
this is the whole code of what im doing, im trying to create a song library that will put what the user enter into file. now the compiler says that passing argument 2 of strcpy makes pointer from integer without a cast and i dont know why. also can u check my linked list for the struct. im so noob at linked list :(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node {
//definition of struct node to create struct node song
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
struct node*next;
};
add_song(int SongID, char Title, char Artist, char Composer, char Album, char Genre, int Rating, char Remarks) {
//this is the add song function as stated in the mp2 specs
FILE*fp;
fp=fopen("song.txt","r+");
int i=1, j, choice;
struct node* temp=malloc(sizeof(struct node));
temp->SongID=SongID;
fprintf(fp,"%d",SongID);
strcpy(temp->Title, Title);
fprintf(fp,"%s",Title);
strcpy(temp->Artist, Artist);
fprintf(fp,"%s",Artist);
strcpy(temp->Composer, Composer);
fprintf(fp,"%s",Composer);
strcpy(temp->Album, Album);
fprintf(fp,"%s",Album);
strcpy(temp->Genre, Genre);
fprintf(fp,"%s",Genre);
temp->Rating=Rating;
fprintf(fp,"%d",Rating);
strcpy(temp->Remarks, Remarks);
fprintf(fp,"%s",Remarks);
fclose(fp);
}
int main ()
{
struct node song;
int choice;
int k, i;
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
/* do
{
printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
scanf("%d\n", &choice1);
if (choice1==1)
{*/
srand(time(NULL));
song.SongID=rand();
printf("Enter Title: ");
fgets(Title,100,stdin);
printf("Enter Artist: ");
fgets(Artist,100,stdin);
printf("Enter Composer: ");
fgets(Composer,100,stdin);
printf("Enter Album: ");
fgets(Album,100,stdin);
//for easier code, numbers are being chosen as input
printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
scanf("%d", &choice);
if (choice==1)
{
strcpy(song.Genre,"Art Music");
}
else if (choice==2)
{
strcpy(song.Genre,"Popular Music");
}
else if (choice==3)
{
strcpy(song.Genre,"Traditional Music");
}
else
{
printf("You entered a blank genre.\n");
}
printf("Enter your rating, choose from 1-5: ");
scanf("%d", &Rating);
printf("Enter Remarks: ");
fgets(Remarks,1000,stdin);
add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
/*k=0;
break;
}
else if (choice1==2)
{
//update_song(song);
k=0;
break;
}
else if (choice1==3)
{
// list_songs(song);
k=0;
break;
}
else
{
k=1;
printf("That is not a valid input.\n");
}
}while (k==1);*/
return 0;
}
Your function definition don't match with the arguments you pass. It should be
void add_song(int SongID, char *Title, char *Artist, char *Composer, \
char *Album, char *Genre, int Rating, char *Remarks) {
...
...
}
Another issue is that songID is uninitialized in main(). Reading from an uninitialized variable is undefind behaviour.
Another problem you might face is that fgets() reads the newline \n into buffer if there's space available which might be problematic.
Something to be aware of and you need trim it if nececcary.
This is working copy of your code except one thing "Remark". Before executing scanf for Remark process exits . Change your arguments as I did here and also change fopen mode as +w so if file is not exist it can be created automatically. strcpy prototype is `char *strcpy(char *dest, const char *src).
So I have changed accordingly
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node {
//definition of struct node to create struct node song
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
struct node *next;
};
void add_song(int SongID, char *Title, char *Artist, char *Composer, char *Album, char *Genre, int Rating, char *Remarks) {
//this is the add song function as stated in the mp2 specs
FILE *fp;
fp=fopen("song.txt","a+");
int i=1, j, choice;
struct node* temp=malloc(sizeof(struct node));
temp->SongID=SongID;
fprintf(fp,"%d",SongID);
strcpy(temp->Title, Title);
fprintf(fp,"%s",Title);
strcpy(temp->Artist, Artist);
fprintf(fp,"%s",Artist);
strcpy(temp->Composer, Composer);
fprintf(fp,"%s",Composer);
strcpy(temp->Album, Album);
fprintf(fp,"%s",Album);
strcpy(temp->Genre, Genre);
fprintf(fp,"%s",Genre);
temp->Rating=Rating;
fprintf(fp,"%d",Rating);
strcpy(temp->Remarks, Remarks);
fprintf(fp,"%s",Remarks);
fclose(fp);
}
int main ()
{
struct node song;
int choice;
int k, i;
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
/* do
{
printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
scanf("%d\n", &choice1);
if (choice1==1)
{*/
srand(time(NULL));
song.SongID=rand();
printf("Enter Title: ");
fgets(Title,100,stdin);
printf("Enter Artist: ");
fgets(Artist,100,stdin);
printf("Enter Composer: ");
fgets(Composer,100,stdin);
printf("Enter Album: ");
fgets(Album,100,stdin);
//for easier code, numbers are being chosen as input
printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
scanf("%d", &choice);
if (choice==1)
{
strcpy(song.Genre,"Art Music");
}
else if (choice==2)
{
strcpy(song.Genre,"Popular Music");
}
else if (choice==3)
{
strcpy(song.Genre,"Traditional Music");
}
else
{
printf("You entered a blank genre.\n");
}
printf("Enter your rating, choose from 1-5: ");
scanf("%d",&Rating);
printf("Enter Remarks: \n");
fgets(Remarks,1000,stdin);
add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
/*k=0;
break;
}
else if (choice1==2)
{
//update_song(song);
k=0;
break;
}
else if (choice1==3)
{
// list_songs(song);
k=0;
break;
}
else
{
k=1;
printf("That is not a valid input.\n");
}
}while (k==1);*/
return 0;
}
Didn't go through the whole code, but strcpy problem is obvious. According to definition of function strcpy (http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm), it expects two char pointers (aka strings in C)
char *strcpy(char *dest, const char *src)
You're passing only char arguments to a function add_song, which is just not the pointer. Change the signature of the function add_song and then you should be fine with strcpy
I'm attempting to create a simple program that stores ten "pets" into an array. Each stuct contains data that must be accessed through functions. For some reason this doesn't seem to be working the way I would expect. Does anyone know why the program prompts for the name and then runs through the rest of the program without prompting the user again?
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
struct Pet {
char name[50]; //name
char type[50]; //type
char owner[50]; //owner
};
void setPetName(struct Pet *pet, char *name){
memcpy(pet->name,name, 50);
}
void setPetType(struct Pet *pet, char *type){
memcpy(pet->type,type, 50);
}
void setOwner(struct Pet *pet, char *owner){
memcpy(pet->owner,owner, 50);
}
char* getName(struct Pet *pet){
return pet->name;
}
char* getType(struct Pet *pet){
return pet->type;
}
char* getOwner(struct Pet *pet){
return pet->owner;
}
void printPetInfo(struct Pet *pet){
printf("Pet's name is %s, Pet's type is %s, Pet's owner is %s", pet->name, pet->type, pet->owner);
}
int main(){
struct Pet Pets[9];
int index;
char name[50], type[50], owner[50];
for (index=0; index<9; index++){
struct Pet pet;
printf("Please enter pet's name ");
scanf("%s\n", name);
setPetName(&pet, name);
printf("Please enter pet's type ");
scanf("%s\n", type);
setPetType(&pet, type);
printf("Please enter pet's owner ");
scanf("%s\n", owner);
setOwner(&pet, owner);
printPetInfo(&pet);
Pets[index]=pet;
}
return 0;
}
First you can't hold a string in a char:
char name, type, owner;
Instead you need an array of char (ie char name[50]; for example)
Then the format to scan a string is %s, not &s
scanf("&s\n", name);
And finally if you want to print a string, use format %s, not %c (%c is to print a single char).
#include<stdio.h>
struct student{
char name[80];
char subject
char country;
};
int main(){
struct student s[10];
int i;
printf("Enter the information of the students:\n");
for(i=0;i<4;++i)
{
printf("\nEnter name of the student: ");
scanf("%s",&s[i].name);
printf("\nEnter the subject of the student: ");
scanf("%s",&s[i].subject);
printf("\nEnter name of the student country: ");
scanf("%s",&s[i].country);
}
printf("\n showing the input of student information: \n");
for(i=0;i<10;++i)
{
printf("\nName: \n");
puts(s[i].name);
printf("\nMajor: \n",s[i].subject);
printf("\nCountry: \n",s[i].country);
}
return 0;
}
***while I tried to display the result it is not showing the subject and country.Can u tell me what problem is in my coding?
Is it not showing the subject and the country or is it displaying the first letter only?
I'm not familiar with C but i would suggest you to change
char variableName
to
char variableName[size]
As you have in name but you do not have in country and subject. I'm not sure if it is your problem but it might be, I believe just char variableName would store only one character of the users input.
You need to provide a conversion pattern, for char it is %c
printf("\nMajor: %c\n",s[i].subject);
printf("\nCountry: %c\n",s[i].country);
Also
scanf("%s",&s[i].name);
is not correct, it should be
scanf("%s", s[i].name); // s[i].name is already an array
for reading a char you need to pass the correct conversion pattern too
scanf("%c", &s[i].subject);
scanf("%c", &s[i].country);
Your struct should be something like:
struct student{
char name[80];
char subject[80];
char country[80];
};