Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2006, 5:47 PM   #1
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
Help with Loop and Calculation

Hi - I'm haveing problems with the calculation and loop. Here are my problems:
Calculation - When I enter in a start time and a length time the calculation is not coming out correctly and I can't seem to figure out why.
Loop - I had to write a line to say that you may enter another set by hitting the n key. I cannot figure out how to make it so that it automatically goes to the next set without hitting the n key.
Any help is greatly appreciated. Here is what I have coded so far.

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

#define rate .10
#define discounta .50
#define discountb .15
#define tax .04

void getStartTime(int time);
void getLengthofCall(int length);
void calcGross(int time, int length, double gross);
void calcNet(int time, int length,double gross, double net);
void displayReport(double gross, double net, char input);
void end();

int main()
{
    int time;
    int length;
    double gross;
    double net;
	char input;
    
    getStartTime(time);
    getLengthofCall(length);
    calcGross(time, length, gross);
    calcNet(time, length, gross, net);
    displayReport(gross, net, input);
	return 0;
}

void getStartTime(int time)
{
     printf("Please input start time: ");
     scanf("%d", &time);
	 if(time >= 2401 || time <= -1)
	 {
		 printf("Invalid time. Please input a time between 0 and 2400.\n\n");
		 main();
	 }
}

void getLengthofCall(int length)
{
     printf("Please input call length: ");
     scanf("%d", &length);
}

void calcGross(int time, int length, double gross)
{
    if(time <= 800 || time >= 1800 && time <= 60)
    {
        gross = length*rate*discounta;
    }
    else if(time <= 800 || time >= 1800 && time >= 60)
    {
        gross = length*rate*discounta*discountb;
    }
    else
    {
        gross = length*rate;      
    }
}

void calcNet(int time, int length, double gross, double net)
{
    if(time <= 800 || time >= 1800 && time <= 60)
    {
        net = gross*tax+gross;
    }
    else if(time <= 800 || time >= 1800 && time >= 60)
    {
        net = gross*tax+gross;
    }
    else
    {
        net = gross*tax+gross;       
    }
}
void displayReport(double gross, double net, char input)
{
     printf("\nGross: %lf\nNet: %lf", gross, net);
	 printf("\nYou may enter another set by hitting the n key.\n");
	 printf("\nYou may exit this program by hitting the e key.\n");
	 scanf("%s", &input);
	 if (input!='e')
	 {
	 	main();
	 }
	 else if(input='e')
	 {
		 end();
	 }
}
void end()
{
     printf("\n\nThank you for using this phone calculator program!\n");
}

This is my output
Please input start time: 1600
Please input call length: 15

Gross: -92559631349317831000000000000000000000000000000000 000000000000.000000
Net: -92559631349317831000000000000000000000000000000000 000000000000.000000
You may enter another set by hitting the n key.

You may exit this program by hitting the e key.
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 5:51 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
When I enter in a start time and a length time the calculation is not coming out correctly and I can't seem to figure out why.
Ok, you've told us you have a problem, but for all we know you were trying to get the outcome you had. What's the problem with the calculation?

PS> About your loop, what do you mean you can't goto the next set without telling the user to press the n key? Usually, "things" will automatically continue, unless interupted, no?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Apr 9th, 2006, 5:54 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Barb
[code]int main()
{
int time;
int length;
double gross;
double net;
char input;

getStartTime(time);
getLengthofCall(length);
calcGross(time, length, gross);
calcNet(time, length, gross, net);
displayReport(gross, net, input);
return 0;
}
I wonder what the problem is....
Maybe initialise your variables?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 9th, 2006, 6:08 PM   #4
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
Sorry that I was clear - this should be what is output.
[code]
Please input start time: 1600
Please input call length: 15
Gross Cost: 1.50
Net Cost: 1.56
[\code]

I initialized my variables and my gross and net are shoeing 0.00.

As for the loop - I wanted the user to be able to continue with the input, but I had to make the user hit a character in order to do this, yes I must have an interuption somewhere - I'm just not sure where.
Thanks for all of the help
Barb
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 6:17 PM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
You seem to be misunderstanding the things.

Functions have paramaters where you put something in. They can return values too.
They are in the form:
returnvalue function(type parameter)
{
	dosomething with parameter;
	return something;
}

You are inputting uninitialized values. And after that you don't do anything with it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 9th, 2006, 6:40 PM   #6
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
Thanks - I tried what you suggested and this is the error that I get

