2013-02-23 77 views
-5
#include <iostream> 
using namespace std; 
void num7() 
{ 
    int numRows; 
    cin >> numRows; 
    for (int x = 0; x < numRows/2; x++) { 
     for (int y = 0; y <= x; y++) { 
      cout << "*"; 
     } 
     cout << "\n"; 
    } 
    float numRowsfloat = numRows; 
    double cos = numRowsfloat/2; 
    int tan = numRowsfloat/2; 
    double sin = tan; 
    if (cos == sin) 
     cout << "\n"; 
     for (int x = 0; x < numRows/2; x++) { 
      for (int y = numRows/2; y >0; y--) { 
       cout << "*"; 
      } 
     } 
    else 
     for (int x = 0; x < numRows/2+1; x++) { 
      for (int y = x; y >0; y--) { 
       cout << "*"; 
      } 
      cout << "\n"; 
     } 

} 

在else列中,它表示期望的表達式。 這是試圖使三角形形狀。像在C++中的預期表達式?

* 
** 
*** 
*** 
** 
* 

爲inputed 6 或

* 
** 
*** 
** 
* 

爲inputed 5

+2

將條件語句的開始和結束括號放在一起並再次編譯您的代碼。 – mumair 2013-02-23 06:31:25

回答

2

你忘了括號的if語句。試試這個:

if (cos == sin) { 
    cout << "\n"; 
    for (int x = 0; x < numRows/2; x++) { 
     for (int y = numRows/2; y >0; y--) { 
      cout << "*"; 
     } 
    } 
} else 
    for (int x = 0; x < numRows/2+1; x++) { 
     for (int y = x; y >0; y--) { 
      cout << "*"; 
     } 
     cout << "\n"; 
    } 
3

你的問題是這樣的:

if (cos == sin) 
    cout << "\n"; 
    for (int x = 0; x < numRows/2; x++) { 
     for (int y = numRows/2; y >0; y--) { 
      cout << "*"; 
     } 
    } 

這裏只有coutif語句的一部分。循環不是。您需要在整個模塊中添加大括號。