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


Log-in to answer to this question.