error C2440: 'return' : cannot convert from 'int (__cdecl *)(int)' to 'int'

int getStartTime(int time);
...

int getStartTime(int time)
{
     printf("Please input start time: ");
     scanf("%d", &time);
	 if(time >= 2401 || time <= -1)
	 {
		 printf("Invalid time. Please input a time between 0 and 2400.\n\n");
		 main();
	 }
	 return getStartTime;
}
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 6:59 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're going about this all wrong. While it's valid to call main from within a C program, it is a very BAD THANG. When you want a value, don't go to a function to get it and then call main from within that function, which was, itself, called by main. That's called recursion. A person unfamiliar with the operation of a function (as called to your attention by nnxion) is almost certain to be unfamiliar with how to handle recursion. Goof it up and you run in ever-decreasing circles until you disappear up your own trailing orifice.

Messages informing you that you can't convert a specific type to a pointer of that type mean precisely that. Change one of the two types and rewrite as necessary to accomodate that.

Read your responses, reexamine your code, make some corrections, and post back with additional problems. Be clear about what you expect, how that's not happening, and, of course, include the error messages.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 7:20 PM   #8
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
Thanks for the help - I'll do what you suggest.
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 9:19 PM   #9
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
I have made many changes and I am having problems with the program not storing the values correctly. I am not receiving any errors or warnings - than again I think even with my compiler cranked, that it's still not a very good one. Any suggestions is greatly appreciated.

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

#define rate .10
#define discounta .50
#define discountb .15
#define tax .04

int getStartTime();
int getLengthofCall();
double calcGross();
double calcNet();
void displayReport();
void end();

int main()
{
int time=0;
int length=0;
double gross=0;
double net=0;
char input=0;


getStartTime();
getLengthofCall();
calcGross();
calcNet();
displayReport();
return 0;
}

int getStartTime()
{
int time;

printf("Harford Telephone Company\n");
printf("\nStart time: ");
scanf("%d", &time);
if(time >= 2401 || time <= -1)
{
printf("Invalid time. Please input a time between 0 and 2400.\n\n");
getStartTime();
}
return time;
}

int getLengthofCall()
{
int length;

printf("Length of call: ");
scanf("%d", &length);

return length;
}

double calcGross()
{
int time, length;
double gross;

if(time <= 800 || time >= 1800 && time <= 60)
{
gross = length*rate*discounta;
}
else if(time <= 800 || time >= 1800 && time >= 60)
{
gross = length*rate*discounta*discountb;
}
else
{
gross = length*rate;
}
return gross;
}

double calcNet()
{
int time, length;
double gross, net;

if(time <= 800 || time >= 1800 && time <= 60)
{
net = gross*tax+gross;
}
else if(time <= 800 || time >= 1800 && time >= 60)
{
net = gross*tax+gross;
}
else
{
net = gross*tax+gross;
}
return net;
}

void displayReport()
{
double gross, net;
char input;

printf("\nGross: %.2f", gross);
printf ("\nNet: %.2f", net);
printf("\nYou may enter another set by hitting the n key, than enter.\n");
printf("\nYou may exit this program by hitting the e key.\n");
scanf("%s", &input);
if (input!='e')
{
main();
}
else if(input=='e')
{
end();
}
}

void end()
{
printf("\n\nThank you for using this phone calculator program!\n");
}

This is my output
Start time: 1600
Length of call: 15

Gross: -92559631349317831000000000000000000000000000000000 000000000000.00
Net: -92559631349317831000000000000000000000000000000000 000000000000.00

The calculation should appear as so:
Start time:  1600
Length of call:  15

Gross:  1.50
Net:  1.56

Last edited by Barb; Apr 9th, 2006 at 9:23 PM. Reason: Need to add
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 9:33 PM   #10
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
Your problem is with your function calls. You call the function correctly, but fail to assign the returned values to anything.
int time=0;
int length=0;
double gross=0;
double net=0;
char input=0;


getStartTime();
getLengthofCall();
calcGross();
calcNet();
should be
int time=getStartTime();
int length=getLengthofCall();
double gross=calcGross();
double net=calcNet();
char input=0;
Also, when you call the calcGross, calcNet, and displayResult functions, you need to pass the values that you have in your main function, as they are local to the main function. Note: I did NOT change this for you in the code I gave above, I am leaving that up to you.
nindoja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC