0

I am having troubles understanding this issue. The code is really basic but it behaves rather unexpectedly. The code is the simplified version of a routine to extract and save to a separate file the data for the day 15 of each month out of a daily database. Where is the problem ? The first cout prints the number of any day the outer if is entered. Then there are some conditional lines to select the right day ( not really important in this example ) and then there is the block, the inner if, that should print the data for the day 15 to the new file. Now, as you can see, while the outer loop ( attention ! ) is entered only on the 10th ( and this is already wrong - it should have written all the numbers from 11 to 15 ) but then it also prints that the file is written on the 15 ! Where is the problem ? The OUTER IF was entered only on the 10th, how can be the INNER IF be execute on the 15th ????

#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;

}

4

2 回答 2

7

if (day=15)分配 15day并返回true。你需要if (day==15)

于 2013-04-27T09:53:47.793 回答
1

当我简单地编译你的代码时,g++ -Wall我收到了一个警告:

error: suggest parentheses around assignment used as truth value

在这条线上:

if (day=15)  // if it is exactly day 15 print that day's result                                         

我愿意成为编译器告诉你你的问题是什么。我建议您在构建系统中启用所有警告。

于 2013-04-27T09:55:54.390 回答