code.club

 找回密碼
 立即註冊
搜索
查看: 5974|回復: 0

一個有點詭異的C++程式

[複製鏈接]
發表於 2019-4-6 19:14:30 | 顯示全部樓層 |閱讀模式
用dev c++編譯時,在第三個元素時一直出現3的答案。在Visual Studio 2017時雖然都正常列出元素,但最後會報當掉的結果。怪哉....

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>

  4. using namespace std;

  5. void test(int *a){
  6.         int i=0;
  7.        
  8.         char ch;
  9.         do{
  10.                 cout<<"Enter a number: ";
  11.                
  12.                 cin>>a[i];
  13.                 a[i+1] = -1;
  14.                 cout<<"a["<<i<<"] is "<<a[i]<<" again?(y/n)";
  15.                 i++;
  16.                 cin>>ch;
  17.         }while(ch!='n');
  18. }
  19.        

  20. int main(){
  21.         int x[] = {9,12,121,888,4,1312,349,-1};
  22.         //test(x);
  23.         int i =0;
  24.         while(x[i] != -1) {
  25.                 cout<<"x["<<i<<"] is "<<x[i]<<endl;
  26.                 i++;
  27.         };
  28.        
  29.         return 0;
  30. }
複製代碼
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-28 16:42 , Processed in 0.070409 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表