我无法理解这个问题。该代码非常基本,但它的行为却出乎意料。该代码是例程的简化版本,用于从每日数据库中提取每月第 15 天的数据并将其保存到单独的文件中。哪里有问题 ?第一个 cout 打印输入外部 if 的任何一天的编号。然后是一些条件行来选择正确的日期(在这个例子中不是很重要)然后是 block ,内部 if,它应该将第 15 天的数据打印到新文件。现在,正如您所看到的,虽然外循环(注意!)仅在 10 日输入(这已经是错误的 - 它应该写入从 11 到 15 的所有数字)但随后它也打印出文件已写入15号!哪里有问题 ? OUTER IF 10号才进,INNER IF怎么15号执行????
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string newdata="mamma";
string risultato="-";
string PriorLine="-";
int PriorDay=0;
int lastmonth=0;
// Loops through all the months
for (int mese=1; mese <=12; mese++)
{
//Loops through all the days in the month
for(int day=1; day<=30; day++)
{
// at the month's beginning these 2 strings are set ="-"
if (mese != lastmonth)
{
risultato="-";
PriorLine="-";
}
// if we are between the 10th and the 20th and the result is still to be found
if (day>=10 && day<=20 && risultato=="-")
{
cout << day << endl; // LISTS ALL THE DAYS THIS LOOP IS ENTERED
if (day=15) // if it is exactly day 15 print that day's result
risultato=newdata;
// if that month in the data there is no day 15 and
// this is the second pass, choose the closest !
if (day>15 && PriorLine !="-")
{
if (abs(day-15)<=abs(15-PriorDay))
risultato=newdata;
else
risultato=PriorLine;
}
PriorLine=newdata;
PriorDay=day;
if (risultato != "-")
{
// writes the result ( risultato ) in a file
cout << "file written on the " << day << endl;
}
}
lastmonth=mese;
}
}
system("pause");
return 0;
if (day=15)
将 15 分配给 day
并返回 true
。你需要 if (day==15)
我是一名优秀的程序员,十分优秀!