2016-07-15 47 views
-3
#include <cstdlib> 
#include <iostream> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 

    int SIZE = 10; 
    int NUMBERS[SIZE]; 
    int i; 
    int j; 
    int temp; 

    for((i = 0) to (SIZE - 2)) 
    { 
      for((j = 0) to (SIZE - 2)) 
      { 
        if(NUMBERS[j] < NUMBERS[j + 1]) 
        { 
        temp = NUMBERS[j] 
        NUMBERS[j] = NUMBERS[j+1] 
        NUMBERS[j+1] = temp 
        } 
      } 
    } 

    cout << "Sorted list"; 
    cout << "==========="; 

    for((i = 0) to (SIZE - 1)) 
    { 
     cout << "Number ", i + 1, ": ", NUMBERS[i] 
    } 


    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

总是收到:Dev C++预计`;'之前“到”,预期“)'之前';''令牌

线18 & 34:“之前 '预期;' before "to" line 32: expected);'令牌

我想不通为什么。 任何帮助,非常感谢!

+0

'SIZE'应'常量为size_t SIZE = 10;'' –

+3

为(第(i = 0)到(SIZE - 1))',这不是有效的C++语法。 –

+2

你在哪里学的'((I = 0)至(SIZE - 2))'是正确的语法[for循环(http://en.cppreference.com/w/cpp/language/for) ? – NathanOliver

回答

0

你已经错过了一些;代码(和其他一些)。试试这个版本:

using namespace std; 

int main(int argc, char *argv[]) 
{ 

    int SIZE = 10; 
    int NUMBERS[SIZE]; 

    int temp; 

    for(int i = 0; i< SIZE - 2; i++) 
    { 
      for(int j = 0; j < SIZE - 2; j++) 
      { 
        if(NUMBERS[j] < NUMBERS[j + 1]) 
        { 
        temp = NUMBERS[j]; 
        NUMBERS[j] = NUMBERS[j+1]; 
        NUMBERS[j+1] = temp; 
        } 
      } 
    } 

    cout << "Sorted list"; 
    cout << "==========="; 

    for(int i = 0; i < SIZE - 1; i++) 
    { 
     cout << "Number ", i + 1, ": ", NUMBERS[i]; 
    } 


    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

请考虑你应该初始化NUMBERS

+0

对不起@πάνταῥεῖ,现在我才意识到,他们可能是我错过了 – meJustAndrew

+1

恐怕没有办法给出一个简洁的答案来解决代码中的所有错误,(有解释)其他的事情和我认为这个问题是对任何未来的研究都有用。我们应该彻底摆脱这个问题,我一直在投票结束它。 –

+0

我想你是对的... frotunately – meJustAndrew

-1

尝试:

for(i = 0; i <= (SIZE - 2); i++) 
{ 
     for(j = 0; j<=(SIZE - 2); j++) 
     { 
       if(NUMBERS[j] < NUMBERS[j + 1]) 
       { 
       temp = NUMBERS[j]; 
       NUMBERS[j] = NUMBERS[j+1]; 
       NUMBERS[j+1] = temp; 
       } 
     } 
} 
cout << "Sorted list"; 
cout << "==========="; 

for(int i = 0; i <= SIZE - 1; i++) 
{ 
    cout << "Number ", (i + 1), ": ", NUMBERS[i]; 
} 
system("PAUSE"); 
return EXIT_SUCCESS; 
+0

没有看到分号丢失。感谢oreo。 –