Not excepting the input for calculating phone calls by time.

receiveing invalid input when I put a valid time in??

Page 1 of 1

14 Replies - 2061 Views - Last Post: 29 October 2006 - 10:07 PM Rate Topic: -----

#1 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 01:55 PM

I need to be able to enter in a start time based on a 24 hour clock, and the length of time. It will calculate the gross cost (before any discounts),and net cost. It was working until I entered in the 2nd
"Else If" statement, but the time frames I entered are valid. I'm not sure what I'm missing...Any help or comments are appreciated.

#include <stdio.h>
#include <conio.h>

int main()

{
	float taxRate=.04;
	float reg_Rate=.10;					
	float sixtyMinDisc=.15;
	float afterHours_call=.50;		
	float startTime=0;
	float lengthTime=0;
	float grossCost=0;
	float netCost=0;
	

	printf("Hardford Telephone Company\n");
	printf("\nStart Time:");
	scanf("%f",&startTime);
	if((startTime<0 && startTime>=2401)||(lengthTime=0))
	{
	printf("\nLength of Time:");
	scanf("%f",&lengthTime);
		
	grossCost=(lengthTime*reg_Rate);
		printf("\nGross Cost%5.2f\n ",grossCost);
	}
	/* Calculating regular rate call w/ no discount */
	if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
	{
		netCost=(grossCost*taxRate+grossCost);
		printf("\nNet Cost%5.2f",netCost);
	}
	/* Calculating regular rate call w/ discount */
	else if ((startTime >=800)&&(startTime <1800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc); /*Calculating Discount*/

		netCost=(grossCost-sixtyMinDisc)+(grossCost*taxRate); /*Calcualting Net Cost discounted*/
		printf("\nNet Cost%5.2f\n",netCost);
	}
	/*Calcualting After Hours Calling */
	else if ((startTime>=1800)&&(startTime<800)&&(lengthTime<60))
	{
		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	
	else
		printf("Invalid Data Entered");

		getch ();

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Not excepting the input for calculating phone calls by time.

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 02:16 PM

for a start the = (an assignment) in statement
if((startTime<0 && startTime>=2401)||(lengthTime=0))
should be == (test equality), i.e.
if((startTime<0 && startTime>=2401)||(lengthTime==0))
Was This Post Helpful? 0
  • +
  • -

#3 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 02:24 PM

Got it..thanks :)

I am now having the output give me when I input 700 as my Start time, and 20 as my length of time it give me the output
"gross cost 2.00
Invalid Data Entered!"

it is bypassing my else if statement for afterhours calls?
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 02:35 PM

should you have an else before
/* Calculating regular rate call w/ no discount */
if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
Was This Post Helpful? 0
  • +
  • -

#5 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 02:50 PM

did not make a difference, It still will not give me my Net Cost. I looked to see if I might have forgotten anything else, but did not see anything missing.

thanks for the help so far!! still new to programming :)

could it be because of where I have this statement?

grossCost=(lengthTime*reg_Rate);
printf("\nGross Cost%5.2f\n ",grossCost);

I was trying to avoid repetitive typing?

This post has been edited by jona431: 29 October 2006 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 03:46 PM

ok, here is an update of what I have so far. For some reason I cannot get an output of the "Net Cost". Any help please..Thanks

#include <stdio.h>
#include <conio.h>

int main()

