0

Ok Guys... Im Just trying to practice structs here and i made this C++ Code:

#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,*bookNamePointer = "", *bookAuthorPointer = "";
     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;

     printf("********** \n");

     cout << "Book Author ? " << endl;
     cin >> bookAuthor;



     strcpy_s(book1.name, &bookName);
     strcpy_s(book1.author, &bookAuthor);
     printf("Book Name %s \n", book1.name);
     printf("Book Author %s \n", book1.author);
    return 0;
}

Well Obviously here the user just enters the name of book,author,and so on... Well it did that but it stopped me when i reached inputing the Book Author... Meaning the I couldnt get the book author, and gave me the most wierdest anwser for my printf(); i havent seen anything wierd like this yet. I Think i will need to demonstrate an image(btw no warnings or error): enter image description here

EDIT

After i use std::string....

#include <iostream>
#include <cstring>
#include <string>

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;
     std::string 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;

     printf("********** \n");

     cout << "Book Author ? " << endl;
     cin >> bookAuthor;



    /* strcpy_s(book1.name, &bookName);
     strcpy_s(book1.author, &bookAuthor);
     printf("Book Name %s \n", book1.name);
     printf("Book Author %s \n", book1.author);*/
    return 0;
}

I Actually dont get to type for Book Author.. It Just stops. and say press a key to continue... Please Help!

EDIT2

#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;
     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 >> book1.name;

     cout << "Book Author ? " << endl;
     cin >> book1.author;

     cout << "Book Author: " <<book1.author << endl;
     cout << "Book Name: " << book1.name << endl;
     cout << "Date : " << book1.date.month << "/" << book1.date.date << "/" << book1.date.year;

    return 0;
}

Im Solid for almost everything but it dosent let me type for the author!!! Look at the image to be more descriptive:

enter image description here

SOLUTION

#include <iostream>
#include <cstring>



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;

     std::cout << "Date Of Publishing? " << std::endl;
     std::cin >> book1.date.date;
     std::cout << "Month Of Publishing?" << std::endl;
     std::cin >> book1.date.month;
     std::cout << "Year Of Publishing?" << std::endl;
     std::cin >> book1.date.year;


     std::cout << "Book Name ? " << std::endl;
     std::cin >> book1.name;

     std::cout << "Book Author ? " << std::endl;
     std::cin >> book1.author;

     std::cout << "Book Author: " <<book1.author << std::endl;
     std::cout << "Book Name: " << book1.name << std::endl;
     std::cout << "Date : " << book1.date.month << "/" << book1.date.date << "/" << book1.date.year << std::endl;

    return 0;
}
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • Yea @M.M They said to display a pointer --> http://stackoverflow.com/questions/36756336/no-instance-of-overloaded-function-strcpy-s-matches-the-argument-list .. And How do i fix? I Need help!! Btw im learning here: https://www.youtube.com/watch?v=aOVputhO2Ow&index=21&list=PLS1QulWo1RIYSyC6w2-rDssprPrEsgtVK – amanuel2 Apr 20 '16 at 23:29
  • Why are you mixing `printf` and `cout`? – Bob__ Apr 20 '16 at 23:30
  • Would it have killed you to trim off some of that graphic, which is 90 percent a sea of black? – Dan Korn Apr 20 '16 at 23:32
  • Isnt it the same thing? @Bob__ .. By same i mean doesnt it do the same thing? – amanuel2 Apr 20 '16 at 23:32
  • First of all, char is storing a single character. Secondly, why mix cout and printf? – Andrew Li Apr 20 '16 at 23:32

3 Answers3

4

char means one single character. bookName is a single character. cin >> bookName; stores the first character you type, and only that first character.

Then strcpy_s(book1.name, &bookName); causes undefined behaviour because the last argument is supposed to point to a string, but you supplied pointer to a single character instead.

Also you used the wrong number of arguments to strcpy_s, the compiler should warn you about this. Always fix all compiler warnings/errors before running the program. There should also be a #include for printf.

bookAuthor has similar problems. To fix these problems, stop using chars and char arrays. Use #include <string> and then std::string instead.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • cant i just do using namespace string; so i dont do std:: every time? – amanuel2 Apr 20 '16 at 23:33
  • 1
    You mean `using namespace std;`. also `cin>>book1.name;` will save the copy. – SHR Apr 20 '16 at 23:34
  • 3
    @AmanuelBogale you should learn from a book, C++ is not well suited to trial and error – M.M Apr 20 '16 at 23:35
  • Lol Yes @SHR ... Wow... M.M Any Books you would reccomend me? I was planning on first learning by viedo then book? Im not sure if its a good way lol – amanuel2 Apr 20 '16 at 23:38
  • Maybe start with ``The C Programming Language`` (up to you) then for C++ try ``C++ primer``, ``A Tour of C++`` or ``Accelerated C++``. These are **generally** considered good books. – nathanesau Apr 21 '16 at 00:00
  • 1
    NOooooooooooo! Don't do `using namespace std;` `using std::string` is OK, but `using namespace std` pulls the entire c++ standard library into the global namespace. Before you know it your nice little `reverse` function is fighting it out with `std::reverse` for ownership of the name and completely insane, nigh unreadable error messages result. That way lies madness. MADNESS! Read more here: [Why is “using namespace std” in C++ considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – user4581301 Apr 21 '16 at 00:03
  • On which books to learn from, here's the official list: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. If you are on a budget and can't afford Stroustrup's book, the first in the list and pretty much the gold standard if you have zero experience, he posts an abridged version here: https://isocpp.org/tour . Trying to learn C++ from the C Programming Language (a great book for learning C) is not recommended. While the syntax of the two languages is very similar, they differ greatly at points and the ideologies of C and C++ are frequently at odds. – user4581301 Apr 21 '16 at 07:12
1

You define bookName and bookAuthor as a single letter, a char. By using:

cin >> bookName; 

You read only one character, the rest of the line are still in buffer and will be read by next input operation. You should define those variables with type std::string, which is defined in string header (#include <string>).

struct Book {
    string name;
    string author;
    int id;
    DATE date;
};

and

string bookName, bookAuthor;

But you still will be reading only one word, without a leading space or any white space character, to read to the end of line you need to use std::getline:

getline( cin, bookName ); // read to the end of line, without new line char
book1.name = bookName; //simply copy string by assing
Arpegius
  • 5,817
  • 38
  • 53
1

Just for reference!

Generally prefer cout and cin to printf in C++. Also, you don't need to worry about std::string here - just read directly to the struct.

#include <iostream>
#include <cstring>

struct DATE 
{
   int Year;
   int Month;
   int Date;
};

struct Book 
{
   char  Name   [50];
   char  Author [50];
};


int main() 
{
   Book Book1;
   DATE Date1;

   std::cout << "Date Of Publishing? " << std::endl;
   std::cin >> Date1.Date;
   std::cout << "Month Of Publishing?" << std::endl;
   std::cin >> Date1.Month;
   std::cout << "Year Of Publishing?" << std::endl;
   std::cin >> Date1.Year;

   std::cout << "Book Name ? " << std::endl;
   std::cin >> Book1.Name;

   std::cout << "********** \n";

   std::cout << "Book Author ? " << std::endl;
   std::cin >> Book1.Author;

   std::cout << "Book Name \n" << Book1.Name << std::endl;
   std::cout << "Book Author \n" << Book1.Author << std::endl;
   return 0;
}
nathanesau
  • 1,681
  • 16
  • 27