已经有一段时间了(去年的Java课程).自从我的学校不提供C++以来,我一直在努力学习C++.我写了一个简单的程序,只是为了测试我到目前为止所学到的东西 - 实际上只是语法 - 然后才进入中间件.无论如何我只想强调我从不寻找答案,我宁愿你质疑我的物流,所以我可以重新思考一些事情,并可能自己完成.我认为既然我可以用Java成功地编写这个,那么在C++中一切都会很好,但我遇到了各种各样的问题.我试图调试并逐步完成,但我仍然不明白为什么我的一些变量没有得到我指定的值.如果你能指出我正确的方向,我会非常感激.
// This program will create any number of teams the user chooses,
// give each a score and calculate the average of all the teams.
#include <iostream>
using namespace std;
int main(){
//number of teams
int teamCount;
//array to keep scores
int team[0];
//total of scores
int total=0;
//average of all scores
int average=0;
cout<<"How many teams do you want to keep scores of?"<<endl;
cin>>teamCount;
//cout<<teamCount;
//ask the person for the score as many time
//as there are teams.
for(int i=0; i<teamCount; i++){
cout<< "Give me the score of team "<< i+1<<":"<<endl;
cin>>team[i];
total+=team[i];
}
average = teamCount/total;
//output the list of the scores
for(int i=0; i<teamCount; i++){
cout<<"Team "<<i+1<<" score is:"<<team[0]<<endl;
}
cout<<"and the average of all scores is "<<average<<endl;
return (0);
}
Run Code Online (Sandbox Code Playgroud)
你的阵列
int team[0];
Run Code Online (Sandbox Code Playgroud)
不适用于C++.顺便说一句,你不能这样分配0大小的数组
尝试c ++容器
std::vector<int> team;
Run Code Online (Sandbox Code Playgroud)