0
//testing some structs
#include <stdio.h>

struct books {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} book;

//Function Declairations
int print_book(struct books book);

int main() {
    struct books book1;
    struct books book2;

    strcpy(book1.title, "Spot goes to the beach");
    strcpy(book1.author, "Mr Biggles");
    strcpy(book1.subject, "A story of a stupid little dog that goes to the beach and chases birds.");
    book1.book_id = 684687654;

    strcpy(book2.title, "The Cat in the Hat");
    strcpy(book2.author, "Dr Seuse");
    strcpy(book2.subject, "A mischeviouse cat come to visit and causes such a mess");
    book2.book_id = 5754454;

    printf("Available books for hire from the library\n");

    print_book(book1);
    print_book(book2);
}

int print_book(struct books book) {
    //prints the details of a books struct parsed as a parameter
    printf("\nBook Title: %s", book.title);
    printf("\nBook Author: %s", book.author);
    printf("\nBook Subject: %s", book.subject);
    printf("\nBook ID: %d", book.book_id);
    printf("\n");
}

Hey guys for some reason \n has randomly stopped working. I decided to add another \n at the bottom just to separate the output from the "press any key to continue..." from the console window at the end but it's not working now.

I've even tried going to line 28 and just inserting more \n's eg...

printf("Available b\n\n\n\n\n\n\nooks for hire from the library\n");

with no difference in the output.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Brad
  • 741
  • 7
  • 17
  • did you recompile it.. – amdixon Aug 24 '15 at 08:12
  • 2
    `int print_book()` doesn't return *anything* let alone an `int`. – EOF Aug 24 '15 at 08:12
  • yeah cheers forgot to do a return statement. But that isnt the problem. Was all working fine then just wasnt all of sudden. I just tried copying all the code into a new file and now I get strcpy() is undefined. I think VS is broken – Brad Aug 24 '15 at 08:21
  • okay something weird is definitely going on.. Tried heaps of things and restarted VS still weird behavour. Then I restarted windows and opened the original file that was having trouble. With no changes all the \n's work properly now. Could my recent upgrade to Windows 10 broken Visual Studio? – Brad Aug 24 '15 at 08:23
  • maybe vs was not doing a clean rebuild.. – amdixon Aug 24 '15 at 08:26
  • 1
    1) check the warnings when compiling: you're missing some headers (like string.h/cstring for `strcpy`) 2) `int main()` should be `int main ( void )`, and is not returning an int 3) though not an answer, try to see if using `puts` makes any difference, instead of `printf("\n");` – Elias Van Ootegem Aug 24 '15 at 09:05

1 Answers1

0
  1. int print_book(struct books book);void print_books(...).

    You didn't return anything from that function

  2. You didn't include string.h so the compiler doesn't know the declaration of strcpy() and assumes that it returns int and take any number of arguments via default promotion. So your code invokes undefined behavior. It may run this time but not some other time or on other systems. See

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Bit weird how it let me use strcpy if I need string.h included then complained later on after running it 50 times. VS is so weird. Thanx for all the help guys. – Brad Aug 24 '15 at 09:27
  • It's not weird. C allows a function to be used without or before it's declaration, with implied return and argument types. You should always enable all warnings to here what the compiler shout at you. – phuclv Aug 24 '15 at 09:35