簡體   English   中英

C ++ cout不打印所有參數

[英]C++ cout not printing all args

我有以下代碼來打印出Car對象。 所有字段均可公開訪問。

void print_cars_array(Car cars[]) {
/**
 * Prints all cars in the given car array.
 */
  for(int i = 0; i < NUM_CARS; i++) {
      std::cout << "Car #" << i + 1 << std::endl;
      std::cout << cars[i].year << ' ' << cars[i].color << ' ' << cars[i].make << ' ' << cars[i].model << std::endl;
  }
}

但是,這給了我以下輸出:

Car #1
 Subaru Outback
Car #2
 Toyota Corolla
...

起初我以為前兩個字段搞砸了,但是修改了這個循環:

void print_cars_array(Car cars[]) {
/**
 * Prints all cars in the given car array.
 */
  for(int i = 0; i < NUM_CARS; i++) {
      std::cout << "Car #" << i + 1 << std::endl;
      std::cout << cars[i].year << std::endl;
      std::cout << cars[i].color << std::endl;
      std::cout << cars[i].year << ' ' << cars[i].color << ' ' << cars[i].make << ' ' << cars[i].model << std::endl;
  }
}

產生以下內容:

Car #1
2016
green
 Subaru Outback
Car #2
2006
white
 Toyota Corolla

我是否想知道為什么不打印這些內容? year外的所有字段均為字符串, year為int。

嘗試to_string()因為問題可能與int和字符串類型串聯有關

參考: http : //www.cplusplus.com/reference/string/to_string/

暫無
暫無

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

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