gpt4 book ai didi

c++ - C++ 代码的意外输出

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:51 24 4
gpt4 key购买 nike

好的,伙计们...我只是想在这里练习结构,我编写了这个 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(); 提供了最奇怪的答案;我还没有看到像这样奇怪的东西。我想我需要展示一张图片(顺便说一句,没有警告或错误): enter image description here

编辑

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

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

enter image description here

解决方案

#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 使用了错误数量的参数,编译器应该警告你这一点。在运行程序之前始终修复所有编译器警告/错误。还应该有一个 #include对于 printf .

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

关于c++ - C++ 代码的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com