Can anyone tell me what's wrong in my code given below? - c

I want to take input from user using structure. So I'm using the code like below. But it's not printing the values which I'm entering. Can anyone help me?
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
But I'm not getting the output as desired. My
Output Image

Your question is similar to this one: scanf() leaves the newline character in the buffer
And can be corrected like this:
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
char buffer[255];
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf(" %c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
Note the leading space before the %c input.

Related

printf not printing integer stored in variable

For some weird reason when I input the values in the marks section, the printed values are all 0, please help, I tried running a smaller version of the code and it worked I don't know why this doesn't.
here is the code:
Stack overflow keeps giving me an error since apparently most of my question is code so pls ignore this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marksComm;
char gradeComm[1];
int marksEco;
char gradeEco[1];
int marksCompsys;
char gradeCompsys[1];
int marksProg;
char gradeProg[1];
int marksDis;
char gradeDis[1];
int marksLab;
char gradeLab[1];
int marksPhy;
char gradePhy[1];
printf("CCS001\nMarks> ");
scanf("%d", &marksComm);
printf("Grade> ");
scanf("%s", &gradeComm);
printf("\nCCS009\nMarks> ");
scanf("%d", &marksEco);
printf("Grade> ");
scanf("%s", &gradeEco);
printf("\nCSC111\nMarks> ");
scanf("%d", &marksCompsys);
printf("Grade> ");
scanf("%s", &gradeCompsys);
printf("\nCSC112\nMarks> ");
scanf("%d", &marksProg);
printf("Grade> ");
scanf("%s", &gradeProg);
printf("\nCSC113\nMarks> ");
scanf("%d", &marksDis);
printf("Grade> ");
scanf("%s", &gradeDis);
printf("\nCSC126\nMarks> ");
scanf("%d", &marksPhy);
printf("Grade> ");
scanf("%s", &gradePhy);
printf("\nCSC115\nMarks> ");
scanf("%d", &marksLab);
printf("Grade> ");
scanf("%s", &gradeLab);
printf("\nCourse Code Marks Grade\n");
printf("CCS001 %d %s \n", marksComm, gradeComm);
printf("CCS009 %d %s \n", marksEco, gradeEco);
printf("CSC111 %d %s \n", marksCompsys, gradeCompsys);
printf("CSC112 %d %s \n", marksProg, gradeProg);
printf("CSC113 %d %s \n", marksDis, gradeDis);
printf("CSC115 %d %s \n", marksLab, gradeLab);
printf("CSC126 %d %s \n", marksPhy, gradePhy);
}
Your arrays are two small. C strings are NUL-terminated, so you need a char for the letter, and a char for the NUL. That's two char. But you reserve space for one.

can anyone tell me where is the mistakes? [duplicate]

This question already has an answer here:
C Access violation writing location scanf_s
(1 answer)
Closed 1 year ago.
While the program is running , in the middle of it (after typing the Vaccine name ) it stops and gives me {Exception Thrown} can anyone help me fix this issue? I have also included a photo of the error it gives me
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int choose, dosage, quantity;
char code[10];
char name[32];
char produce[64];
float population;
{
printf("COVID-19 Vaccination System \n");
printf("1:- Inventory Creation \n");
printf("2:- Update Vaccine Quantities \n");
printf("3:- Search Vaccine \n");
printf("4:- Produce a list of all Vaccines and their distributed Quantities \n");
printf("5:- EXIT \n\n");
printf("==>");
scanf_s("\n%d", &choose);
if (choose == 1)
{
char ch;
FILE* cv;
errno_t err;
err = fopen_s(&cv, "vaccine.txt", "w");
for (int i = 0; i < 5; i++)
{
printf("Please Enter Vaccine Full information %d \n", i + 1);
printf(" Name of Vaccine : ");
scanf_s("\n%s", &name);
printf("Please Enter Vaccine Code %c \n", i + 1);
printf(" Vaccine Code : ");
scanf_s("\n%s", &code);
printf("Please Enter Vaccine Producing Country %c \n", i + 1);
printf("Producing Country : ");
scanf_s("\n%s", &produce);
printf("Please Enter Dosage Required %d \n", i + 1);
printf("Dosage Required (MAX=2 - MIN=1) : ");
scanf_s("\n%d", &dosage);
printf("Please Enter Population Covered %d \n", i + 1);
printf("Population Covered (%) : ");
scanf_s("\n%f", &population);
printf("Please Enter Vaccine Quantity %d \n", i + 1);
scanf_s("\n%d", &quantity);
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
}
}
}
return 0;
}
The line
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
is wrong. You have to pass a file pointer as the first argument of fprintf(). It should be:
fprintf(cv, "%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
Also
You should check if fopen_s succeeded before proceeding to next operation and stop operation if it failed.
You should remove & before the array names (code, name, produce) used in scanf_s. Array in expressions (excluding some exceptions) are automatically converted to pointers to the first element, so no explicit & is not needed here. Also it will make the type wrong scanf() expects char* for %s, but things like &code are an pointers to arrays (like char(*)[10]).

The input that i give to my %f does not save

The gradeOne and secondGrade does not save the number that i give. So i cant have the average , because, always end in a division of (0 + 0)/2, can anyone help me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
%f in scanf() is for reading float. You should use %lf to read double.
Note that you should use %f in printf() for printing double. Newer specification allows %lf for printf(), but %f should be better for compatibility.

C - Practice with pointers and Structs - Getting string with spaces

I'm practicing C using structs and pointers, I'm asking to the user to input some info using structs and then using pointers to modify it but I'm having problems when I need to get a string with spaces. I switched all the scanfs for fgets and still getting problems with the output.
#include <stdio.h>
#include <stdlib.h>
#define MAXTSTR 25
int main(int argc, char **argv) {
typedef struct birthday {
int day, year;
char month[10];
} BDATE;
struct employee {
char name[MAXTSTR];
int id;
float salary;
BDATE bday;
} emp;
struct employee *ptrEmp = &emp;
printf("Enter employee name:");
fgets(emp.name, MAXTSTR, stdin );
printf("enter id number:");
scanf(" %d", &emp.id);
printf("enter salary");
scanf(" %f", &emp.salary);
printf("enter birhday:");
scanf(" %d", &emp.bday.day);
printf("enter year:");
scanf(" %d", &emp.bday.year);
printf("enter month:");
fgets(emp.bday.month, MAXTSTR, stdin);
printf("\nName: %s \nID: %d \nSalary: %.2f\n", emp.name, emp.id, emp.salary);
printf("%d %s %d", emp.bday.day, emp.bday.month, emp.bday.year);
printf("\n------------------------------------\n");
printf("Enter employee name:");
fgets(ptrEmp->name, MAXTSTR, stdin);
printf("enter id number:");
scanf(" %d", &ptrEmp->id);
printf("enter salary");
scanf(" %f", &ptrEmp->salary);
printf("enter birhday:");
scanf(" %d", &ptrEmp->bday.day);
printf("enter year:");
scanf(" %d", &ptrEmp->bday.year);
printf("enter month:");
scanf(ptrEmp->bday.month, MAXTSTR, stdin);
printf("\nName: %s \nID: %d \nSalary: %.2f\n",
ptrEmp->name, ptrEmp->id, ptrEmp->salary);
printf("%d %s %d", ptrEmp->bday.day, ptrEmp->bday.month,
ptrEmp->bday.year);
return (EXIT_SUCCESS);
}
INPUT and OUTPUT EXAMPLE
Enter employee name:Luis Oliveira
enter id number:01
enter salary1525.25
enter birhday:05
enter year:1991
enter month:
Name: Luis Oliveira
ID: 1
Salary: 1525.25
5
1991
------------------------------------
Enter employee name:Patricia Santos
enter id number:02
enter salary16546.46
enter birhday:05
enter year:1946
enter month:Fev
Name: Patricia Santos
ID: 2
Salary: 16546.46
5
1946
What I'm doing wrong?
Thank you in advance for your help.
Mixing scanf() and fgets() can be very confusing: scanf() leaves the pending newline in the input stream and fgets() reads it and immediately returns because it has reached the end of line.
You can read the month with scanf("%9s", emp.bday.month); and the employee name with scanf(" %14[^\n]", ptrEmp->name);.
Also check for invalid input causing scanf() to return a value different from the expected 1.
Modified program:
#include <stdio.h>
#include <stdlib.h>
#define MAXTSTR 25
typedef struct birthday {
int day, year;
char month[10];
} BDATE;
struct employee {
char name[MAXTSTR];
int id;
float salary;
BDATE bday;
};
int main(int argc, char **argv) {
struct employee emp;
for (int i = 0; i < 2; i++) {
printf("Enter employee name: ");
if (scanf(" %14[^\n]", emp.name) != 1)
return EXIT_FAILURE;
printf("enter id number: ");
if (scanf(" %d", &emp.id) != 1)
return EXIT_FAILURE;
printf("enter salary: ");
if (scanf(" %f", &emp.salary) != 1)
return EXIT_FAILURE;
printf("enter birthday: ");
if (scanf(" %d", &emp.bday.day) != 1)
return EXIT_FAILURE;
printf("enter year: ");
if (scanf(" %d", &emp.bday.year) != 1)
return EXIT_FAILURE;
printf("enter month: ");
if (scanf("%9s", emp.bday.month) != 1)
return EXIT_FAILURE;
printf("\n\nName: %s\nID: %d\nSalary: %.2f\n", emp.name, emp.id, emp.salary);
printf("Birthday: %d %s %d\n", emp.bday.day, emp.bday.month, emp.bday.year);
printf("\n------------------------------------\n");
}
return EXIT_SUCCESS;
}

C - Scanf not getting correct info

Why is it when I run my code I get this result:
Name: Zeref
Age: 20
Float: 20.11
-----
Name: Zeref
Age: 1072324272
Dec: 0.000000
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char name[524288];
int age[524288];
float dec[524288];
printf("Name: ");
scanf("%s", name);
printf("Age: ");
scanf("%d", age);
printf("Float: ");
scanf("%f", dec);
printf("-----\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Dec: %f\n", dec);
return 0;
}
it isn't taking in what I say correctly, why?
I want it to print out exactly what I write but using age as an int, dec as a float and name as a string. Only name works
This is just printing the address of the array age as a decimal integer (actually it introduces undefined behavior);
printf("Age: %d\n", age);
Try this
printf("Age: %d\n", age[0]);

Resources