C++ C++;计算负数和X以上的数
我正在做一个项目,在一个txt文件中有一个数字列表。C++程序是在这些数字的基础上,将它们放在适当的“桶”中,如果它们符合某些标准。我想把小于0的数字和大于200的数字放在一个桶里。但我似乎无法识别负数。想法C++ C++;计算负数和X以上的数,c++,C++,我正在做一个项目,在一个txt文件中有一个数字列表。C++程序是在这些数字的基础上,将它们放在适当的“桶”中,如果它们符合某些标准。我想把小于0的数字和大于200的数字放在一个桶里。但我似乎无法识别负数。想法 //GET IT STARTED IN HERE int main() { //VARIABLES ETC int score; //SET SCORE RANGES TO 0 int bucket[9] = {0,0,0,0,0,0,0,0,0}; ifstream scoreFi
//GET IT STARTED IN HERE
int main()
{
//VARIABLES ETC
int score;
//SET SCORE RANGES TO 0
int bucket[9] = {0,0,0,0,0,0,0,0,0};
ifstream scoreFile;
string file;
//OPEN UP THE SCORE FILE
cout << "Enter path to Score File: ";
cin >> file;
scoreFile.open(file.c_str());
if (file == "kill" || file == "KILL" || file == "Kill") {
cout << "Program terminated with KILL command" << endl;
return 0;
}
else
{
//CHECK FOR BAD PATH
while (!scoreFile)
{
cerr << "Wrong path" << endl;
cout << "Try path again: ";
cin >> file;
scoreFile.clear();
scoreFile.open(file.c_str());
}
}
//LOOK AT ALL THE SCORES FROM LAST WEEKS TEST
int scoreRow(1);
//CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
while (scoreFile >> score)
{
if (score <= 24)
bucket[0]++;
else if (score <= 49)
bucket[1]++;
else if (score <= 74)
bucket[2]++;
else if (score <= 99)
bucket[3]++;
else if (score <= 124)
bucket[4]++;
else if (score <= 149)
bucket[5]++;
else if (score <= 174)
bucket[6]++;
else if (score <= 200)
bucket[7]++;
//ADDED TWO EXTRA SCORES IN THE FILE TO TEST THIS AREA
else if (score < 0 || score > 200)
bucket[8]++;
scoreRow++;
}
//OUTPUT SOME RESULTS
cout << endl << "SCORE EVALUATION"<< endl;
cout << "Amount of students who scored 0 - 24: " << bucket[0] << endl;
cout << "Amount of students who scored 25 - 49: " << bucket[1] << endl;
cout << "Amount of students who scored 50 - 74: " << bucket[2] << endl;
cout << "Amount of students who scored 75 - 99: " << bucket[3] << endl;
cout << "Amount of students who scored 100 - 124: " << bucket[4] << endl;
cout << "Amount of students who scored 125 - 149: " << bucket[5] << endl;
cout << "Amount of students who scored 150 - 174: " << bucket[6] << endl;
cout << "Amount of students who scored 175 - 200: " << bucket[7] << endl;
cout << "Scores out of Range: " << bucket[8] << endl;
}
//从这里开始
int main()
{
//变量等
智力得分;
//将分数范围设置为0
int bucket[9]={0,0,0,0,0,0,0};
ifstream文件;
字符串文件;
//打开分数文件
cout>文件;
scoreFile.open(file.c_str());
如果(文件==“kill”| |文件==“kill”| |文件==“kill”){
cout这里的每个分数只能进入一个区间。如果分数<0,将由第一个If语句进行评估并放入该区间
因此,您的最后一个else if()将只捕获超过200的值,如果您有任何负值,它们将全部落在第一个桶中。您希望将最后一个条件放在顶部
//CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
while (scoreFile >> score)
{
if (score < 0 || score > 200)
bucket[8]++;
else if (score <= 24)
bucket[0]++;
else if (score <= 49)
bucket[1]++;
else if (score <= 74)
bucket[2]++;
else if (score <= 99)
bucket[3]++;
else if (score <= 124)
bucket[4]++;
else if (score <= 149)
bucket[5]++;
else if (score <= 174)
bucket[6]++;
else if (score <= 200)
bucket[7]++;
scoreRow++;
}
//检查每一个并将其添加到相应的BUCKET中
while(scoreFile>>分数)
{
如果(分数<0 | |分数>200)
桶[8]++;
else if(score)显示您的分数声明。如果分数文件名为“kill”,会发生什么情况?@Neil Kirk:然后程序终止。不,如果您输入“kill”,会发生什么情况在您的程序中。如果分数文件名为kill,则会发生什么情况?用户无法使用您的程序来处理它。您可能希望在向客户发布之前解决此问题!?!!?可以在您的程序中打开名为kill的文件,方法是先输入错误的文件名,然后输入kill;)非常感谢。我不知道我是怎么看得过份的。可能是因为我是学这个的学生,但再次感谢你。@BerryBlue为什么所有的额外案例?你只需要3个案例:if(score<0||score>200)bucket[8]++;else if(score==200)bucket[7]++;else bucket[score/25]++;