簡體   English   中英

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

我實際上沒有輸入Book Author ..它只是停止了。 並說按一個鍵繼續...請幫助!

編輯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使用了錯誤數量的參數,編譯器應對此發出警告。 在運行程序之前,請務必修復所有編譯器警告/錯誤。 對於printf還應該有一個#include

bookAuthor有類似的問題。 要解決這些問題,請停止使用chars和char數組。 使用#include <string> ,然后使用std::string代替。

您將bookNamebookAuthor定義為一個字母,即一個字符。 通過使用:

cin >> bookName; 

您僅讀取一個字符,其余行仍在緩沖區中,將在下一個輸入操作中讀取。 您應該使用string標頭( #include <string> )中定義的std::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

僅供參考!

通常在C ++中更喜歡coutcincin 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM