Question:
C PROGRAMMERS, can you analyze my short code and tell me where i'm going wrong?
2010-12-07 22:48:41 UTC
basically, you have to calculate the RAISE AMOUNT and NEW PAY AMOUNT for 4 faculty members.
the formula for RA: current yearly income*raise percentage/100
the formula for NPA: Current Yearly Income + Raise Amount

there are a few things going wrong at execution even though i have zero errors.

here is a copy of my coding, i know i'm gonna get yelled at for this one.

#include
struct Employee
{
char name [20];
float cye, rp, ra, npa; //cye=current yearly income, rp=raise percentage, ra=raise amount, npa=new pay amount

};
void main ()
{



Employee e;
int i;
for(i=0;i<4;i++);
{
printf("Enter employee name");
gets(e.name);
printf("Enter current yearly income");
scanf(" %f\n", &e.cye);
printf("Enter raise percentage");
scanf(" %f\n",&e.rp);
e.ra=(e.cye * e.rp)/(float)100;
printf("the raise amount is", &e.ra);

}
}

the problem is my output in the command window.
it asks to "enter employee name"
"enter current yearly income"
"enter raise percentagethe raise amount isPress any key to continue...."

so it doesnt give me a chance to enter percentage nor does it display the raise amount


THANKS IN ADVANCE
Six answers:
John D
2010-12-07 23:17:35 UTC
You forgot the %f in your printf.



#include



struct Employee

{

char name [20];

float cye, rp, ra, npa;

};





int main ()

{

struct Employee e;

//Remove for loop from here

printf( "Enter employee name: " );

gets( e.name );

printf( "Enter current yearly income: " );

scanf( "%f", &e.cye );

printf( "Enter raise percentage: " );

scanf( "%f", &e.rp );

e.ra = ( e.cye * e.rp ) / 100.0;

printf( "The raise amount is: %.2f: ", e.ra );

return 0;

}





### EDIT ### It works for me:

Enter employee name: John

Enter current yearly income: 200000

Enter raise percentage: 4.5

The raise amount is: 9000.00



### EDIT 2 ### Did you copy my answer, or did you only add the %f in your printf format string? Because if you edited your code, you should also remove the ampersand (&) in front of e.ra.
cja
2010-12-08 06:03:43 UTC
I'm not sure what you mean when you say "... i have zero errors." My compiler (gcc) has this to say about your code:



emp.c:9: warning: return type of 'main' is not `int'

emp.c: In function `main':

emp.c:13: error: `Employee' undeclared (first use in this function)

emp.c:13: error: (Each undeclared identifier is reported only once

emp.c:13: error: for each function it appears in.)

emp.c:13: error: parse error before "e"

emp.c:18: error: `e' undeclared (first use in this function)

emp.c:24: warning: too many arguments for format

emp.c:24: warning: too many arguments for format



And, of course, the program isn't running the way you want it to, so there are errors, and not just those listed above. The execution problems you describe are because you're using scanf. My advice: don't use scanf; scanf leaves the newline in the input stream, and its an annoyance. Here's how I would fix your code:



#include



#define MAX_NAME_LEN 80

#define NUM_EMPLOYEES 4

#define MAX_LINE_LEN 128



typedef enum { false = 0, true } bool;

float getFlt(const char *p);

typedef struct {

    char name[MAX_NAME_LEN];

    float cye, rp, ra, npa;

} Employee;



int main(int argc, char *argv[]) {

    Employee e[NUM_EMPLOYEES];

    int i;



    for (i = 0; i < NUM_EMPLOYEES; i++) {

        printf("\nEnter employee name: ");

        fgets(e[i].name, MAX_NAME_LEN, stdin);

        e[i].cye = getFlt("Enter current yearly income");

        e[i].rp = getFlt("Enter raise percentage");

        e[i].ra = (e[i].cye * e[i].rp) / 100.0;

        printf("the raise amount is %g\n", e[i].ra);

    }

    return 0;

}



float getFlt(const char *p) {

    static char line[MAX_LINE_LEN];

    static char junk[MAX_LINE_LEN];

    bool inputOk;

    float z;



    printf("%s : ",p);

    for (inputOk = false; inputOk == false; ) {

        fgets(line,MAX_LINE_LEN,stdin);

        inputOk = (sscanf(line,"%f%s",&z,junk) == 1);

        if (inputOk == false) {

            printf("invalid input, try again: ");

        }

    }

    return z;

}
es
2010-12-07 23:44:11 UTC
The problem you are having is because of scanf() (This can happen with repeated calls to scanf) There is a non-standard call which helps in some cases. You can put a call to "_flushall()" without the quotes before the troublesome scanf() (obviously it's better to have a strictly correct way of doing it but I haven't gotten one working yet)



