Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need help with clock Posted by tabyrne on 5 Nov 2007 at 2:05 PM
I'm really new to this, if fact just started 4 weeks ago. I'm trying to calculate the difference between to time.
I know where i'm going wrong just don't know how to rectify it.
Got time in and got time out, but time is in 60 minutes and not 100.
Please help.

int convert(char time[5])

{
int time_mins;
int int_time[4];

int_time[0]=time[0]-'0';
int_time[1]=time[1]-'0';
int_time[2]=time[2]-'0';
int_time[3]=time[3]-'0';

time_mins = int_time[0]* 1000 + int_time[1] * 100 + int_time[2] * 10 + int_time[3];
return time_mins;



}

void calctime ()
{
cout<<""<<endl;
cout<<" Please enter the start time of your call"<<endl;
cout<<""<<endl;
cin>> chstart;
cout<<""<<endl;

starttime = convert(chstart);
cout<<""<<endl;
cout<< starttime;
cout<<""<<endl;

cout<<""<<endl;
cout<<" Please enter the time your call ended"<<endl;
cout<<""<<endl;
cin>> chend;
cout<<""<<endl;

Endtime = convert(chend);
cout<<""<<endl;
cout<< Endtime;




totmins = (Endtime - starttime);

cout<<""<<endl;
cout<<" Your total minutes are: "; cout <<totmins;
cout<<""<<endl;


tothrs =(totmins / 60);

cout<<""<<endl;
cout<<" Your total hours are: "; cout <<tothrs;

cout<<""<<endl;
}
Report
Re: Need help with clock Posted by BitByBit_Thor on 5 Nov 2007 at 2:40 PM
: I'm really new to this, if fact just started 4 weeks ago. I'm trying
: to calculate the difference between to time.
: I know where i'm going wrong just don't know how to rectify it.
: Got time in and got time out, but time is in 60 minutes and not 100.

So, what's stopping you from changing the 100 into a 60? And thus, the 1000 into 600?

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Need help with clock Posted by tabyrne on 5 Nov 2007 at 2:44 PM

: So, what's stopping you from changing the 100 into a 60? And thus,
: the 1000 into 600?
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

I know that sounds like it should work but it doesn't. But thanks anyway
Report
Re: Need help with clock Posted by BitByBit_Thor on 5 Nov 2007 at 3:16 PM
: I know that sounds like it should work but it doesn't. But thanks
: anyway
:

Changing it to 60, respectively 600 will make that part of the code work (for if the characters entered are in the format: "1230" for "12:30").

