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
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);
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, "Courier New", monospace; font-size: 14px; white-space: pre;">
</span>
</span>
<span style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">
</span>
</span>
<span style="color: rgb(0, 0, 0); font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> printf(</span><span style="font-family: Consolas, "Courier New", 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, "Courier New", 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);
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);
#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
#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();
}
#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
#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();
}
#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();
}
#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
Login to add comment