Reputation: 4646
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):
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!
#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:
#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;
}
Upvotes: 0
Views: 406
Reputation: 1721
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;
}
Upvotes: 1
Reputation: 5887
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
Upvotes: 1
Reputation: 141544
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.
Upvotes: 4