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;
}
5
  • Yea @M.M They said to display a pointer --> stackoverflow.com/questions/36756336/… .. And How do i fix? I Need help!! Btw im learning here: youtube.com/… Commented Apr 20, 2016 at 23:29
  • Why are you mixing printf and cout? Commented Apr 20, 2016 at 23:30
  • Would it have killed you to trim off some of that graphic, which is 90 percent a sea of black? Commented Apr 20, 2016 at 23:32
  • Isnt it the same thing? @Bob__ .. By same i mean doesnt it do the same thing? Commented Apr 20, 2016 at 23:32
  • First of all, char is storing a single character. Secondly, why mix cout and printf? Commented Apr 20, 2016 at 23:32

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

cant i just do using namespace string; so i dont do std:: every time?
You mean using namespace std;. also cin>>book1.name; will save the copy.
@AmanuelBogale you should learn from a book, C++ is not well suited to trial and error
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
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?
|
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

Comments

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;
}

4 Comments

you should try ... using namespace; but thanks a lot!!
For this example you're right! But sometimes that isn't a good practice
Updated Code Nathan... Spot any problems with the author printing?
Guess what... the problem was with using std:: i had to use it... it was calling the wrong functions i think... gosh the 7th comment of first anwser was right...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.