{
	float taxRate=.04;
	float reg_Rate=.10;					
	float sixtyMinDisc=.15;
	float afterHours_call=.50;		
	float startTime=0;
	float lengthTime=0;
	float grossCost=0;
	float netCost=0;
	

	printf("Hardford Telephone Company\n");
	printf("\nStart Time:");
	scanf("%f",&startTime);
	if((startTime<1 && startTime>=2401)||(lengthTime==0))
	{
	printf("\nLength of Time:");
	scanf("%f",&lengthTime);
		
	grossCost=(lengthTime*reg_Rate);
		printf("\nGross Cost%5.2f\n ",grossCost);
	}
	/* Calculating regular rate call w/ no discount */
	else if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
	{
		netCost=(grossCost*taxRate+grossCost);
		printf("\nNet Cost%5.2f",netCost);
	}
	/* Calculating regular rate call w/ discount */
	else if ((startTime >=800)&&(startTime <1800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc); /*Calculating Discount*/

		netCost=(grossCost-sixtyMinDisc)+(grossCost*taxRate); /*Calcualting Net Cost discounted*/
		printf("\nNet Cost%5.2f\n",netCost);
	}
	/*Calcualting After Hours Calling */
	else if ((startTime>=1800)&&(startTime<800)&&(lengthTime<60))
	{
		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	
	else if ((startTime>=1800)&&(startTime<800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc);

		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call)+(grossCost*sixtyMinDisc)+(grossCost*taxRate);
		printf("Net Cost%5.2f",netCost);

	}
	else
		printf("Invalid Data Entered");

		getch ();

	return 0;
}


Here is the message I am getting when I use the f5 (go):

The thread 0x34C has exited with code 0 (0x0).
The program 'C:\Documents and Settings\xxxx\Desktop\Lab3assignments\Harford Telephone Company\Debug\harfod_Tele.exe' has exited with code 0 (0x0).

This post has been edited by jona431: 29 October 2006 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 05:26 PM

Okay well first of all, your loop is testing true no matter what at the moment when it hits ||(lengthTime == 0)) because the value of lengthTime was never modified so it is zero, meaning no matter what the user enters as the start time, this will always evaluate to true. It should be changed to && instead.

Second you may want to allow the user to enter the length of the call before you enter the if statement, otherwise testing for the length is useless.

Let me see what you got after that and we'll see what else may be amiss if anything.
Was This Post Helpful? 0
  • +
  • -

#8 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 05:48 PM

Is this what you meant?

printf("Hardford Telephone Company\n");
printf("\nStart Time:");
scanf("%f",&startTime);
printf("\nLength of Time:");
scanf("%f",&lengthTime);
if((startTime<0 && startTime>=2401)&&(lengthTime=0))
{
grossCost=(lengthTime*reg_Rate);
printf("\nGross Cost%5.2f\n",grossCost);
}

This post has been edited by jona431: 29 October 2006 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#9 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 06:14 PM

That would be it. So now its not going to always test positive on that If statement. Now is it still doing anything unwanted?
Was This Post Helpful? 0
  • +
  • -

#10 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 06:45 PM

now it's not giving me the gross cost or the net cost (netcost is giving me 0.00).

Here is the updated code

#include <stdio.h>
#include <conio.h>

int main()

