2015-05-30 17 views
-1

我一直在這個程序上工作了幾個小時,但無法找到如何讓數字在星期六打響後循環。他們要麼走的方式通過它的權利,或者如果我添加和endl;他們上下。我如何循環在c + +日曆周圍的數字?

// This is how my output looks like (except they curve around they just go forever to the right: 

Number of days: 31 
Offset: 0 
Su Mo Tu We Th Fr Sa 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
+0

歡迎來到StackOverflow。如果不顯示正在生成它的代碼,並顯示輸出*應該看起來像什麼,則顯示輸出不是很有用。如果我不得不猜測,你的循環代碼在第7天后不會輸出換行符。 –

回答

0

這是什麼意思?

#include <iostream> 

using namespace std; 

int main() 
{ 
    int i; 
    for (i=1; i<=31; i++) { 
     cout << ((i<10) ? " " : "") << i << " "; 
     if (i%7==0) cout << endl; 
    } 
    return 0; 
} 

輸出:

1 2 3 4 5 6 7                                                     
8 9 10 11 12 13 14                                                     
15 16 17 18 19 20 21                                                     
22 23 24 25 26 27 28                                                    
29 30 31 

%符號是模運算符。它給出了餘下的部分。因此每7天除以7將剩餘零。這就是你如何檢查在哪裏放置換行符。