By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,690 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,690 IT Pros & Developers. It's quick & easy.

Help with a simple c program

P: n/a
SK
Hi
I am trying to write a simple C program with devc++ as the complier
using the concept of arrays. I cannot get the program to compile
without mutiple errors. If anyone has the time to help me I would
really apprecaite it.
Thanks SK
//Specs to be added later

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME[20]
# define EMPLOYEES[1000]

/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
}
/*Rename the structure syntax.*/
typedef EMP_WeeklyPay EWP;
/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

/*Strings in input*/
char department[20], fn[FULLNAME], ln[FULLNAME], char again;

/*temporary float*/
float wage, float OTwage, float hours, float RegHr,
float OTHrPay, float OTHr, float GrossPay;
printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);
/*Loop to read in employee wage data*/
for (n = 0; n < EMPLOYEES; ++n){

printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &Fname, &Lname);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);

}
/*Read in the input*/
numemp = scanf("%11s%11s%f%f%f%f%f", fn, ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);
/*Check if user is done*/
printf("\nThank you. Process another employee?");
scanf("%s", &again);

while(again == 'Y' || again == 'y');

if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");
/*Process the input*/
if(num == 7)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(employee[n].first_name, fn, FULLNAME-1);
employee[n].first_name[FULLNAME-1] = '\0';

strncpy(employee[n].last_name, ln, FULLNAME-1);
employee[n].last_name[FULLNAME-1] = '\0';

employee[n].regularhours = RegHr;
employee[n].wage = wage;
employee[n].overtimehours = OTHr;
employee[n].overtimepay = OTHrPay;
employee[n].GrossPay. = GrossPay;
++count;
}

/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");


for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
employee[n].first_name,
employee[n].last_name, employee[n].RegHr,
employee[n].wage, employee[n].OTHr, employee[n].OTHrPay,
employee[n].GrossPay);
}
}

Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"SK" <ba******@aol.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com
Hi
I am trying to write a simple C program with devc++ as the complier
using the concept of arrays. I cannot get the program to compile
without mutiple errors.
The errors are there for a reason. They tell you (approximately) what is
wrong and (approximately) where. Usually a program that spits out numerous
errors actually has rather less. Fixing errors at the start tends to get rid
of many error messages that occur later. I will make a few suggestions to
get you started --- your compiler is probably already making some of the
same suggestions.
If anyone has the time to help me I would
really apprecaite it.
Thanks SK
//Specs to be added later

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME[20]
# define EMPLOYEES[1000]
You don't use brackets to #define constants (or equals signs):

# define FULLNAME 20
# define EMPLOYEES 1000

/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
}
Any struct (or class in C++) must have a semi-colon after the closing
bracket.


/*Rename the structure syntax.*/
typedef EMP_WeeklyPay EWP;
/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

/*Strings in input*/
char department[20], fn[FULLNAME], ln[FULLNAME], char again;
You can't repeat the type char in a single statement. It is either:

char a, b;

or

char a; char b;

Never

char a, char b;

i.e., you need to use a semi-colon before you can use char a second time.

/*temporary float*/
float wage, float OTwage, float hours, float RegHr,
float OTHrPay, float OTHr, float GrossPay;


Same problem, but this time with float instead of char.
--
John Carson

Nov 22 '05 #2

P: n/a
SK
Thank you very much. SK

Nov 22 '05 #3

P: n/a
"SK" <ba******@aol.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
Thank you very much. SK


You're welcome. One other thing. Your code was lacking a main() function and
won't work without one.

The only thing you are allowed to do at global scope (i.e., at the outermost
level of a file) is make declarations and definitions. Any function calls
must be nested inside other functions --- unless this is done as part of a
definition. For this and other reasons, any standard program must define a
main() function within which other function calls occur. To illustrate, a
file:

#include "stdlib.h"

printf("Test\n");

won't compile.

Instead, you need:

#include "stdlib.h"

int main()
{
printf("Test\n");
}
--
John Carson

Nov 22 '05 #4

P: n/a
SK
I should have remembered that one. Thanks. It seems to be compiling
better and I am now seeming to be able to correct many more of the
errors.
Thanks again. SK

Nov 22 '05 #5

P: n/a
SK
I managed to essentially corect all of the errors but ones concerned
with how I am placing my data into the memory. I am getting error
descriptions of
(Line 134)C:\Documents and Settings\Owner\My Documents\HELPCOMP.c
subscripted value is neither array nor pointer

I am trying to add in the information from each employee however I
think I am declaring the memory location erroneously. It is essentially
the later thirdof the program.

Any thoughts??
SK

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000
//Global Defined Constant
const float OT = 1.5;

//Global Variable Declaratives
FILE*inp;

//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000

char fn[10];
char ln[10];
char department[20];
char again;
int count_EMP;
int number_EMP;

float wage;
float OTwage;
float hours;
float RegHr;
float RegHrPay;
float OTHrPay;
float OTHr;
float GrossPay;
int Main(void)
{
/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
};
/*Rename the structure syntax.*/
typedef struct EMP_WeeklyPay EWP;
/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);
/*Loop to read in employee wage data*/
for (n = 0; n < EMPLOYEES; ++n){

printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);

}
/*Read in the input*/
numemp = scanf("%11s%11s%f%f%f%f%f", fn, ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);
/*Check if user is done*/
printf("\nThank you. Process another employee?");
scanf("%s", &again);

while(again == 'Y' || again == 'y');

if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");
/*Process the input*/
if(number_EMP == 7)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(count_EMP[n].first_name, fn, FULLNAME-1);
count_EMP[n].first_name[FULLNAME-1] = '\0';

strncpy(count_EMP[n].last_name, ln, FULLNAME-1);
count_EMP[n].last_name[FULLNAME-1] = '\0';