When I tried it with _flushall(), it still did not finish reading in a float until I pressed the period key for a decimal, and it would not let me enter any subsequent part of the number, either, despite being a float.



Many programmers recommend using variations of gets and getc but I would not be sure to how to use them to get floats.



I think you can safely read each number into a string (with either scanf() or gets()) and then use atof() to convert the string to a number -- there is no function to convert to float, so pass the string you have read in (instead of a float) to atof() and cast the double it returns to a float So if you read in a string called s, your float would be (float) atof(s). You might get some warnings about potential loss of precision.



The commenter is correct who said you forgot to put in the %f for the raise amount in printf("the raise amount is", &e.ra); and it should be printf("the raise amount is %d", &e.ra);



The other commenter missed the sentence indicating that, when working, this will be run on four faculty members (Once you get it working, you could make an array of four structures, if you chose)



### EDIT ###: This isn't pretty but it works.



void main ()

{

 

  struct Employee e;

  int i = 0;

  const int NUM_FACULTY = 4;

  const int MAX = 81;

  char a[MAX];



  for(i=0; i < NUM_FACULTY; i++ )

  {

   printf("Enter employee name: ");

   gets(e.name);

   printf("Enter current yearly income: ");

   gets( a );

   e.cye = (float) atof( a );

   printf("Enter raise percentage: ");

   gets( a );

   e.rp = (float) atof( a );

   e.ra=(e.cye * e.rp)/100.0;

   printf("the raise amount is %f\n", e.ra);

  }

  system ( "pause" );

}



Explanation:



It avoids the issues with scanf (by reading each float into a string, calling atof() to convert the string to double, and cast the double down to a float -- often floating point variables in programs are made doubles, since the Math functions use doubles and it's the highest precision) and runs four times and gets proper results.



I added a few variables, an array to read the input as a string which will then be converted to float, and a couple of constants to modularize the size of the array and the number of faculty (they are easier to update or isolate problems if declared this way instead of using bald numbers). As noted, the final printf should use e.ra rather than &e.ra.
carmel
2016-06-03 02:00:19 UTC
The first thing I notice is that the Product::Product constructor allocates a char array big enough to hold the item name, but doesn't copy the data over. You also have the same name for the constructor argument (itemName) as the data member. That won't work as-is because the argument name hides the data member name. You never actually store new char[] array in the object being constructed. Try this for a change: Product::Product( const char *itemName) { int size = strlen(itemName) + 1; this->itemName = new char[size]; strncpy(this->itemName, itemName, size); } That add the "this->" qualifier to force finding itemName in the object, and copies the contents of the argument into the created data member. The Tshirt::Tshirt constructor has a different problem. You have different field names for the arguments and field names, but all the assignments are backwards! You should be getting warnings about using uninitialized data members. If you are using Visual C++, use /W4 at the command line or open the project properties and (on the C/C++ general node) select /W4 from the drop-down box. Do this for all configurations. For GNU-based compilers (almost everything else, from Code::Blocks to NetBeans uses GNU), check your docs. The command line use -Wall and -Wextra to enable nitpicky warnings. Anyway, change the assignments to use something like: this->cost = c; this->insurance = i; You don't strictly need "this->" in this case, but it doesn't hurt. Had you done it the first time, you might have noticed what was wrong...or something like: this->c = cost; ...would just fail to compile and maybe wake you up.
Wajahat Karim
2010-12-07 23:05:04 UTC
your logic is right but you have few problems...... here i have listed below:



1. You have 1 employee 'e', then why are you using for loop from 0 to 4 to take input. It will take input 4 times and overwrite data of e again and again. So remove for loop....



2. You do need to specify & in printf() as you have did it here....

printf("the raise amount is", &e.ra);

it will print address, so remove that.....



I have modified your code.... try that one... i hope it will run....



#include

struct Employee

{

char name [20];

float cye, rp, ra, npa;

};

void main ()

{







Employee e;

//Remove for loop from here

printf("Enter employee name");

gets(e.name);

printf("\nEnter current yearly income");

scanf(" %f", &e.cye);

printf("\nEnter raise percentage");

scanf(" %f",&e.rp);

e.ra=(e.cye * e.rp)/100;

printf("the raise amount is", e.ra);



}
?
2010-12-07 22:51:57 UTC
it simply means that your program does not input the values of variables given by the users because you are not using the read command properly


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...