2013-02-20 88 views
0

我想讓我的for循環輸出一個給定用戶特定的最大和最小鑽石,甚至不允許輸入。任何想法,將不勝感激。實踐並不像我想的那樣順利。C++鑽石控制檯輸出問題

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 

int rows, count; 

cout<<"What is the width of the diamond (3 to 21, odd values only): "; 
cin>>rows; 

//Error checking 
if(rows < 1 || rows > 25) 
    { 
     cout<<"Invalid. please enter an odd number from 1 to 25: "; 
     cin>>rows; 
    } 

//Ascending 
for (count = 1; count < rows; count += 1)  
    { 
     for (int rows = 0; rows < count; rows ++) 
     cout<<"*"; 
     cout<<endl; 
    } 

//Descending 
for (count; count > 0; count -= 1)    
    { 
     for (int rows = 0; rows < count; rows ++) 
     cout<<"*"; 
     cout<<endl; 
    } 


cin.get(); 
cin.get(); 

return 0; 

} 
+0

我們不打算做你的功課你。如果您有*特定*問題,請提問。 – metal 2013-02-20 21:35:49

+2

你需要檢查奇怪,你需要把空間,以使鑽石 – 2013-02-20 21:36:50

+0

這是一個挑戰,而不是作業。 – Klinetel 2013-02-21 02:42:28

回答

1

這執行objective-上半年:

for (int stars = 1; stars <= rows; stars+=2) 
{ 
     int padding = rows - stars; 
     for (int c = 0; c < padding/2; ++c) 
      cout<<" "; 
     for (int s = 0; s < stars; ++s) 
      cout<<"*"; 
     cout<<endl; 
}