2013-02-11 97 views
1

爲什麼這段代碼無效?xcode scanf不能正常工作c

基本點子:

每輛車可以有4人在裏面。每列火車的第一輛車長10英尺,其後每輛長8英尺。 列車只能佔據軌道總長度的25%。
目標是打印出可以填充火車軌道的最大人數,以及用戶輸入的最大軌道長度和最大列車長度。

//Roller Coaster Capacity 
//Justin Castillo, Section 1, COP 3223, 2/9/2013 
//This program calculates the maximum number of people that can be on the 
//track at one time. 

int main(void) { 

    int max_people_train; 
    int num_cars_train; 
    int total_num_trains; 
    int max_length_track; 
    int max_length_train; 
    int max_people_track; 
    int prev_max_people_track; 
    int prev_total_num_trains, i; 

    printf("Please enter the track length:\n"); 
    scanf("%d", &max_length_track); 

    printf("Please enter the max length of the train:\n"); 
    scanf("%d", &max_length_train); 

    for (i=10; i<=max_length_train; i+8) { 
     num_cars_train = ((i-10)/8 +1); 
     max_people_train = (num_cars_train)*4; 
     total_num_trains = max_length_track/i; 
     max_people_track = total_num_trains*max_people_train; 

     prev_max_people_track = prev_total_num_trains * max_people_train; 
     prev_total_num_trains = max_length_track/(i-8); 

     if (prev_max_people_track > max_people_track) 
      max_people_track = prev_max_people_track; 
    } 



    printf("Your ride can have at most %d people on the track,\n", max_people_track); 
    printf("This can be achieved with trains of %d cars.\n", num_cars_train); 


    system("pause"); 
    return 0; 
} 

回答

1

我猜它有事情做與此:

for (i=10; i<=max_length_train; i+8) { 
// ============================^^^ WTF ? 

嘗試:

for (i=10; i<=max_length_train; i+=8) { 
+0

謝謝夥計! – user2044189 2013-02-11 03:58:49

+0

@ user2044189如果這個答案解決了你的問題,你應該接受它作爲正確的答案。常見問題的這一部分[我如何在這裏提問?](http://stackoverflow.com/faq#howtoask)解釋瞭如何做到這一點。 – Nocturno 2013-02-11 04:20:23

+0

@Nocturno我懷疑他已經走了很久,但我當然很欣賞你的投票權,先生。謝謝。 – WhozCraig 2013-02-11 04:21:40