![]() |
| | #1 |
| null Join Date: Dec 2005
Posts: 18
| Trouble with a lab ![]() I've been perusing the board here for a couple weeks, and have found it to be a great resource in learning C and C#. However, I can't find any answer to my current problem, which is part of a college homework assignment. Here's my code so far: Code: /************************
* CIS 15AG AM - Lab VII
*
* A program to help a business keep
* statistics of its employee salaries
* for one year.
*
* Author: Michael Baird
*
* Begin date: Nov 28, 2005
* Last modified: Dec 4, 2005
**/
#include <stdio.h>
char PromptUser();
int CountEmployees();
void HighestAnnualSalary();
int main()
{
char choice;
do
{
choice = PromptUser();
switch( tolower( choice ) )
{
case 'c':
{
printf( "There are entries for %d employees.\n\n", CountEmployees() );
} break;
case 'h':
{
HighestAnnualSalary();
} break;
}
} while( tolower( choice ) != 'q' );//(choice = tolower( PromptUser() )) != 'q' );
system( "PAUSE" );
return 0;
}
char PromptUser()
{
char res;
printf( "Choose an option from the following menu:\n" );
printf( "\tc: calculate total employees\n" );
printf( "\th: report highest annual salary\n" );
printf( "\ta: calculate average monthly salary\n" );
printf( "\tf: format the report to output file\n" );
printf( "\tq: quit this application\n\n" );
printf( "Enter your choice: " );
scanf( " %c", &res );
return res;
}
int CountEmployees()
{
FILE * input;
int count = 0;
char holder;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
while( (fscanf( input, "%c", &holder ) != EOF) )
{
if( holder == '\n' )
count++;
}
fclose( input );
return count;
}
void HighestAnnualSalary()
{
}
Code: 1234,12,1200 2050 3010 4000 5560 6005 6780 5340 4000.50 3040 2005 1100 If I need to clarify anything, please let me know. |
| michael- is offline | |
| | #2 |
| Logic Junkie Join Date: Nov 2005
Posts: 31
| fscanf seems the obvious choice. Read one int, the read one char to get rid of the comma, then read the second int, and read a char again to kill off the comma. Then, read one and one float and put them whereever you like (I would declare a float array the size of the months, dunno if you are allowed that in your assignment).
__________________ -S |
| Silfer is offline | |
| | #3 |
| null Join Date: Dec 2005
Posts: 18
| Unfortunately, I'm not allowed to use arrays at all :/ Would your suggestion be like this (I apologize for not testing first, but I dont have an IDE at hand here): Code: fscanf( input, "%d%c%d%c%f...%f" ); |
| michael- is offline | |
| | #4 |
| Registered User Join Date: Mar 2005
Posts: 140
| You don't need an array because there is no need to store all the values, you just need to keep track of the max. Code: max = 0;
for each salary
if salary > max
max = salary
return max
Code: fscanf( input, "%d,", &id ); fscanf( input, "%d,", &num_of_months ); Code: loop num_of_month times fscanf( input, "%f", &salary ); Last edited by spydoor; 12-05-2005 at 04:09 PM. |
| spydoor is offline | |
| | #5 |
| null Join Date: Dec 2005
Posts: 18
| I see. So fscanf( ... ) doesn't go back to the start of the line if the call to the function ends? That's helpful info. Thanks, I'll get back to giving it a shot |
| michael- is offline | |
| | #6 |
| Logic Junkie Join Date: Nov 2005
Posts: 31
| Oh, of course, you don't need an array - silly me. fscanf does not care for spaces if you ask it to read numbers, btw, so you would only need to read a char twice to kill the two commas.
__________________ -S |
| Silfer is offline | |
| | #7 |
| null Join Date: Dec 2005
Posts: 18
| Hrm. I've been battling this one again for a while. I dont understand why I'm so baffled by this, but I need support again. Here's what I have so far: Code: /************************
* CIS 15AG AM - Lab VII
*
* A program to help a business keep
* statistics of its employee salaries
* for one year.
*
* Author: Michael Baird
*
* Begin date: Nov 28, 2005
* Last modified: Dec 4, 2005
**/
#include <stdio.h>
char PromptUser();
int CountEmployees();
void HighestAnnualSalary();
int main()
{
char choice;
do
{
choice = PromptUser();
switch( tolower( choice ) )
{
case 'c':
{
printf( "There are entries for %d employees.\n\n", CountEmployees() );
} break;
case 'h':
{
HighestAnnualSalary();
} break;
}
} while( tolower( choice ) != 'q' );
system( "PAUSE" );
return 0;
}
char PromptUser()
{
char res;
printf( "Choose an option from the following menu:\n" );
printf( "\tc: calculate total employees\n" );
printf( "\th: report highest annual salary\n" );
printf( "\ta: calculate average monthly salary\n" );
printf( "\tf: format the report to output file\n" );
printf( "\tq: quit this application\n\n" );
printf( "Enter your choice: " );
scanf( " %c", &res );
return res;
}
int CountEmployees()
{
FILE * input;
int count = 0;
char holder;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
while( (fscanf( input, "%c", &holder ) != EOF) )
{
if( holder == '\n' )
count++;
}
fclose( input );
return count;
}
void HighestAnnualSalary()
{
FILE * input;
char holder;
int i, j;
int studentID, monthsWorked;
float salary = 0, highestSalary = 0;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( (fscanf( input, "%c", &holder ) != EOF) )
{
fscanf( input, "%d,", &studentID );
fscanf( input, "%d,", &monthsWorked );
for( i = 0; i < monthsWorked; i++ )
salary += fscanf( input, "%f", &salary );
if( salary > highestSalary )
highestSalary = salary;
}
}
printf( "Highest salary: %.2f\n\n", highestSalary );
}
Code: 1234,12,1200 2050 3010 4000 5560 6005 6780 5340 4000.50 3040 2005 1100 2245,5,980 525 1120 405.50 650.50 3496,3, 1020.50 2085 1050.50 8849,10,500.50 305.50 810 925 1060 1256.50 800.50 810.50 1087 578 2390,6,875.50 1020 510.50 790 250.50 1000.50 Any help would be greatly appreciated. |
| michael- is offline | |
| | #8 |
| Registered User Join Date: Mar 2005
Posts: 140
| When fscanf successfully reads one value it will return 1, so you are adding 1 to salary each time. Also, you are changing the value of salary before you compare/set it to highest, so you need a different variable to keep track of the total for highest annual salary of all employees you'd want something like Code: for( j = 0; j < CountEmployees(); j++ )
{
while( (fscanf( input, "%c", &holder ) != EOF) )
{
fscanf( input, "%d,", &studentID );
fscanf( input, "%d,", &monthsWorked );
for( i = 0; i < monthsWorked; i++ )
{
fscanf( input, "%f", &salary );
total_salary += salary;
}
if( total_salary > highestSalary )
{
highestSalary = total_salary;
}
total_salary = 0; //reset for each employee
}
}
printf( "Highest salary: %.2f\n\n", highestSalary
Last edited by spydoor; 12-06-2005 at 12:41 PM. |
| spydoor is offline | |
| | #9 |
| Logic Junkie Join Date: Nov 2005
Posts: 31
| fscanf() returns the number of items read and assigned, or EOF. Thus, your line Code: salary += fscanf( input, "%f", &salary ); try this: Code: for( i = 0; i < monthsWorked; i++ )
int ret = fscanf( input, "%f", &salary );
//ret will let you know if you got EOF, or if fscanf failed somehow.
//as before onwards - salary now contains the newly read number, so just check it vs. highestSalary.
__________________ -S |
| Silfer is offline | |
| | #10 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Code: salary += fscanf( input, "%f", &salary ); Code: fscanf( input, "%f", &temp ); salary += temp;
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #11 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| tolower() and family are in <ctype.h>.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #12 |
| null Join Date: Dec 2005
Posts: 18
| Thank you all so much. That's really helpful. I'm sure I'll be back with more questions (and I think I know why - after rereading the assignment, it's written very poorly, but my professor is a CIS major, not an English major), but thank you for all the help so far! |
| michael- is offline | |
| | #13 |
| null Join Date: Dec 2005
Posts: 18
| One final question, and it's to do with formatting this input file into an output file. Here's the code so far: Code: /************************
* CIS 15AG AM - Lab VII
*
* A program to help a business keep
* statistics of its employee salaries
* for one year.
*
* Author: Michael Baird
*
* Begin date: Nov 28, 2005
* Last modified: Dec 6, 2005
**/
#include <stdio.h>
#include <ctype.h>
char PromptUser();
int CountEmployees();
void HighestAnnualSalary();
float CalculateHighestSalary( int * emp );
void AverageMonthly();
float CalculateAverageSalary();
void FormatReport();
int InputToOutput();
int main()
{
char choice;
do
{
choice = PromptUser();
switch( tolower( choice ) )
{
case 'c':
{
printf( "There are entries for %d employees.\n\n", CountEmployees() );
} break;
case 'h':
{
HighestAnnualSalary();
} break;
case 'a':
{
AverageMonthly();
} break;
case 'f':
{
FormatReport();
} break;
}
} while( tolower( choice ) != 'q' );
system( "PAUSE" );
return 0;
}
char PromptUser()
{
char res;
printf( "Choose an option from the following menu:\n" );
printf( "\tc: calculate total employees\n" );
printf( "\th: report highest annual salary\n" );
printf( "\ta: calculate average monthly salary\n" );
printf( "\tf: format the report to output file\n" );
printf( "\tq: quit this application\n\n" );
printf( "Enter your choice: " );
scanf( " %c", &res );
return res;
}
int CountEmployees()
{
FILE * input;
int count = 0;
char holder;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
while( (fscanf( input, "%c", &holder ) != EOF) )
{
if( holder == '\n' )
count++;
}
fclose( input );
return count;
}
void HighestAnnualSalary()
{
int employee = 0;
float highest = CalculateHighestSalary( &employee );
printf( "Employee %d has the highest annual salary of %.2f.\n\n", employee, highest );
return;
}
float CalculateHighestSalary( int * emp )
{
FILE * input;
int i, j; //for use with loop methods
int employeeID, monthsWorked;
float highestSalary = 0, iSalary /*individual monthly salary*/ = 0, iTotalSalary = 0 /*individual total salary*/;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &monthsWorked );
for( i = 0; i < monthsWorked; i++ )
{
fscanf( input, "%f", &iSalary );
iTotalSalary += iSalary;
}
if( iTotalSalary > highestSalary )
{
highestSalary = iTotalSalary;
*emp = employeeID;
}
//reset for each employee
iSalary = 0;
iTotalSalary = 0;
}
}
fclose( input );
return highestSalary;
}
void AverageMonthly()
{
printf( "The average monthly salary for all employees is: %.2f.\n\n", CalculateAverageSalary() );
return;
}
float CalculateAverageSalary()
{
FILE * input;
int i, j; //for use with loop methods
int employeeID; //holder for this entry
int totalMonths = 0, iMonthsWorked /*individual months worked*/;
float totalAverage = 0, iSalary /*individual monthly salary*/;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening the input file.\n" );
return 1;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &iMonthsWorked );
totalMonths += iMonthsWorked;
for( i = 0; i < iMonthsWorked; i++ )
{
fscanf( input, "%f", &iSalary );
totalAverage += iSalary; //store in totalAverage simply to save mem space
}
}
}
return (totalAverage / totalMonths);
}
void FormatReport()
{
if( InputToOutput() )
{
printf( "The input file has been successfully written to the output file, \'out.txt\'.\n\n" );
}
else
printf( "Formatting failed.\n\n" );
return;
}
int InputToOutput()
{
FILE * input;
FILE * output;
int i, j; //for use with loops
int employeeID, monthsWorked;
float jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 0;
}
else if( (output = fopen( "out.txt", "w" )) == NULL )
{
printf( "Error creating output file.\n" );
return 0;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &monthsWorked );
fprintf( output, "%d\t%d", employeeID, monthsWorked );
}
}
}
The input file is this: Code: 1234,12,1200 2050 3010 4000 5560 6005 6780 5340 4000.50 3040 2005 1100 2245,5,980 525 1120 405.50 650.50 3496,3, 1020.50 2085 1050.50 8849,10,500.50 305.50 810 925 1060 1256.50 800.50 810.50 1087 578 2390,6,875.50 1020 510.50 790 250.50 1000.50 Anyway, here's my dilemma: I need to replace the "extra" spaces - those for unemployed months - with asteriks ( '*' ), while keeping the full float numbers in tact to also print those in between. You can see in the last function of my code that I also output the ID and the months worked, separated by tabs. After that, it's supposed to be either the monthly income or an asterik if unemployed for the month. I'll keep tinkering with what I've got above, but if I'm on the wrong track or if there are any great ideas (like all the ones above ), please help! |
| michael- is offline | |
| | #14 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Why don't you save yourself the trouble and write 0.0 to the file for when they don't work that month? Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #15 |
| null Join Date: Dec 2005
Posts: 18
| That'd be going against the rule of the lab, and in either case, I still don't know how to read a space versus an entire float (switching between %c or %f). |
| michael- is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble with assignment in C | mohanlon | C Programming | 17 | 06-23-2009 10:44 AM |
| Is it so trouble? | Yumin | Tech Board | 4 | 01-30-2006 04:10 PM |
| Datastructure lab | El_Maco | C++ Programming | 14 | 10-10-2004 07:35 AM |
| programming from home to the lab??? | nbo10 | Tech Board | 7 | 08-27-2004 04:35 PM |
| Please help debug this lab! Please! | mack_ol | C Programming | 0 | 02-17-2002 12:32 PM |