C ++代码的意外输出

时间:2016-04-20 23:18:56

标签: c++ printf cin

Ok伙计们......我只是想在这里练习结构,我制作了这个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()给了我最最激动人心的anwser;我还没有看到像这样的奇怪事情。我想我需要演示一个图像(顺便说一句没有警告或错误): 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;
}

我实际上没有为书籍作者输入..它只是停止。然后按一个键继续...请帮助!

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

我几乎所有的东西都很坚固,但它让我为作者打字!看一下图像更具描述性:

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

3 个答案:

答案 0 :(得分:4)

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

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

你使用了错误数量的strcpy_s参数,编译器应该警告你这个。始终在运行程序之前修复所有编译器警告/错误。 #include也应该printf

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

答案 1 :(得分:1)

您将bookNamebookAuthor定义为单个字母,即char。 使用:

cin >> bookName; 

您只读取一个字符,该行的其余部分仍处于缓冲区中,并将通过下一个输入操作读取。 您应该定义std::string类型的变量,该变量在string标题(#include <string>)中定义。

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

string bookName, bookAuthor;

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

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

答案 2 :(得分:1)

仅供参考!

通常在C ++中更喜欢coutcinprintf。此外,您不必担心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;
}