saving . . . saved Regarding Assignment has been deleted. Regarding Assignment has been hidden .
Regarding Assignment
Title
Question
I don't understand what is wrong with the following code. Please help.

<span style="color: #0000ff;">#include</span> <span style="color: #0000ff;"><</span><span style="color: #a31515;">stdio.h</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;">#include</span> <span style="color: #0000ff;"><</span><span style="color: #a31515;">string.h</span><span style="color: #0000ff;">></span>

<span style="color: #0000ff;">struct</span> employee
{
<span style="color: #0000ff;">char</span> name[<span style="color: #09885a;">100</span>];
<span style="color: #0000ff;">char</span> address[<span style="color: #09885a;">100</span>];
<span style="color: #0000ff;">char</span> desgn[<span style="color: #09885a;">100</span>];
<span style="color: #0000ff;">float</span> salary;
};

<span style="color: #0000ff;">int</span> main()
{
<span style="color: #0000ff;">struct</span> employee details;
printf(<span style="color: #a31515;">"Please enter the full name of the employee : "</span>);
scanf(<span style="color: #a31515;">"%[^</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">]s"</span>, details.name);

printf(<span style="color: #a31515;">"</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">"</span>);

printf(<span style="color: #a31515;">"Please enter the address of the employee : "</span>);
scanf(<span style="color: #a31515;">"%[^</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">]s"</span>, details.address);

printf(<span style="color: #a31515;">"</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">"</span>);

printf(<span style="color: #a31515;">"Please enter the designation of the employee : "</span>);
scanf(<span style="color: #a31515;">"%[^</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">]s"</span>, details.desgn);

printf(<span style="color: #a31515;">"</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">"</span>);

printf(<span style="color: #a31515;">"Please enter the salary of the employee : "</span>);
scanf(<span style="color: #a31515;">"%f"</span>, details.salary);

printf(<span style="color: #a31515;">"</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">"</span>);
printf(<span style="color: #a31515;">"</span><span style="color: #ff69b4;">\n</span><span style="color: #a31515;">"</span>);

printf(<span style="color: #a31515;">"PAY SLIP"</span>);
printf(<span style="color: #a31515;">"This is to certify that Mr/Ms. %s ,who lives at %s, works in our company as the %s. He receives %f only, as salary per month."</span>, details.name, details.address, details.desgn, details.salary);

<span style="color: #0000ff;">return</span> <span style="color: #09885a;">0</span>;
}

C-and-Cpp Working-With-Structures 05-06 min 40-50 sec 24-10-20, 11:48 p.m. bsscharan

Answers:

I believe the way you are taking the input is not okay. Please try adding & in your scanf statements, as given below:

scanf("%[^\n]s", &details.name); 


24-10-20, 11:57 p.m. sudhakarst
Sorry sir, 'copy and paste' is giving here like that. If you see there are scanf statements in the above code. Please use the following link for my code:

<Please copy and paste the following code in a browser to access my code and run it.>
https://repl.it/@coder746/Details-of-an-Employee-Assignment#main.c

I have commented some of my other doubts in the code, sir. Please check them also.

The problem is, I am not able to enter the address and designation of the employee as user input.
Thank you

25-10-20, 12:44 p.m. bsscharan

Login to add comment


Here is something to help you out:

<font color="#576871" face="OpenSans, Arial, Helvetica, sans-serif"><span style="font-size: 14px;">"</span><font size="3">The statement: scanf("%[^\n]%*c", s); will not work because the last statement will read a newline character, \n, from the previous line. This can be handled in a variety of ways. One way is to use scanf("\n"); before the last statement as given below: </font></font>
<span style="color: rgb(0, 0, 0); font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; white-space: pre;">
</span>
<span style="color: rgb(0, 0, 0); font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; white-space: pre;">
</span>
<span style="color: rgb(0, 0, 0); font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; white-space: pre;"> printf(</span><span style="font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; white-space: pre; color: rgb(163, 21, 21);">"Please enter the address of the employee : "</span><span style="color: rgb(0, 0, 0); font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; white-space: pre;">);</span>
scanf(<span style="color: rgb(163, 21, 21);">"\n%[^</span><span style="color: rgb(255, 105, 180);">\n</span><span style="color: rgb(163, 21, 21);">]s"</span>, details.address);

printf(<span style="color: rgb(163, 21, 21);">"Please enter the designation of the employee : "</span>);
scanf(<span style="color: rgb(163, 21, 21);">"\n%[^</span><span style="color: rgb(255, 105, 180);">\n</span><span style="color: rgb(163, 21, 21);">]s"</span>, details.desgn);

16-11-21, 12:55 a.m. monsur_001



Problem is with scanf() statements
if you want to use multiple scanf() statements with strings then you need to use fgets() with scanf().
 
  scanf("%[^\n]s", details.name);
 
  fgets(details.address,100, stdin);
  scanf("%[^\n]s", details.address);
 
  fgets(details.desgn,100, stdin);
  scanf("%[^\n]s", details.desgn);
 
  scanf("%f", &details.salary);
31-01-22, 10:32 p.m. Ravi_Ns


#include<stdio.h>
#include<conio.h>
Struct employee 
{
char N[20];
char A[20];
char D[20];
float salary;
};
Void main()
{
Struct employee emp
emp.N[20]="Ram";
emp.A[20]="Bvpm";
emp.D[20]="HR executer";
emp.salary=50,000;
Printf("employee record:");
Printf("name:%s",N);
Printf("address:%s",A);
Printf("designation:%s",D);
Printf("salary :%f", salary );
getch();
}

Output:
Employee record:
name:Ram
Address:Bvpm
Designation:HR executer 
Salary :50,000

19-08-24, 6:38 a.m. 238T1A05D9


#include<stdio.h>
#include<conio.h>
Struct employee 
{
char N[20];
char A[20];
char D[20];
float salary;
};
Void main()
{
Struct employee emp
emp.N[20]="Ram";
emp.A[20]="Bvpm";
emp.D[20]="HR executer";
emp.salary=50,000;
Printf("employee record:");
Printf("name:%s",N);
Printf("address:%s",A);
Printf("designation:%s",D);
Printf("salary :%f", salary );
getch();
}

19-08-24, 2:19 p.m. Nikki5D4


#include<stdio.h>
#include<conio.h>
Struct Employee
{
char name[20];
char add[20];
float salary;
};
Void main()
{
Struct employee emp
emp.name[20]="shaik";
emp.add[20]="gosala";
emp.salary=50,000;
Printf("employee record:");
Printf("name:%s",name);
Printf("address:%s",add);
Printf("salary :%f", salary );
getch();
}

Output:
employee record:
name:shaik
address:gosala
salary :50,000
19-08-24, 2:44 p.m. 238T1A05G7


#include<stdio.h>
#include<conio.h>
Struct employee 
{
char N[20];
char A[20];
char D[20];
float salary;
};
Void main()
{
Struct employee emp
emp.N[20]="Ram";
emp.A[20]="Bvpm";
emp.D[20]="HR executer";
emp.salary=50,000;
Printf("employee record:");
Printf("name:%s",N);
Printf("address:%s",A);
Printf("designation:%s",D);
Printf("salary :%f", salary );
getch();
}

19-08-24, 2:51 p.m. Shalini_09


#include<stdio.h>
#include<conio.h>
Struct employee 
{
char N[20];
char A[20];
char D[20];
float salary;
};
Void main()
{
Struct employee emp
emp.N[20]="Ram";
emp.A[20]="Bvpm";
emp.D[20]="HR executer";
emp.salary=50,000;
Printf("employee record:");
Printf("name:%s",N);
Printf("address:%s",A);
Printf("designation:%s",D);
Printf("salary :%f", salary );
getch();
}

19-08-24, 3:28 p.m. nasreensk2005@gmail.com


#include<stdio.h>
#include<conio.h>
Struct employee 
{
char Name[20];
char Add[20];
char Des[20];
float salary;
};
Void main()
{
Struct employee emp
emp.Name[20]="Lamees";
emp.Add[20]="Vuyyuru";
emp.Des[20]="HR executer";
emp.salary=60,000;
Printf("employee record:");
Printf("Name:%s",Name);
Printf("Address:%s",Add);
Printf("Designation:%s",Des);
Printf("Salary :%f", salary );
getch();
}

Output:
Employee record:
Name:Lamees
Address:Vuyyuru
Designation:HR executer 
Salary :60,000

19-08-24, 4:19 p.m. 238T1A05G1


Log-in to answer to this question.