![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 | ||
|
Professional Programmer
|
Quote:
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:
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! ▄▄▄▄ |
||
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]() 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 |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
Thanks for the help - I'll do what you suggest.
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
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(); int time=getStartTime(); int length=getLengthofCall(); double gross=calcGross(); double net=calcNet(); char input=0; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|