{
	double taxRate=.04;
	double reg_Rate=.10;					
	double sixtyMinDisc=.15;
	double afterHours_call=.50;		
	float startTime=0;
	float lengthTime=0;
	float grossCost=0;
	float netCost=0;
	

	printf("Hardford Telephone Company\n");
	printf("\nStart Time:");
	scanf("%f",&startTime);
	printf("\nLength of Time:");
	scanf("%f",&lengthTime);
	if((startTime<0 && startTime>=2401)&&(lengthTime=0))
	{
		grossCost=(lengthTime*reg_Rate);
		printf("\nGross Cost%5.2f\n",grossCost);
	}
	/* Calculating regular rate call w/ no discount */
	if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
	{
		netCost=(grossCost*taxRate+grossCost);
		printf("\nNet Cost%5.2f",netCost);
	}
	/* Calculating regular rate call w/ discount */
	else if ((startTime >=800)&&(startTime <1800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc); /*Calculating Discount*/

		netCost=(grossCost-sixtyMinDisc)+(grossCost*taxRate); /*Calcualting Net Cost discounted*/
		printf("\nNet Cost%5.2f\n",netCost);
	}
	/*Calcualting After Hours Calling no Afet hours discount only*/
	else if ((startTime>=1800)&&(startTime<800)&&(lengthTime<60))
	{
		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	/*Calcualting After Hours Calling no After hours discount & over 60min discount*/
	else if ((startTime>=1800)&&(startTime<800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc);

		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call)+(grossCost*sixtyMinDisc)+(grossCost*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	else
		printf("Invalid Data Entered");

		getch ();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 07:32 PM

Not sure if I should add an else to the 2nd if statement..Could that be causing one of my issues.
printf("Hardford Telephone Company\n");
printf("\nStart Time:");
scanf("%f",&startTime);
printf("\nLength of Time:");
scanf("%f",&lengthTime);
if((startTime<0 && startTime>=2401)&&(lengthTime=0))
{
grossCost=(lengthTime*reg_Rate);
printf("\nGross Cost%5.2f\n",grossCost);
}
/* Calculating regular rate call w/ no discount */
if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
{
Was This Post Helpful? 0
  • +
  • -

#12 monolyth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 29-October 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 07:48 PM

your grossCost is never calculated because your if statement
if((startTime<0 && startTime>=2401)&&(lengthTime=0))

is never executed, your start time cannot be both less than zero and >= 2401

it seems to me that you DONT want your code to run under those conditions..
you can try to change it to
if((startTime>0 && startTime<2401)&&(lengthTime>0))

This post has been edited by monolyth: 29 October 2006 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 08:01 PM

ok, I changed it, but when I input 700 for a start time, and 20 for length it fives me an output of gross cost 2.00(which is right)but then it says invailid input.
Was This Post Helpful? 0
  • +
  • -

#14 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 08:34 PM

Okay figured out what the problem was. You second if statement was almost correct. It needed to be embedded, meaning between the code brackets for the first if statement.
if(condition)
{
	 then
	 if(condition)   //embedded if statement
	 {
	  code here
	 }
}



And in the last two else if statements there you were evaluating with '&&' when it should be '||', but there is no prossible way for a number to be >1800 AND <800. Physically impossible, but you can test to see if the number meets one of those conditions, which is what you want. So here is the code i have now:

#include <stdio.h>
#include <conio.h>

int main()

{
	double taxRate=.04;
	double reg_Rate=.10;					
	double sixtyMinDisc=.15;
	double afterHours_call=.50;		
	float startTime=0;
	float lengthTime=0;
	float grossCost=0;
	float netCost=0;
	

	printf("Hardford Telephone Company\n");
	printf("\nStart Time:");
	scanf("%f",&startTime);
	printf("\nLength of Time:");
	scanf("%f",&lengthTime);
	if((startTime>0 && startTime<=2400)||(lengthTime=0))
	{
		grossCost=(lengthTime*reg_Rate);
		printf("\nGross Cost%5.2f\n",grossCost);
	
	/* Calculating regular rate call w/ no discount */
	if ((startTime >=800)&&(startTime<1800)&&(lengthTime<60))
	{
		netCost=(grossCost*taxRate+grossCost);
		printf("\nNet Cost%5.2f",netCost);
	}
	/* Calculating regular rate call w/ discount */
	else if ((startTime >=800)&&(startTime <1800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc); /*Calculating Discount*/

		netCost=(grossCost-sixtyMinDisc)+(grossCost*taxRate); /*Calcualting Net Cost discounted*/
		printf("\nNet Cost%5.2f\n",netCost);
	}
	/*Calcualting After Hours Calling no Afet hours discount only*/
	else if ((startTime>=1800)||(startTime<800)&&(lengthTime<60))
	{
		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	/*Calcualting After Hours Calling no After hours discount & over 60min discount*/
	else if ((startTime>=1800)||(startTime<800)&&(lengthTime>60))
	{
		sixtyMinDisc=(grossCost*sixtyMinDisc);

		afterHours_call=(grossCost*afterHours_call);

		netCost=(grossCost-afterHours_call)+(grossCost*sixtyMinDisc)+(grossCost*taxRate);
		printf("Net Cost%5.2f",netCost);
	}
	else
		printf("Invalid Data Entered");

		getch ();
  }
	return 0;
}


This post has been edited by Antiokus: 29 October 2006 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#15 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Not excepting the input for calculating phone calls by time.

Posted 29 October 2006 - 10:07 PM

got it working!!

Thanks for all the help Antiokus, monolyth, and horace ..Really appreciate it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1