C++ 代码的意外输出

Unexpected Output of C++ Code

好的,伙计们...我只是想在这里练习结构,我制作了这个 C++ 代码:

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

好吧,显然这里用户只是输入书名、作者等...好吧,它做到了,但是当我输入书籍作者时它阻止了我...意思是我无法得到这本书作者,并为我的 printf(); 提供了最奇怪的答案;我还没有看到像这样奇怪的东西。我想我需要展示一张图片(顺便说一句,没有警告或错误):

编辑

使用后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;
}

我实际上不会为图书作者打字..它就停止了。并说按一个键继续...请帮忙!

编辑2

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

我对几乎所有东西都很可靠,但它不允许我为作者打字!!!查看图片以更具描述性:

解决方案

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

char 表示一个字符。 bookName 是单个字符。 cin >> bookName; 存储您键入的第一个字符,并且仅存储第一个字符。

然后 strcpy_s(book1.name, &bookName); 导致未定义的行为,因为最后一个参数应该指向一个字符串,但您提供了指向单个字符的指针。

此外,您为 strcpy_s 使用了错误数量的参数,编译器应该对此发出警告。始终在 运行 程序之前修复所有编译器 warnings/errors。 printf.

也应该有一个 #include

bookAuthor也有类似的问题。要解决这些问题,请停止使用字符和字符数组。使用 #include <string>,然后 std::string

您将 bookNamebookAuthor 定义为单个字母,一个字符。 通过使用:

cin >> bookName; 

您只读取了一个字符,该行的其余部分仍在缓冲区中,将在下一次输入操作时读取。 您应该定义类型为 std::string 的变量,这些变量在 string header (#include <string>).

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

string bookName, bookAuthor;

但是您仍然只会阅读一个单词,没有前导 space 或任何白色 space 字符,要阅读到您需要使用 std::getline 的行尾:

getline( cin, bookName ); // read to the end of line, without new line char
book1.name = bookName; //simply copy string by assing

仅供参考!

在 C++ 中,通常更喜欢 coutcin 而不是 printf。此外,您无需担心此处的 std::string - 只需直接读取结构即可。

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