Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If I put this c++ program in a do..while cycle, it counts how manytimes the cycl

ID: 3608022 • Letter: I

Question


If I put this c++ program in a do..while cycle, it counts how manytimes the cycle is running and print my results that number oftimes. Do you know how to fix that, I just want to print theresults once

Here is the code:


#include<iostream>
#include <stdio.h>
#include <time.h>
#include<fstream>
using namespace std;

struct event{
       time_t t;
       char date[20];
       char str[1000];
       }event_arr[1000];
int counter=0;


void print_by_date(int yr,int mon)
{
    
     int i;
     struct tm *t;
     int count=0;
     for(i=0;i<counter;i++)
     {
         t=gmtime(&event_arr[i].t);
         if(t->tm_mon == mon -1 && t->tm_year == yr-1900)
          {
              cout<<event_arr[i].date<<" ---------->"<<event_arr[i].str<<endl;
              count++;
          }
     }
     if(count ==0)
     cout<<"There is no record on thisdate ";
}

int insert_event(char *d, char *s)
{
   struct tm time_check;
   time_t n;
   char temp[200];
   char *yr,*mon,*day;
   
    strcpy(event_arr[counter].date,d);
    strcpy(event_arr[counter].str,s);
   
   strcpy(temp,d);
   yr=strtok(temp,"-");
   mon=strtok(NULL,"-");
   day=strtok(NULL,"-");
  
   time_check.tm_year = atoi(yr) - 1900;
   time_check.tm_mon = atoi(mon) - 1;
   time_check.tm_mday = atoi(day);
   time_check.tm_hour = 0;
   time_check.tm_min = 0;
   time_check.tm_sec = 1;
   time_check.tm_isdst = -1;

   if ((n=mktime(&time_check)) == -1)
      time_check.tm_wday = 7;
     
      event_arr[counter].t=n;
      counter++;
      return 0;
}

void arrange()
{
     int i,j;
              
     struct event temp;
     for(i=0;i<counter;i++)
     for(j=i+1;j<counter;j++)
     {
     if(event_arr[i].t > event_arr[j].t)
                      {
                      temp = event_arr[i];
                      event_arr[i]= event_arr[j];
                      event_arr[j]=temp;                      
                      }
     }
}
int main(void)
{
   int k = 0;  
do{
   struct tm time_check;
   int i;
   int year, month, day;
   time_t n;
   char str[1000],temp[1000];
   char date[20];
   char *ptr;
/* Input a year, month and day to find the weekday for */



   ifstream ifs;
   int yr,mon;
   ifs.open("calendar.txt");
   if(ifs.is_open())
   {

while(ifs.getline(str,sizeof(str)))
{

      memset(date,0,sizeof(date));
   //    cout<<str<<endl;
       ptr=strstr(str," ");
       if(ptr == NULL)
       {
       cout<<"Invalid string"<<temp<<endl;
       if(ifs.eof())
       break;
       continue;
       }
      strncpy(date,str,(int)((long)ptr-(long)str));
       strcpy(temp,ptr+1);
    //        cout<<date<< "      "<<temp<<endl;
       insert_event(date,temp);
       if(ifs.eof())
       break;
       }

       arrange();   /*Arrange in ascending order */

   cout<<"Search yr :";
   cin>>yr;
   cout<<"Search mon :";
   cin>>mon;
   print_by_date(yr,mon);
   k++;
   }
  
} while(k <=5);
  
   system("pause");
   return 0;
}

Explanation / Answer

If I put this c++ program in a do..while cycle, it counts how manytimes the cycl

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote