The output not displaying after scanf() using c programming - c

Here im working on my first coding in c programming. I've got the problem when I want to get the user input and display the output from the user input. here my code:
#include <stdio.h>
int main(){
printf("Enter the number : ");
int hallo = 0;
scanf("%d", hallo);
printf("hallo, %d", hallo);
}
after executing the code the last line not appear where is prinf("hallo, %d", hallo);. Which is to display the user input.

The 4th line of the code: scanf("%d", hallo);
Here &hallo should be used instead of just the variable name hallo.
The significance of the & sign is that it gives the address of a particular variable. So whatever the value is entered by the user, it will be stored at the address of the variable (in this case at the address of variable hallo).

Use the & after the , in scanf
Like this
scanf("%d", & hallo);

#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
double salary;
} Employee;
int main()
{
int n;
printf("Number of employee to process: ");
scanf("%d",&n);
Employee employees[n];
printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
printf("Name: ");
scanf("%[^\n]s",employees[i].name);
printf("Id: ");
scanf("%d",&employees[i].id);
printf("Salary: ");
scanf("%lf",&employees[i].salary);
char ch = getchar();
printf("\n");
}
printf("-------------- All Employees Details ---------------\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}

Related

1.How can I compare 2 chars 2.How can I increase the size of my dynamic array using realloc and print the information it has

I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.

MY code is only displaying the input which i entered at the very end. I have attached my full code. PLEASE help me find where i am doing it wrong

QUESTION:
WAP to enter id, name, age and basic salary of n number of employees. Calculate the gross salary of all the employees and display it along with all other details in a tabular form, using pointer to structure.
--->
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee emp, *ptr;
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp.id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", &emp.name);
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp.age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp.salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
ptr = &emp;
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}
First of all you need to save each of your employees in variable so you can refer them later. Second because your variable is structure and you want loop through it your best choice is to use array.
I changed your code and highlighted them it worked for me i hope it will help you.
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee *ptr; //One change is here
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
struct employee emp[n];
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp[i].id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", emp[i].name); // Another change is here "%s" doesn't need '&'
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp[i].age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp[i].salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
ptr = &emp[i]; // This yet another change i grab "ptr" down here and assign it to "i" in each loop
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}

String and for loop [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I wrote this C program to enter names and ages of 3 people. But the output wasn't my expectation. It was able to enter name and age for the first person, but it wasn't able for second and third persons. Please help.
#include <stdio.h>
#include <string.h>
int main()
{
int i, age;
char name[20];
for(i=0; i<3; i++)
{
printf("\nEnter name: ");
gets(name);
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
}
return 0;
}
In short: Your 2nd puts is processing the '\n' from your scanf.
Fix by adding getchar(); after scanf
Explanation:
1st iteration:
printf("\nEnter name: ");
gets(name); // line is read from input
printf("Enter age: ");
scanf(" %d", &age); // a number is read from input, and the newline char ('\n') remains in buffer
puts(name);
printf(" %d", age);
2nd iteration:
printf("\nEnter name: ");
gets(name); // previously buffered newline char is read, thus "skipping" user input
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
Same goes for 3rd iteration, and this is why you lose user input
The best way to store information of more than one person is to use struct, like
struct person {
int age;
char name[20];
};
and make array of struct, like
struct person people[3];
than use loop with accessing people[i].age and people[i].name, e.g.:
#include <stdio.h>
#include <string.h>
struct person {
int age;
char name[20];
};
#define ARR_SIZE 3
int main(int argc, char* argv[])
{
struct person people[ARR_SIZE];
int i;
char *lastpos;
for(i = 0; i < ARR_SIZE; i++)
{
printf("\nEnter name: ");
scanf(" %s", people[i].name);
if ((lastpos=strchr(people[i].name, '\n')) != NULL) *lastpos = '\0'; // remove newline from the end
printf("Enter age: ");
scanf(" %d", &people[i].age);
}
printf("This is the people you entered:\n");
for(i = 0; i < ARR_SIZE; i++)
{
printf("%d : %s : %d\n", i+1, people[i].name, people[i].age);
}
return 0;
}
UPDATE:
As you see I use scanf(" %s", people[i].name); instead of gets(people[i].name); to read name from stdin. Try both option for the following cases:
enter short name (e.g. John) and correct age (e.g. 15)
enter two word name (e.g. John Smith) and correct age (e.g. 17)
enter short name and uncorrect age (e.g. five)
Then read articles about value returned by scanf and cleaning input buffer

C Program exits before taking input from user

Why does my program close before taking the input for k and then displaying it.
I am writing a code for a menu based program so I need to take input from user after he has entered the information so I can have 1.Print names 2.Exit
while doing this I realized my program didn't take the input and just skipped the part where it is supposed to take value of l from user. So trying to debug it I deleted stuff and came down to this simple program and realized it still wont work any idea why?
#include <stdio.h>
struct student
{
char name[50];
char lname[50];
float marks;
} s[15];
int main ()
{
int i, j,k;
printf("Please enter the number of students:\n");
scanf ("%d", &j);
printf ("Please enter the information for students as asked.\n");
for (i = 0; i < j; i++)
{
scanf ("%s %s %f\n", s[i].name, s[i].lname, &s[i].marks);
}
printf("Please enter a number\n");
scanf ("%d", &k);
printf("your number was %d", k);
return 0;
}
scanf ("%s %s %f\n", s[i].name, s[i].lname, &s[i].marks);
should be
scanf ("%s %s %f", s[i].name, s[i].lname, &s[i].marks);
The \n in scanf just consumes newline char. It will continue consuming newline until a non-newline char is found, which is put back into stdin for the next IO operation
Try this Code
#include<stdio.h>
typedef struct student
{
char name[50];
char lname[50];
int mark;
}S;
int main ()
{
int i, j,k;
printf("Please enter the number of students:\n");
scanf ("%d", &j);
S record[j];
for (i = 0; i < j; i++) {
printf ("Please enter the information for %d student as asked.\n",i+1);
scanf ("%s %s %f",record[i].name, record[i].lname, &record[i].mark);
}
printf("Please enter a number\n");
scanf ("%d", &k);
printf("your number was %d \n", k);
return 0;
}
You were declaring structure student array in the structure declaration itself.you have to declare the array in main function.

Unable to enter Name with Spaces in C program?

This program asks for employee name, age and Salary. There is one slight problem and i don't know what this wrong. When i enter name without space or just the first name, then program works correctly but as soon as i enter full name with space. The program just skips some values without asking and jumps to other values. I know their is something wrong with scant of "worker[i].name" and i have tried %c too but with no success.
struct employee {
int pAge;
float salary;
char name[30];
};
int main(void) {
struct employee worker[2];
for (int i = 0; i < 2; i++) {
printf("Enter Name of %d employee: ", (i+1));
scanf(" %s", worker[i].name);
printf("Enter Age of %d employee: ", (i+1));
scanf("%d", &worker[i].pAge);
printf("Enter Salary of %d employee: ", (i+1));
scanf("%f", &worker[i].salary);
}
printf("\n");
printf("List of All workers\n\n");
printf( "Age\tSalary\t\tName\n");
for (int i = 0; i < 2; i++) {
printf("%d\t%.2f\t\t%s\n", worker[i].pAge, worker[i].salary, worker[i].name);
}
}
Its very simple just use this instead:
scanf(" %[^\n]s", worker[i].name);

Resources