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
Related
I wrote a code that is supposed to write into a file but when i execute the program it says "error in saving student data".Here is the code.
#include<stdio.h>
typedef struct Student{
int numberOfStudents;
char name; // onoma foithth
char surname; // epi8eto foithth
};
int main(){
struct Student s1;
FILE *file=fopen("d:\\student.txt","w");
if(file==NULL){
printf("error in saving student data");
return 1;
}
while(1){
printf("Enter number of students: ");
scanf("%d",&s1.numberOfStudents);
printf("enter name: ");
scanf("%s",&s1.name);
printf("enter surname: ");
scanf("%s",&s1.surname);
fprintf(file,"%d\t%s\t%s\n",s1.numberOfStudents,s1.name,s1.surname);
printf("continue (Y/N)");
char ch=getch();
if (ch=='N' || ch=='n')
break;
}
fclose(file);
return 0;
}
I have searched but i can't find the problem.where is my mistake?
name field of structure should be char array if you want to store onoma foithth in that as shown. modify your structure as
typedef struct Student{
int numberOfStudents;
char name[10]; // onoma foithth
char surname; // epi8eto foithth
};
and then while scanning name remove & because name is itself address.
scanf("%s",s1.name);
Make sure you gave correct path in fopen()
I'm starting coding this week so I'm dummy about it. I need help about return to main in my script. For example when I done Course registration part I can't return menu program is crashing
Codes:
#include <stdafx.h>
#include <stdio.h>
void eng();
void menu();
void huh();
int main()
{
menu();
return 0;
}
void menu()
{
int menu1choice;
printf("Menu\n");
printf("\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf_s("%d", &menu1choice);
switch (menu1choice)
{
case 1:
{
eng();
break;
}
}
}
void eng()
{
int a = 5;
char name[30];
printf("1.Student Number: ");
scanf_s("%d", &a);
//student number
printf("2.Name: ");
scanf_s("%s", &name);
//student name
getchar();
}
void huh()
{
int a = 5;
char name[30];
printf("Your Student number: %d\n", a);
printf("Your Name: %s\n", name);
//result
getchar();
}
Pls help me write return code lines, Thanks in Advance
Here is some code that may help you understand how to the mechanics of functions returning values, in your case back to the main function.
As for some advice, please read up about magic numbers and specifically why they are bad.
/*
* 35917794_main.c
*/
#include <stdio.h>
#define STU_REG 1
#define STU_SHOW 2
#define EXIT_SUCCESS 0
unsigned int show_menu(void);
unsigned int main
(
unsigned int argc,
unsigned char *arg[]
)
{
unsigned int menu1choice;
/*
* The next statements says run the function show_menu() and put the returned
* result in the variable menu1choice.
*/
menu1choice = show_menu();
switch(menu1choice)
{
case (STU_REG):
{
printf("\nGo do STUDENT REGISTRATION things...\n\n");
break;
}
case STU_SHOW:
{
printf("\nGo do SHOW STUDENT things...\n\n");
break;
}
default:
{
printf("\nGo do something for invalid option...\n\n");
break;
}
}
return(EXIT_SUCCESS);
}
unsigned int show_menu
(
void
)
{
unsigned int ui_W0;
printf("Menu\n\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf("%d", &ui_W0);
/*
* The next statements says run the function show_menu() has finished and returned
* returns the result in the variable ui_W0.
*/
return(ui_W0);
}
My main goal for this code is to capture the users input and do whatever he wants to do with the choices I have presented, but I'm stuck: when I compile, I can only type the word and the program stops working.
i have no idea where I'm making a mistake.
The is my code:
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 100
void grab_user_input(void);
void load_menu(void);
void Count_the_letters(void);
int main(void)
{
grab_user_input();
return 0;
}
void grab_user_input(void)
{
char word;
{
printf("Please enter a single word (25 characters or less): \n");
scanf("%s", &word);
printf("Thanks! The word you entered is: %s\n", word);
}
void load_menu(void)
{
int choice;
do
{
int choice;
printf("\n(:===Menu====:)\n");
printf("1. Count_the_letters\n");
printf("2. Count_the_vowels\n");
printf("3. Reverse_the_word\n");
printf("4. Check_if_palindrome\n");
printf("5. Enter_a_new_word\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1: Count_the_letters();
break;
}
} while (choice != 3);
}
void Count_the_letters(void)
{
char S[MAX_STRING_LENGTH];
int count;
count = 0;
do {
printf("string:\t");
scanf("%s",S);
if (strcmp(S,"exit") != 0)
++count;
} while (strcmp(S,"exit") != 0);
printf("word count:\t%d\n", count);
}
return 0;
}
scanf("%s", &word);
needs an array of characters to read the data. &word only has space for one character.
You are running into undefined behavior.
Use
char word[26];
scanf("%25s", &word);
The reason is that you are passing the address to the char variable you declared and scanf() is trying to write two bytes where it only fits one.
char word
this declares a char variable, it can hold a single byte
scanf("%s", &word);
whill require at least one byte for an empty string the '\0'.
But also, you declared a lot of functions inside void grab_user_input(void), that is not valid standard c, it might work with some compiler, but it's not standard.
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).
I have the following struct
typedef char String[256];
typedef struct
{
String name;
int year;
float price;
} Book;
Array of Books
int main(int argc, const char * argv[])
{
Book books[5];
for (int i=0; i<5; i++) {
books[i] = inputBook();
}
return 0;
}
inputBook() function
Book inputBook()
{
Book myBook;
//Name
puts("Enter Book Name:");
gets(myBook.name);
//Publishing Year
puts("Enter Book Publishing Year:");
scanf("%i", &myBook.year);
//Price
puts("Enter Book Price:");
scanf("%f", &myBook.price);
return myBook;
}
For some reason the first book input is going well but when trying to input the second book and the second call to inputBook() I can set a book name, it jumps straight to the year import.
What is the problem ?
Thanks!
To correct, replace:
gets(myBook.name);
with:
scanf("%255s", myBook.name); /* 255 as name is 256 chars. */
as scanf() will skip any whitespace characters, but gets() will not. A newline character is considered a whitespace character and there will be a newline remaining in stdin after the price has been entered causing gets() to read the newline and effectively read nothing.
Worth reading: warning:gets function is dangerous
This is because the variable myBook is valid only in the inputBook scope and is destroyed as soon as the function exits.
you should pass the book item you want to initialize as a parameter of your function.
function inputBook()...
void inputBook(Book *ptBook )
{
if( ptBook==NULL )
return;
//Name
puts("Enter Book Name:");
gets(ptBook->name);
//Publishing Year
puts("Enter Book Publishing Year:");
scanf("%i", &ptBook->year);
//Price
puts("Enter Book Price:");
scanf("%f", &ptBook->price);
}
The main function...
int main(int argc, const char * argv[])
{
Book books[5];
for (int i=0; i<5; i++) {
inputBook( &books[i] );
}
return 0;
}
I think you need to flush stdin before next iteration. You have orevious CRLF in your stdin stream.
use fflush(stdin); in the beginning of loop.
Maybe you can try fflush(stdin) before input.
Book inputBook()
{
Book myBook;
fflush(stdin);
// rest of the code
}
#include <stdio.h>
typedef char String[256];
typedef struct
{
String name;
int year;
float price;
} Book;
Book inputBook()
{
Book myBook;
//Name
puts("Enter Book Name:");
getchar();
gets(myBook.name);
//Publishing Year
puts("Enter Book Publishing Year:");
scanf("%i", &myBook.year);
//Price
puts("Enter Book Price:");
scanf("%f", &myBook.price);
return myBook;
}
int main(int argc, const char * argv[])
{
Book books[5];
int i = 0; for (i=0; i<5; i++) {
books[i] = inputBook();
}
return 0;
}