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