1

Hello I have question about some code. Is this code supose to work as it is here?

I thought I would need to use #include cstring

I asked my teacher and he told me that the code is good as it is and that it should work with #include string

Is this correct? can someone explain me please ?thank you.

#include <iostream>
#include <string> //strcpy() works with string?
using namespace std;

class libraryBook{

  private:

    char title [80]; //cstring
    int available;

  public:

    libraryBook(char initTitle[]);//cstring as argument

};


libraryBook::libraryBook(char initTitle[]){

  strcpy(title, initTitle); 
  available = 1;

}


int main() {

  libraryBook b1 ("computing"); //what would be the output without changing the code ?

  return 0 ;
}
juan
  • 113
  • 4
  • 6
    Since you're in C++, why not use `std::string` instead of arrays of `char` ? – Louen Apr 10 '19 at 23:48
  • What parts don't you understand? Note: the current code will go horribly wrong if you pass an 80+ character string to libraryBook's constructor, can you figure out why? – Dave S Apr 10 '19 at 23:50
  • im not supose to change the code :(.The exercise was just about guessing the output. – juan Apr 10 '19 at 23:52
  • I'm confused; your professor said it should work with ``, right? So the @Louen would be correct – Jose Fernando Lopez Fernandez Apr 10 '19 at 23:54
  • Yea it doesn't make much sense to give you the string library but force you to pass a char array. – jwolsborn Apr 10 '19 at 23:58
  • 4
    Methinks you want `#include `, not `#include ` to get `strcpy`. `strcpy` is a good ol' C function and all the ol' C stuff is in headers that start with c – user4581301 Apr 10 '19 at 23:58
  • 1
    If you want to copy a string literal `.copy()' is the function – jwolsborn Apr 10 '19 at 23:59
  • 1
    Like @user4581301 said, `strcpy` is [not in ](https://en.cppreference.com/w/cpp/header/string) – Jose Fernando Lopez Fernandez Apr 10 '19 at 23:59
  • 1
    Sidenote: `libraryBook(char initTitle[]);` will be a bit more versatile as `libraryBook(const char *initTitle);` You'll be able to eat string literals safely and without errors. In modern Standard C++ `libraryBook b1 ("computing");` is flat-out illegal to protect you from accidentally writing to read-only storage. – user4581301 Apr 11 '19 at 00:00
  • Dunno about your compiler, but my compiler (and my brain) don't like the fact that you are passing a string-literal to a method via a non-const-qualified argument: `temp.cpp:29:19: warning: conversion from string literal to 'char *' is deprecated [-Wc++11-compat-deprecated-writable-strings]` – Jeremy Friesner Apr 11 '19 at 00:05
  • ok , got it. I least I'm sure now that it won't work like that .Now I can ask my teacher again to doble check my answer .(thank you for the comments) – juan Apr 11 '19 at 00:06
  • No worries. It's only one letter so I wrote the error off as a typo. It's astounding how easy it is for the human brain to insert a or miss a character. A good trick to check yourself when the compiler says you've screwed up is to pop *man * into google and see what comes back at you. The man pages usually bring up what header to include pretty early. [It's the first line in the synopsis here.](http://man7.org/linux/man-pages/man3/strcpy.3.html). For C++ stuff [go to cppreference](https://en.cppreference.com/w/cpp/string/byte/strcpy). It's a pretty reliable reference. – user4581301 Apr 11 '19 at 00:12
  • If all else fails, [here's a link to an up-to-date draft of the C++ Standard.](https://timsong-cpp.github.io/cppwp/) The Standard's the final word on what is and isn't valid C++, but it's meant to be read by 20th level multi-class Programmer/Lawyers, so don't get upset if it looks like it's written in Martian. – user4581301 Apr 11 '19 at 00:14

3 Answers3

1

In short, "as is", the program may or may not compile. You need to include <cstring> if you want the strcpy() function (as pointed out in the comments by @user4581301.

After including <cstring>, the output of the program is nothing since you're printing nothing out. Realistically though, you shouldn't be using character arrays in lieu of std::string in C++. A demo of your code can be found here.

Developer Paul
  • 1,502
  • 2
  • 13
  • 18
1

TL;DR

Use <cstring> ranther than <string>, but even when the header is corrected, the program has no output.

Discussion

I thought I would need to use #include cstring

I asked my teacher and he told me that the code is good as it is and that it should work with #include string

You thought right and the teacher is wrong1. The C++ Standard does not guarantee that strcpy is made available by including <string>.

1 Sort-of wrong. There are no guarantees that <string> provides strcpy or a header chain that ultimately includes <cstring>, but no one said that it couldn't. Just don't count on it. A file should always include all of the headers it requires2 to prevent avoidable errors. The teacher may also have been fooled by their brain into seeing a c where there was no c when they told you your code was correct. They may have meant for you to use the old C header <string.h>. It's hard to tell.

2 Sometimes you'll find a header that you'd expect to contain another header instead forward declares the parts of the other header that it needs to avoid the compile-time overhead of including the other header.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
1

At least in my opinion, your teacher's idea was clearly better. A halfway reasonable starting point would be something like this:

#include <string>

class LibraryBook { 
    std::string name;
    int available;
public:
    LibraryBook(std::string const &name, int available = 1) 
        : name(name)
        , available(available) 
    {}
};

Then creating a book would look something like this:

LibraryBook book("Steal This Code");

Since we haven't included any code to write anything out, this won't produce any output (other than returning a code to indicate successful exit).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111