cout导致我的程序正常运行

时间:2014-03-30 02:59:48

标签: c++ cout dev-c++

我有一个非常奇怪的问题。下面是一个程序,它从文本文件中读取1到366之间的值,并输出一个文件,该文件创建不同值的频率表。 问题是,当我在sentinel for循环的开头删除cout语句时,程序不会做任何事情。然而,当cout语句存在时,它运行正常。这是怎么回事?

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

//CHECK IF DOUBLE IS INTEGER
bool isInt (double value) {
    double dummy;
    return bool(modf(value, &dummy) == 0);
}
//GET DATE AND CHECK FOR ERROR
int getDate(istream &is, int minimum, int maximum){
    double val;
    is >> val;
    is.clear();
    is.ignore(INT_MAX, '\n');
    if((val > maximum)||(val < minimum)||(is.fail())||!(isInt(val))){
         return 0;
    }
    return (int)val;
}
//SELECTION SORT, HIGHEST TO LOWEST
void sort(int count, int counter[], int dates[]){ 
    for(int i = 0; i < count; i ++){
         for(int j = i+1; j < count; j++){
               if(counter[i] < counter[j]){
                    swap(counter[i],counter[j]);
                    swap(dates[i],dates[j]);
               }           
         }
    }
}
//SWAP FUNCTION
void swap(int &v1, int &v2){
     int temp;
     temp = v1;
     v1 = v2;
     v2 = temp;
}
//MAKE TABLE
void produceTable(ostream &os, int counter[], int dates[], int nbDifDates, double nbTotalDates){
     //HEADERS
     os << " Day of Year     Events      %Events " << endl;
     os << "-------------------------------------" << endl;
     //INFO
     for(int i = 0; i < nbDifDates; i ++){
             os << setw(10) << dates[i] << setw(10) << counter[i] << setw(15) << setprecision(2) << fixed << ((counter[i]/nbTotalDates)*100) << endl;
     }

}
int main(){

    //IOSTREAM OBJECTS
    ifstream fin;
    ofstream fout;
    //FILE NAMES
    char ifilename[60];
    char ofilename[60];
    //DATA
    int eventDates[100] = {0};
    int counter[100] = {0};
    //NB OF DIFFERENT AND TOTAL DATE COUNTER
    int nbDifDates = 0;
    double nbTotalDates = 0;
    //CURRENT CHECKED DATE AND COMPARISON BOOLEAN
    int date;
    bool repeatedDate;

    //ASK FOR INPUT FILE
    cout << "Enter name of input file: ";
    cin >> ifilename;
    //OPEN FILE AND CHECK FOR ERROR
    fin.open(ifilename); 
    if(fin.fail()){
         cout << "Unable to open input file: " << ifilename << endl;
         system("PAUSE");
         return 0; 
    }
    //LOOP TILL END OF FILE
    for(;;){
         **cout << "-"; //NOTE, FOR SOME REASON CODE DOES NOT WORK WITHOUT THIS** 
         //RESET COMPARISON BOOLEAN
         repeatedDate = false;   
         //GET AND CHECK VALUE
         date = getDate(fin,1,366);
         //ERROR NOT FOUND
         if(date != 0){
             //CHECK FOR REPEATED DATE
             for(int j = 0; j < nbDifDates; j++){   
                  if(date == eventDates[j]){
                           //ADD TO CURRENT DATE COUNTER, REPEATED DATE FOUND
                           counter[j] ++;
                           repeatedDate = true;
                           break;
                  }
             }
             //NO SAME DATE FOUND, ADD NEW DATE
             if(!repeatedDate){
                  eventDates[nbDifDates] = date;
                  counter[nbDifDates]++;
                  nbDifDates++;
             }
             //ADD TO TOTAL DATES
             nbTotalDates ++;
         //ERROR FOUND, CHECK IF END OF FILE
         }else{  
             if(fin.eof()){
                  break;
             }else{
                  cout << "BAD DATA WAS ENTERED" << endl;
             } 
         }
    }
    //CLOSE INPUT FILE
    fin.close();

    //SORT DATA
    sort(nbDifDates, counter, eventDates);

    //ASK FOR OUTPUT FILE NAME
    cout << endl << "Enter name of output file: ";
    cin >> ofilename;

    //OPEN OUTPUT FILE
    fout.open(ofilename);
    //FAILED CLOSE PROGRAM
    if(fout.fail()){
         cout << "Unable to open output file: " << ifilename << endl;
         system("PAUSE");
         return 0; 
    }else{
         cout << endl << "The results have been written to the output file";
    }
    //PRODUCE TABLE AND CLOSE FILE
    produceTable(fout,counter,eventDates,nbDifDates,nbTotalDates);
    fout.close();
    //TEST
    cout << endl;
    produceTable(cout,counter,eventDates,nbDifDates,nbTotalDates);
    cout << endl;
    system("PAUSE");
    return 0;               
}

0 个答案:

没有答案