意外的C ++代码输出 [英] Unexpected Output of C++ Code

查看:167
本文介绍了意外的C ++代码输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ok Guys ... Im只是试图在这里练习结构,我做了这个C ++代码:

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

很明显这里的用户只需输入书名,作者等... ...它做到了,但它阻止了我,当我达到输入的书作者...意思是我无法得到的书作者,并给了我最printf()最最严重的anwser;我没有看到任何类似这样的东西。我认为我需要展示一个图像(btw没有警告或错误):

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

使用std :: string ....

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几乎所有的东西,但它dosent让我为作者输入! !查看图片更具描述性:

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


推荐答案

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

char means one single character. bookName is a single character. cin >> bookName; stores the first character you type, and only that first character.

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

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.

此外,您对 strcpy_s 使用了错误的参数数量,编译器应警告您这一点。在运行程序之前,始终修复所有编译器警告/错误。对于 printf ,还应有 #include

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 也有类似的问题。要解决这些问题,请停止使用字符和字符数组。改为使用 #include< string> ,然后使用 std :: string

bookAuthor has similar problems. To fix these problems, stop using chars and char arrays. Use #include <string> and then std::string instead.

这篇关于意外的C ++代码输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