count_EMP[n].regularhours = RegHr;
count_EMP[n].wage = wage;
count_EMP[n].overtimehours = OTHr;
count_EMP[n].overtimepay = OTHrPay;
count_EMP[n].GrossPay = GrossPay;
++count;
}

/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");


for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
count_EMP[n].first_name,
count_EMP[n].last_name, employee[n].RegHr,
count_EMP[n].wage, count_EMP[n].OTHr,
count_EMP[n].OTHrPay,
count_EMP[n].GrossPay);
}
}

Nov 22 '05 #6

P: n/a
"SK" <ba******@aol.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com
I managed to essentially corect all of the errors but ones concerned
with how I am placing my data into the memory. I am getting error
descriptions of
(Line 134)C:\Documents and Settings\Owner\My Documents\HELPCOMP.c
subscripted value is neither array nor pointer


It means exactly what it says. count_EMP is an integer, not an array and not
a pointer. Further, it is unrelated to your struct so using the dot operator
in conjunction with count_EMP[n] makes no sense.

--
John Carson

Nov 22 '05 #7

P: n/a
SK wrote:
I managed to essentially corect all of the errors but ones concerned
with how I am placing my data into the memory. I am getting error
descriptions of
(Line 134)C:\Documents and Settings\Owner\My Documents\HELPCOMP.c
subscripted value is neither array nor pointer

I am trying to add in the information from each employee however I
think I am declaring the memory location erroneously. It is essentially
the later thirdof the program.

Any thoughts??
SK

Some other errors below.
//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000
//Global Defined Constant
const float OT = 1.5;

//Global Variable Declaratives
FILE*inp;

//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000

char fn[10];
char ln[10];
char department[20];
char again;
int count_EMP;
int number_EMP;

float wage;
float OTwage;
float hours;
float RegHr;
float RegHrPay;
float OTHrPay;
float OTHr;
float GrossPay;
int Main(void)
int main()

main must be lower case.
{
/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
};
This isn't wrong, but it makes more sense to declare this outside the
main function not inside it. That way it is available to the whole
program, not just the main function. At the moment it doesn't matter
because the main function is your whole program, but later on...


/*Rename the structure syntax.*/
typedef struct EMP_WeeklyPay EWP;
Diito, and this is C style programming. Are you trying to learn C or
C++? If you are trying to learn C you really should be posting to
comp.lang.c otherwise people are going to suggest all sorts of C++
solutions to your problems.

If you are trying to learn C++ then you can drop the struct

typedef EMP_WeeklyPay EWP;

But really you should also ask yourself, why do I need the same struct
named two different ways? I cannot think of a good reason so you should
just choose EMP_WeeklyPay or EWP and stick with it.



/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);
/*Loop to read in employee wage data*/
for (n = 0; n < EMPLOYEES; ++n){

printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);

}
/*Read in the input*/
numemp = scanf("%11s%11s%f%f%f%f%f", fn, ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);
/*Check if user is done*/
printf("\nThank you. Process another employee?");
scanf("%s", &again);

while(again == 'Y' || again == 'y');
This loop is wrong. I think you meant

do
{
printf("\nThank you. Process another employee?");
scanf("%s", &again);
}
while(again == 'Y' || again == 'y');

When you write a loop, you have to put the code that you ant to loop
over inside the brace. The compiler can't guess what you want to loop over.

if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");
Well this will print a message, but then it will carry on the loop. If
you want to exit the for loop here then you have to say so. Also the
semi colon is incorrect.

if(again != 'Y' && again !='y')
{
printf("End of processing\n\n\n");
break; // this will quit the for loop
}


/*Process the input*/
if(number_EMP == 7)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(count_EMP[n].first_name, fn, FULLNAME-1);
count_EMP[n].first_name[FULLNAME-1] = '\0';

strncpy(count_EMP[n].last_name, ln, FULLNAME-1);
count_EMP[n].last_name[FULLNAME-1] = '\0';

count_EMP[n].regularhours = RegHr;
count_EMP[n].wage = wage;
count_EMP[n].overtimehours = OTHr;
count_EMP[n].overtimepay = OTHrPay;
count_EMP[n].GrossPay = GrossPay;
++count;
}

/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");


for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
count_EMP[n].first_name,
count_EMP[n].last_name, employee[n].RegHr,
count_EMP[n].wage, count_EMP[n].OTHr,
count_EMP[n].OTHrPay,
count_EMP[n].GrossPay);
}
}


Really this looks to me like you are trying to learn C. You really
should stop posting to comp.lang.c++ and post to comp.lang.c instead of
you are going to get horribly confused.

john
Nov 22 '05 #8

P: n/a
/*Check if user is done*/
printf("\nThank you. Process another employee?");
scanf("%s", &again);

while(again == 'Y' || again == 'y');

This loop is wrong. I think you meant

do
{
printf("\nThank you. Process another employee?");
scanf("%s", &again);
}
while(again == 'Y' || again == 'y');

When you write a loop, you have to put the code that you ant to loop
over inside the brace. The compiler can't guess what you want to loop over.


My suggstion above is rubbish. I'm not sure what you meant. Perhaps this

do
{
printf("\nThank you. Process another employee?");
scanf("%c", &again);
}
while(again != 'y' && again != 'Y' && again != 'N' && again
!= 'n');

i.e. loop until the user answers Y or N.
But anyway note there is another error, you shuld have %c for a char, %s
is for strings.

scanf("%c", &again);

You're eventually going to get this to compile and then you are going to
find out that you have all sorts of even harder logic errors to fix.
Getting your code to compile is only the first step. I would say that
you should have started with a smaller program and only built up to this
bigger program gradually. You could be biting off more than you can
chew, if you are then you will only get frustrated, instead of learning
anything. But anyway good luck!

john
Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.