What's not working must be somewhere else. One thing I noticed is that your variables starttime and Endtime are not declared anywhere, and that the sub that is supposed to read the time is never called. I guess you didn't post the full code. Please do and we can find where the problem lies. Also, it helps if you tell what input you used and what output you got (and how that's wrong).

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Need help with clock Posted by tabyrne on 5 Nov 2007 at 3:22 PM
: : I know that sounds like it should work but it doesn't. But thanks
: : anyway
: :
:
: Changing it to 60, respectively 600 will make that part of the code
: work (for if the characters entered are in the format: "1230" for
: "12:30").
:
: What's not working must be somewhere else. One thing I noticed is
: that your variables starttime and Endtime are not declared anywhere,
: and that the sub that is supposed to read the time is never called.
: I guess you didn't post the full code. Please do and we can find
: where the problem lies. Also, it helps if you tell what input you
: used and what output you got (and how that's wrong).
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

Thank you for your time, this is the full code:

#include <iostream.h>
#include <stdio.h>

char day, chday,chstart[3],chend [3];
int starttime, Endtime, totmins, tothrs;




// convert function is called to change the times from characters to interger



void getinfo()
{
cout<<""<<endl;
cout<<"---- Please Select The Appropriate Code ----"<<endl;
cout<<""<<endl;
cout<<"---------------For Your Day------------------"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"\nM - Monday \nU - Tuesday \nW - Wednesday \nT - Thursday \nF - Friday \nA - Saturday \nS - Sunday";
day = false;


// 'while' loop to check on correct day

while (!day)
{
cout<<""<<endl;
cout<<""<<endl;
cout<<"Please select the correct day and enter the code"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cin>>chday;
if ((chday == 'S')|| (chday == 'M')||(chday == 'U')|| (chday == 'W')||(chday == 'T')|| (chday == 'F')||(chday == 'A'))
{

day = true;

cout <<endl;
}

else
{
day = false;
cout << " You have entered an invalid day please re-enter your day";

}


}
}

void outday()

{
switch (chday)
{

case 'M':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Monday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'U':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Tuesday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'W':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Wednesday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'T':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Thursday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'F':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Friday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'A':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Saturday";
cout<<""<<endl;
cout<<""<<endl;
break;

case 'S':
cout<<""<<endl;
cout<<""<<endl;
cout<<" You have choosen Sunday";
cout<<""<<endl;
cout<<""<<endl;
break;
}

}

int convert(char time[5])

{
int time_mins;
int int_time[4];

int_time[0]=time[0]-'0';
int_time[1]=time[1]-'0';
int_time[2]=time[2]-'0';
int_time[3]=time[3]-'0';

time_mins = int_time[0]* 1000 + int_time[1] * 100 + int_time[2] * 10 + int_time[3];
return time_mins;




}

void calctime ()
{
cout<<""<<endl;
cout<<" Please enter the start time of your call"<<endl;
cout<<""<<endl;
cin>> chstart;
cout<<""<<endl;

starttime = convert(chstart);
cout<<""<<endl;
cout<< starttime;
cout<<""<<endl;

cout<<""<<endl;
cout<<" Please enter the time your call ended"<<endl;
cout<<""<<endl;
cin>> chend;
cout<<""<<endl;

Endtime = convert(chend);
cout<<""<<endl;
cout<< Endtime;




totmins = (Endtime - starttime);

cout<<""<<endl;
cout<<" Your total minutes are: "; cout <<totmins;
cout<<""<<endl;


tothrs =(totmins / 60);

cout<<""<<endl;
cout<<" Your total hours are: "; cout <<tothrs;

cout<<""<<endl;


}







void main()

{
getinfo();
outday();
calctime();

}

Report
Re: Need help with clock Posted by BitByBit_Thor on 6 Nov 2007 at 2:27 AM
: Thank you for your time, this is the full code:
:

#include <iostream> //Use iostream instead of iostream.h
#include <stdio.h>

//You forgot this. With this you can cout/cin/etc without std:: infront of it
using namespace std;

//You assume later in the code that times are 5 chars long, 
// but here only 3! I changed it to 5
char day, chday,chstart[5],chend[5];
int starttime, Endtime, totmins, tothrs;

// convert function is called to change the times from characters to interger
void getinfo()
{
	cout<<""<<endl;
	cout<<"---- Please Select The Appropriate Code ----"<<endl;
	cout<<""<<endl;
	cout<<"---------------For Your Day------------------"<<endl;
	cout<<""<<endl;
	cout<<""<<endl;
	cout<<"\nM - Monday \nU - Tuesday \nW - Wednesday \nT - Thursday \nF - Friday \nA - Saturday \nS - Sunday";
	day = false;

	// 'while' loop to check on correct day

	while (!day)
	{ 
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<"Please select the correct day and enter the code"<<endl;
		cout<<""<<endl;
		cout<<""<<endl;
		cin>>chday;
		if ((chday == 'S')|| (chday == 'M')||(chday == 'U')|| (chday == 'W')||(chday == 'T')|| (chday == 'F')||(chday == 'A'))
		{
			day = true;

			cout <<endl;
		}
		else 
		{ 
		day = false;
		cout << " You have entered an invalid day please re-enter your day";
		}
	}
}

void outday()
{
	switch (chday)
	{
	case 'M':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Monday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'U':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Tuesday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'W':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Wednesday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'T':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Thursday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'F':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Friday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'A':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Saturday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;

	case 'S':
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<" You have choosen Sunday";
		cout<<""<<endl;
		cout<<""<<endl;
		break;
	}

}

int convert(char time[5])
{
	int time_mins;
	int int_time[4];

	int_time[0]=time[0]-'0';
	int_time[1]=time[1]-'0';
	int_time[2]=time[2]-'0';
	int_time[3]=time[3]-'0';

        //Applied the 10->6 changes here
	time_mins = int_time[0]* 600 + int_time[1] * 60 + int_time[2] * 10 + int_time[3];
	return time_mins;
}

void calctime ()
{
	cout<<""<<endl;
	cout<<" Please enter the start time of your call"<<endl;
	cout<<""<<endl;
	cin>> chstart;
	cout<<""<<endl;

	starttime = convert(chstart);
	cout<<""<<endl;
	cout<< starttime;
	cout<<""<<endl;

	cout<<""<<endl;
	cout<<" Please enter the time your call ended"<<endl;
	cout<<""<<endl;
	cin>> chend;
	cout<<""<<endl;

	Endtime = convert(chend);
	cout<<""<<endl; 
	cout<< Endtime;

	totmins = (Endtime - starttime);

	cout<<""<<endl;
	cout<<" Your total minutes are: "; cout <<totmins;
	cout<<""<<endl;


	tothrs =(totmins / 60);

	cout<<""<<endl;
	cout<<" Your total hours are: "; cout <<tothrs;

	cout<<""<<endl;

	cin.get();
	cin.get();
}

void main()
{
	getinfo();
	outday();
	calctime();
}

Look for the comments (//) I added. The code worked for me.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.