![]() |
| | #1 | ||
| Registered User Join Date: Mar 2002
Posts: 5
| A small problem with a small program I am learning C at school and have this program I'm writing. I was able to fix all the errors (and there were plenty )Here is the code Code: #include <stdio.h>
/*This is a program to report the charges incurred for each day, and after all days are processed, the total duration for each level, the total charges for the period, the average charge per person trained in that period, and the day, level, amount of the lowest day in that period*/
float prd_chrg; /*total charges per period*/
int day_dur; /*total number of minutes of training session per day*/
int lvl_a_dur; /*Duration, per period that level A was trained*/
int lvl_b_dur; /*Duration, per period that level A was trained*/
int lvl_i_dur; /*Duration, per period that level A was trained*/
int low; /*used to set the lowest day, level and charge of a period*/
int client_num; /*Client ID number*/
int mnth; /*Month*/
int yr; /*Year*/
int day; /*Day number*/
int day_dur; /*Total number of minutes of training session per day*/
char lvl; /*Level trained on a given day*/
int num_ppl; /*Number of people per group*/
int ppl_ovr_4; /*Number of people over four in a group*/
float lvl_chrg; /*Charge per person for a given level*/
int count; /*Total number of people trained per period*/
float avg_chrg; /*Average charge per day during a period*/
char more_client; /*Used to determine if there is more client data to input*/
int low_day; /*This is the day number of the day with the lowest charge in the period*/
char low_lvl; /*This is the level that was trained on the day with the lowest charge in the period*/
float low_chrg; /*This is the lowest day charge of the period*/
float day_chrg; /*This is the charge for the day the data was entered for*/
float dsct_chrg; /*This is the price per person over 4 in a group*/
float lvl_chrg; /*This is the charge for the level trained on a given day*/
float lvl_i_chrg; /*This is the amount charged for training level I*/
float lvl_a_chrg; /*This is the amount charged for training level A*/
float lvl_b_chrg; /*This is the amount charged for training level B*/
/*Functions*/
void initialize (void);
void input_prd (void);
void input_day (void);
void update_avg (void);
void output_prd (void);
void more_client (void);
void update_lvl_chrg (void);
void input_people (void);
void update_chrg (void);
void update_low (void);
void output_day (void);
int main (void)
{
init ();
input_prd ();
do
{
input_day ();
}while (day<=15);
update_avg ();
output_prd ();
more_client ();
return 0;
}
void initialize (void)
/*This function initializes certain variables*/
{
prd_chrg==0;
day_dur==0;
lvl_a_dur==0;
lvl_b_dur==0;
lvl_i_dur==0;
low==999999999;
}
void input_prd (void)
/*This function allows the user to input the month, year, and client the data is for*/
{
printf("\nInput client number: ");
scanf("%d", &client_num);
printf("\nInput month: ");
scanf("%d", &mnth);
printf("\nInput year: ");
scanf("%d", &yr);
}
void input_day (void)
/*This function allows the user to input the day of the month, duration of the training for a given day, and the number of people trained*/
{
printf("\nInput day number: ");
scanf("%d", &day);
printf("\nInput day duration: ");
scanf("%d", &day_dur);
fflush(stdin);
printf("\nInput the day's level (A, I, B): ");
scanf("%c", &lvl);
printf("\nInput number of people trained: ");
scanf("%d", &num_ppl);
update_lvl_chrd ();
input_people ();
if (num_ppl<=4)
{
num_ppl==4;
}
else
{
ppl_ovr_4=num_ppl-4;
num_ppl==4;
}
update_chrg ();
update_low ();
output_day ();
}
void update_lvl_chrg (void)
/*This function determines what the hourly charge for a given day was and calculates the level duration to that point in the period*/
{
if (lvl=='I')
{
lvl_chrg==1.5;
dsct_chrg==.75;
lvl_i_chrg=(lvl_i_chrg+day_dur);
}
else
{
if (lvl=='B')
{
lvl_chrg==1;
dsct_chrg==.5;
lvl_b_chrg=(lvl_b_chrg+day_dur);
}
else
{
if (lvl=='A')
{
lvl_chrg==2;
dsct_chrg==1;
lvl_a_chrg=(lvl_a_chrg+day_dur);
}
}
}
}
void input_people (void)
/*This function allows the user to input the number of people trained on a given day*/
{
printf("\nInput number of people in a group: ");
scanf("%d", num_ppl);
count=(count+num_ppl);
}
void update_chrg (void)
/*This function calculates the charge for a given day*/
{
day_chrg=((lvl_chrg*num_ppl*day_dur)+(dsct_chrg*ppl_ovr_4*day dur));
prd_chrg=(prd_chrg=day_chrg);
}
void update_low (void)
/*This function updates the low charge, day, and level of the period*/
{
if (day_chrg<low)
{
day_chrg=low_chrg;
low_day=low;
low_lvl=lvl;
}
}
void output_day (void)
/*This function outputs day of the month and that day's charge*/
{
printf("\nDay number: %d", day);
printf("\nDay charge: %f", day_chrg);
}
void update_avg (void)
/*This function calculates the average amount charged per person in the period*/
{
avg_chrg=(prd_chrg/count);
}
void output_prd (void)
/*This function outputs the total level durations, period charge, day number, day charge for the last day of the period, and the lowest day, charge, and level of the period*/
{
printf("\nLevel A duration: %d", lvl_a_dur);
printf("\nLevel I duration: %d", lvl_i_dur);
printf("\nLevel B duration: %d", lvl_b_dur);
printf("\nPeriod charge: %f", prd_chrg);
printf("\nDay number: %d", day);
printf("\nDay charge: %f", day_chrg);
printf("\nAverage charge: %f", avg_chrg);
printf("\nLow charge: %f", low_chrg);
printf("\nLow day: %d", low_day);
printf("\nLow level: %c", low_lvl);
}
void more_client (void)
/*This function allows the user to input whether or not they need to input data for more clients*/
{
fflush (stdin);
printf("Are there more clients to enter? (Y, N) ");
scanf("%c", &more_client);
if (more_client=='y')
{
input_prd ();
}
}
Quote:
Quote:
| ||
| Wetling is offline | |
| | #2 |
| Registered User Join Date: Mar 2002
Posts: 5
| It doesn't have something to do with the fact that in that last if statement, I say if more_client=='y', while I'm asking the user to input a Y or N (capital v. lower case)? |
| Wetling is offline | |
| | #3 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| Okay, compiling as is gave me 5 errors and 16 warnings. Remember that == is a test and = is assignment, that was a big cause of the warnings. The rest of your errors consisted of misspelling function and variable names incorrectly and one variable name was the same as a function name which is illegal. The function and variable with the same name were more_client, change one of the names and your error will go away. I also recommend turning your compiler up to the highest warning level so that you catch some of the things I did. The program would not work at all like you wanted if you tested where you wanted to make an assignment. Also, global variables should be avoided. >fflush(stdin); This is undefined, don't use it. Please read the scanf thread on these boards for more details, I'm tired of typing the same thing. -Prelude
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #4 | ||||
| Registered User Join Date: Mar 2002
Posts: 5
| Quote:
Quote:
Quote:
Quote:
Last edited by Wetling; 03-25-2002 at 09:13 PM. | ||||
| Wetling is offline | |
| | #5 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| >I'll have to double check my notes, but I'm almost certain the professor used it. Want to look smart? Tell your professor that he's wrong because the ISO C standard defines fflush only for output streams, when used on input streams the result is undefined behavior. I hate to say it, but teachers tend to not really know what they're talking about. Be sure to research everything you're taught thoroughly in case your professor is wrong. -Prelude
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #6 |
| Registered User Join Date: Mar 2002
Posts: 5
| Okay. Luckily I don't want to be a programmer (a Pascal class made that descision for me). This is just a requirement for the degree. I suppose it doesn't help that the prof is a Visual Basic guy. |
| Wetling is offline | |
| | #7 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| Ooh, a C programming course taught by a VB programmer, sign me up! -Prelude
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #8 |
| Registered User Join Date: Mar 2002
Posts: 5
| Okay, I read the scanf thread and I guess I'll put a space in front of my %c. |
| Wetling is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [NEED HELP] I just can't solve this program problem! | frodonet | C Programming | 7 | 09-23-2007 02:44 PM |
| Problem with simple XOR program | spike_ | C++ Programming | 8 | 08-17-2005 12:09 AM |
| Small Problem that could be a big problem | sytaylor | C++ Programming | 6 | 05-12-2004 09:49 AM |
| simple frontend program problem | gandalf_bar | Linux Programming | 16 | 04-22-2004 06:33 AM |
| problem with 2d array program. | Niloc1 | C Programming | 1 | 04-08-2002 05:47 PM |