[計程] resize的問題
後來發現老師的程式錯了一個不等號(只錯這邊)
助教叫我po出來的
物理二 黃全斌
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void get_names(vector <string> &);
void print_summary(const vector <string> &, const vector <int>);
void initialize(vector <int> &);
void tally(vector <int> &);
int main()
{ vector <string> cand_names(5);
vector <int> vote_counts(5);
get_names (cand_names);
vote_counts.resize(cand_names.size());
initialize(vote_counts);
tally(vote_counts);
print_summary(cand_names, vote_counts);
int bb; //這三行是因為我電腦有問題才加的 不用管
cout << "Program End. (Y)";
cin >> bb;
return 0;
}
void get_names(vector <string> & names)
{ ifstream names_in;
int cand=1;
names_in.open("name.txt");
names.resize(5);
while(names_in >> ws && !names_in.eof())
{ if(cand >= names.size())
//假如有4位候選人則name.size()=5
//所以應該要有等號
names.resize(names.size()+1);
names_in >> names[cand];
cand++;
}
names_in.close();
}
void print_summary(const vector <string> & names, const vector <int> counts)
{ int cand;
for (cand =1; cand<=counts.size(); cand++)
cout << names[cand] << ": " << counts[cand] << endl;
}
void initialize(vector <int> & counts)
{ int cand;
for (cand =1; cand<=counts.size(); cand++)
counts[cand]=0;
}
void tally(vector <int> & counts)
{ int cand;
ifstream vote_in;
vote_in.open("votes.txt");
while(vote_in >> ws && !vote_in.eof())
{ vote_in >> cand;
if(cand>=1 && cand<=counts.size())
counts[cand]++;
else cout << "Invalid vote:" << cand << endl;
}
vote_in.close();
}
--
麻煩本來就都是自找的 ╲|╱ __/ |
生性愛找麻煩的人 ─●─ / _╲╱ ̄▏
通常都有心情可以快快好起來的本領 ╱|╲ 那麼,就繼續吧。 ╱ ╲▏ |
之後繼續找更多麻煩 ● ▎
一直一直 ╱─┐ ╰ >▎
爬上去 by HugeSwallow __╲╥┘___ˍ▁ˍˍ_▁_∕/ ▎__
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59
※ 編輯: realtemper 來自: 140.112.7.59 (05/09 17:43)
推
140.112.12.156 05/09, , 1F
140.112.12.156 05/09, 1F
推
140.112.245.210 05/09, , 2F
140.112.245.210 05/09, 2F
※ 編輯: realtemper 來自: 140.112.245.210 (05/09 21:13)
推
140.112.12.156 05/10, , 3F
140.112.12.156 05/10, 3F