C++ functions loop twice

depending on its location in relation to other functions

Page 1 of 1

1 Replies - 1276 Views - Last Post: 27 July 2009 - 12:18 PM Rate Topic: -----

#1 akokoza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 09

C++ functions loop twice

Post icon  Posted 26 July 2009 - 11:28 PM

After inputting the number of days on the trip, it outputs the "Enter the time...: " twice.

It does still this if you switch the order of f_atime() and f_dtime() functions in calculate().

However, if you take the f_days() function out of calculate(), it will cout "Enter the time...: " only once, as it is supposed to.

If you compile and run the code posted below, you should see what I mean...

This post has been edited by akokoza: 27 July 2009 - 06:22 PM

Is This A Good Question/Topic? 0
  • +

Replies To: C++ functions loop twice

#2 akokoza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 09

Re: C++ functions loop twice

Posted 27 July 2009 - 12:18 PM

#include <iostream>
#include <sstream>
using namespace std;

//note: this is just a small portion of the program, but it should show you the problem...

void calculate(); //main function
int f_days(); //Number of days spent on trip
int* f_atime(); //Time of arrival on the last day
int* f_dtime(); //Time of departure on the first day

int main ()
{
	calculate();
	system("PAUSE");
	return 0;
}

int f_days(){
	static int days=-1;
	do{
	   cout<<"Total number of days spent on business trip: ";
	   cin>>days;
	}while (days<0);
	return days;
}

int* f_atime(){
	int hour, minute;
	string AmPm;
	do{
	   cout << "Time of arrival on the last day (e.g. 7:15 am): ";
	   string time;
	   getline(cin,time);
	   stringstream Stream(time);
	   char null;
	   Stream>>hour>>null>>minute;
	   Stream>>AmPm;
	} while(hour<=0||hour>12||minute<0||minute>=60||AmPm!="am"&&AmPm!="pm");
	int ap; //converts the string used for AM/PM into integer 1 or 2
	if (AmPm=="am")
	ap=1;
	else if(AmPm=="pm")
	ap=2;
	if (minute>0){
	   if(hour<12)
	   hour++;
	   else
	   hour=1;
	}
	static int atime[2];
	atime[1]=hour;
	atime[2]=ap;
	return atime;
}

int* f_dtime(){
	int hour, minute;
	string AmPm;
	do{
	   cout << "Time of departure on the first day (e.g. 7:15 am): ";
	   string time;
	   getline(cin,time);
	   stringstream Stream(time);
	   char null;
	   Stream>>hour>>null>>minute;
	   Stream>>AmPm;
	} while(hour<=0||hour>12||minute<0||minute>=60||AmPm!="am"&&AmPm!="pm");
	int ap; //converts the string used for AM/PM into integer 1 or 2
	if (AmPm=="am")
	ap=1;
	else if(AmPm=="pm")
	ap=2;
	static int dtime[2];
	dtime[1]=hour;
	dtime[2]=ap;
	return dtime;
}

void calculate(){
	static int days; //if you move this function below the other two (or delete it all togethe)
	days=f_days(); //the output of f_dtime and f_atime will be normal
	cout<<endl; 

	static int* dep;
	dep=f_dtime(); //this function will output twice
	cout<<endl;
	
	static int* arr; 
	arr=f_atime(); //if you put this function above f_dtime, then this function will output twice
	cout<<endl;
	
	cout<<dep[1];
	if(dep[2]==1)
	cout<<"am\n";
	else
	cout<<"pm\n";
	
	cout<<arr[1]; 
	if(arr[2]==1) 
	cout<<"am\n";
	else
	cout<<"pm\n";
}


This post has been edited by akokoza: 27 July 2009 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1