0

For some reason a char cant go in strcopy_s();...

#include <iostream>
#include <cstring>

using namespace std;

struct DATE {
    int year;
    int month;
    int date;
};
struct Book {
    char name[50];
    char author[50];
    int id;
    DATE date;
};

int main() {
     Book book1;
     DATE date1;
     char bookName, bookAuthor;
     int date, year, month;

     cout << "Date Of Publishing? " << endl;
     cin >> date;
     cout << "Month Of Publishing?" << endl;
     cin >> month;
     cout << "Year Of Publishing?" << endl;
     cin >> year;
     date1.year = year;
     date1.month = month;
     date1.date = date;

     cout << "Book Name ? " << endl;
     cin >> bookName;
     cout << "Book Author" << endl;
     cin >> bookAuthor;
     strcpy_s(book1.name, bookName);
     strcpy_s(book1.author, bookAuthor);
    return 0;
}

Gives me the error:

Severity    Code    Description Project File    Line    Suppression State
Error (active)      no instance of overloaded function "strcpy_s" matches the argument list Struct  c:\Users\Amanuel\Documents\Visual Studio 2015\Projects\Struct\Struct\Source.cpp 38  

Severity    Code    Description Project File    Line    Suppression State
Error (active)      no instance of overloaded function "strcpy_s" matches the argument list Struct  c:\Users\Amanuel\Documents\Visual Studio 2015\Projects\Struct\Struct\Source.cpp 39  

Severity    Code    Description Project File    Line    Suppression State
Error   C2665   'strcpy_s': none of the 2 overloads could convert all the argument types    Struct  c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 38  



Severity    Code    Description Project File    Line    Suppression State
Error   C2665   'strcpy_s': none of the 2 overloads could convert all the argument types    Struct  c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 39  
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • just a typo: `bookName` is `char` instead of `char *` – fukanchik Apr 20 '16 at 22:29
  • Huh @fukanchik ? I Learned about pointers but?? How is that the problem here? – amanuel2 Apr 20 '16 at 22:32
  • 3
    https://msdn.microsoft.com/en-us/library/td1esda9.aspx `strcpy_s( char *, size_t, const char *);` the last one is `const char *` while you are calling: `char bookName, bookAuthor; ... strcpy_s(book1.name, bookName);` – fukanchik Apr 20 '16 at 22:35
  • Thanks a lot @fukanchik.... im still a begginer so i didnt know about it.... although it makes complete sense.. since you need to pass the pointer to actually change the value! Thanks a lot!! – amanuel2 Apr 20 '16 at 22:37

1 Answers1

4

Correct. strcpy and its family take char*, not char. They work on C strings. And you can't generally put a bookName into a single character anyway.

That said, welcome to the 21st century. We use std::string now, far easier.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I like how he accepts the answer but [completely ignores it in his next post](http://stackoverflow.com/questions/36756910/unexpected-output-of-c-code/) – M.M Apr 20 '16 at 23:34