structs - why gets function is not working compared to scanf? [duplicate] - c

This question already has answers here:
scanf not reading properly because of gets function
(3 answers)
Closed 5 years ago.
gets(edu.classes[i].students[j].name);
I would like to know why the compiler skips this line after debugging and does not input into name? Is it legal to invoke gets function on that way?
Note: Once I use scanf("%s" , ... ) - it works!
scanf("%s",edu.classes[i].students[j].name);
(I know that I did not free the memory allocations and checked if the allocations were not failed - I know that it is necessary! It's only time issue) :)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 20
typedef struct
{
char name[SIZE];
int id;
}Student;
typedef struct
{
Student *students;
int num_students;
char teacher[SIZE];
}Class;
typedef struct
{
Class *classes;
int num_classes;
}Education;
int main()
{
int i, j;
Education edu;
puts("how many classes?");
scanf("%d", &(edu.num_classes));
edu.classes = (Class*)malloc((edu.num_classes) * sizeof(Class));
if (edu.classes == NULL)
{
printf("ERROR allocation\n");
exit(1);
}
for (i = 0; i < edu.num_classes; i++)
{
puts("enter num of students");
scanf("%d", &(edu.classes[i].num_students));
edu.classes[i].students = (Student*)malloc((edu.classes[i].num_students)
* sizeof(Student));
for (j = 0; j < edu.classes[i].num_students; j++)
{
puts("enter student's name");
gets(edu.classes[i].students[j].name); // this is the problematic line
puts("enter id");
scanf("%d", &(edu.classes[i].students[j].id));
}
}
return 0;
}

I think it is seeing the newline character that was produced due to hitting the enter key. gets then stops reading. Try eating those special characters before gets sees them. Or use scanf with %s which eats any leading whitespace as you have seen it works.

Related

C structures displaying numbers rather than user data

Kindly help me debug this code. It is not displaying the correct data. The following program is supposed to get book details from the user, dynamically allocate memory to them and display them.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "problem5.h"
int main()
{
struct books *b;
b = (struct books*)malloc(sizeof(struct books));
int command, flag = 0;
int n=0, i;
while(flag == 0)
{
printf ("1. Add Book\n");
printf ("2. View Books\n");
printf ("3. Quit\n");
scanf("%d", &command);
if (command == 1)
{
printf ("Enter Name\n");
//scanf("%d", &(b+i)->name);
scanf(" ");
gets((b+i)->name);
printf ("Enter Author\n");
//scanf("%d", &(b+i)->author);
scanf(" ");
gets((b+i)->author);
printf ("Enter Year Published\n");
scanf("%d", &(b+i)->year_published);
n=n+1;
i=n;
} else if (command == 2)
{
for(i=0; i<n; i++)
{
printf ("%d - %d by %d\n", (b+i)->year_published, (b+i)->name, (b+i)->author);
}
} else if (command == 3)
{
flag = 1;
} else
{
printf ("Invalid choice!\n");
}
}
}
The following is problem5.h header file that has the structure books. Initially I didn't declare the variables in array since I didn't want to use much memory. But I had to due to many errors.
#define PROBLEM3_H_INCLUDED
typedef struct books{
char *name[30];
char *author[30];
int year_published;
};
#endif // PROBLEM3_H_INCLUDED
When I print I am getting random numbers instead of the data the user entered.
The overall design of your code is wrong.
This is basically what you want.
I made following changements:
using meaningful variable names
changed struct book so the structure can contain one book. Also renamed it from struct books to struct book because the structure contains only one book.
allocating memory properly
using books[numberofbooks].x instead of the less readable *(books + numberofbooks)->x
More explanations in the comments.
#include <stdio.h>
#include <stdlib.h>
struct book {
char name[30];
char author[30];
int year_published;
};
int main()
{
struct book* books = NULL; // no books at all initially so we
// initialize to NULL
// so we can simply use realloc
int numberofbooks = 0;
int programend = 0;
while (programend == 0)
{
printf("1. Add Book\n");
printf("2. View Books\n");
printf("3. Quit\n");
int command;
scanf("%d", &command);
if (command == 1)
{
getchar(); // consume Enter key (due su scanf)
// allocate memory for one more book
books = realloc(books, sizeof(struct book) * (numberofbooks + 1));
printf("Enter Name\n");
gets(books[numberofbooks].name);
printf("Enter Author\n");
gets(books[numberofbooks].author);
printf("Enter Year Published\n");
scanf("%d", &books[numberofbooks].year_published);
numberofbooks++; // increment number of books
}
else if (command == 2)
{
for (int i = 0; i < numberofbooks; i++)
{
printf("%d - %s by %s\n", books[i].year_published, books[i].name, books[i].author);
}
}
else if (command == 3)
{
programend = 1;
}
else
{
printf("Invalid choice!\n");
}
}
}
There is still room for improvement though:
error checking for realloc
error checking for interactive I/O
not using the deprecated and dangerous gets
and certainly a few other things
b = (struct books*)malloc(sizeof(struct books));
Here, you are allocating memory for only one instance of struct books , But you are accessing multiple instances of struct books.
printf ("%d - %d by %d\n", (b+i)->year_published, (b+i)->name, (b+i)->author);
For i>=1 (b+i) is not defined, because you did not allocate memory for it. You have allocated memory for only (b+0).
int n=0, i;
gets((b+i)->name);
Here, i has not been initiliazed.

C compiler error: undefined reference to function

After I execute the exe I get this error :
undefined reference to `StudentScan'
error: ld returned 1 exit status|
Note: I'm bad and new to coding so don't mind my bad coding please^^
Note2: I'm just messing with random functions.
#include <stdio.h>
#include <stdlib.h>
struct student {
char firstName[20];
char AverageNum[2];
};
void StudentScan(int, struct student[]);
void StudentPrint(int, struct student[]);
int main() {
int i;
int length;
struct student *studentp;
printf ("\nEnter the host of students: ");
scanf ("%d ", &length);
struct student list[length];
studentp=malloc(length*sizeof(struct student));
if (studentp==NULL)
{
printf("Out of memory!");
return 0;
}
for(i = 0; i < length; i++) {
StudentScan(i,studentp);
printf("\nEnter average number: ");
scanf("%s", list[i].AverageNum);
}
free (studentp);
void StudentScan(int i, struct student list[])
{ printf("\nEnter first name : ");
scanf("%s", list[i].firstName);
printf("\nEnter average number: ");
scanf("%s", list[i].AverageNum);
}
return 0;
}
The posted code has defined StudentScan() within main(). But nested function definitions are not allowed in C. This should generate a compiler warning, such as:
warning: ISO C forbids nested functions [-Wpedantic]
void StudentScan(int i, struct student list[])
Pay attention to all compiler warnings and fix them. If no warning is seen when compiling this code, turn up the level of compiler warnings. On gcc, I suggest to always use at least gcc -Wall -Wextra, and I always add -Wpedantic. The -Wpedantic is needed with gcc to see a warning for this. Some compilers, and gcc is one of these, do support nested function definitions as a compiler extension. Still, this feature is nonstandard, and it is best to not rely on it.
The fix is simple: move the definition of StudentScan() out of main():
#include <stdio.h>
#include <stdlib.h>
struct student {
char firstName[20];
char AverageNum[2];
};
void StudentScan(int, struct student[]);
void StudentPrint(int, struct student[]);
int main(void) {
int i;
int length;
struct student *studentp;
printf ("\nEnter the host of students: ");
scanf ("%d ", &length);
struct student list[length];
studentp=malloc(length*sizeof(struct student));
if (studentp==NULL)
{
printf("Out of memory!");
return 0;
}
for(i = 0; i < length; i++) {
StudentScan(i,studentp);
printf("\nEnter average number: ");
scanf("%s", list[i].AverageNum);
}
free (studentp);
return 0;
}
void StudentScan(int i, struct student list[])
{ printf("\nEnter first name : ");
scanf("%s", list[i].firstName);
printf("\nEnter average number: ");
scanf("%s", list[i].AverageNum);
}
Also note that you should always specify maximum widths when reading strings using scanf() family functions with %s or %[] to avoid buffer overflow. For example:
scanf("%19s", list[i].firstName);
Note that 19 is used, even though the firstName field is an array of 20 char values. Remember that one space must be reserved for the \0 terminator. And since you are using %s to read a string into the AverageNum field, you should also have:
scanf("%1s", list[i].AverageNum);
That is, this field can only hold one digit. If the intention is to hold two digits, this field must be changed within the struct to: char AverageNum[3].
And while we are discussing scanf(), note that this function returns the number of successful assignments made during the function call. If no assignments are made, 0 is returned. This return value should always be checked. Consider: if the user mistakenly enters a letter when a digit is expected, nothing is stored in the intended variable. This may lead to undefined behavior. You may try something like this to validate numeric input:
printf ("\nEnter the host of students: ");
while (scanf ("%d ", &length) < 1) {
puts("Please enter a number");
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
This code asks the user to enter input again if a number is not entered when expected. Note that if the user does enter a non-digit, this character remains in the input stream and must be cleared before attempting to process more user input. The while loop is a typical construction which accomplishes this task.
Edit
Based on comments made by the OP, here is a modified version of the posted code. This version uses a float value instead of a character array for the AverageNum field of the struct. A floating-point type may be more useful than an integer type for storing averages. It is usually best to use double for floating-point values, but in this case it looks like AverageNum has little need for precision (the char array was intended to hold only two digits); float is probably sufficient for this use. If a different type is desired, it is simple enough to modify the code below.
Some input validation is implemented, but note that more could be done. The user is prompted to enter a number when non-numeric input is found where numeric input is expected. The input stream is cleaned with the while loop construction after such an input mistake; it would be good to remove this code to a separate function called clear_input(), for example.
If the user signals end-of-file from the keyboard, scanf() will return EOF; the code below chooses to exit with an error message rather than continue with malformed input in this case. This could also occur with input redirected from a file, and this condition may need to be handled differently if such input is expected.
The loop that populated the list[] array seemed to be operating inefficiently, asking for AverageNum twice in each pass. This has been streamlined.
Note that the call to malloc() can be rewritten as:
studentp = malloc(length * sizeof *studentp);
This is a very idiomatic way of writing such an allocation. Here, instead of using an explicit type as the operand of sizeof, that is, instead of sizeof (struct student), the variable which holds the address of the allocation is used. sizeof only uses the type of the expression *studentp, so this variable is not dereferenced here. Coding this way is less error-prone and easier to maintain when types change during the maintenance life of the code.
Yet, it is unclear why memory is allocated for studentp in the first place. In the posted code, both the firstName and AverageNum fields are filled for members of the dynamically allocated studentp in calls to StudentScan() in a loop; the same loop fills the AverageNum field of the members of list[] (a different array of structs) with different input. There seems to be no need for one of these arrays of student structs; I have commented-out the dynamically allocated array in favor of the statically allocated version.
Here is the modified code:
#include <stdio.h>
#include <stdlib.h>
struct student {
char firstName[20];
float AverageNum;
};
void StudentScan(int, struct student[]);
void StudentPrint(int, struct student[]);
int main(void) {
int i;
int length;
// struct student *studentp;
printf ("\nEnter the host of students: ");
while (scanf ("%d", &length) < 1) {
puts("Please enter a number");
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
struct student list[length];
/* This is fine */
// studentp = malloc(length * sizeof (struct student));
/* But this is better */
// studentp = malloc(length * sizeof *studentp);
// if (studentp == NULL)
// {
/* Not wrong, but... */
// printf("Out of memory!");
// return 0;
// fprintf(stderr, "Allocation failure\n");
// exit(EXIT_FAILURE);
// }
for(i = 0; i < length; i++) {
StudentScan(i, list);
}
/* Code to display results here */
// free (studentp);
return 0;
}
void StudentScan(int i, struct student list[])
{
putchar('\n');
printf("Enter first name: ");
if (scanf("%19s", list[i].firstName) != 1) {
puts("Input error");
exit(EXIT_FAILURE);
}
printf("Enter average number: ");
while (scanf("%f", &list[i].AverageNum) < 1) {
puts("Please enter a number");
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
}
You have to remove the scan function from the main. Also there is not a printstudent function you are declaring. You must remove /n from the printf and the scanf functions and place them accordingly. You can then test if your data are being added correctly in your struct with a simple loop.
#include <stdio.h>
#include <stdlib.h>
struct student {
char firstName[20];
char AverageNum[2];
};
int main() {
int i=0;
int length;
struct student *studentp;
printf ("Enter the host of students:");
scanf ("%d", &length);
struct student list[length];
studentp=malloc(length*sizeof(struct student));
if (studentp==NULL)
{
printf("Out of memory!");
return 0;
}
for(i = 0; i < length; i++) {
printf("Enter first name :");
scanf("%s", list[i].firstName);
printf("Enter average number: ");
scanf("%1s", list[i].AverageNum);
}
for(i = 0; i< length; i++){
printf("number of host is: %d , his/her first name: %s , his/her avg number: %s \n", i, list[i].firstName, list[i].AverageNum);
}
free (studentp);
return 0;
}

Find the greatest average among a student list using structure [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 5 years ago.
The problem is to find the greatest average of a group of students. As you know Structure works here and to my point of view it's a good idea (Or maybe) that first make that structure using Array of Structure because I have for example 30 students. Then searching through the members of that structure and find the ultimate answer.
But I've encountered a problem which I can't fill the struct Stdinfo student[stdnum] and my for loop actually doesn't work correctly and I don't know why!
As a check I used printf() to print one of the members but I couldn't.
Here is my code :
#include <stdio.h>
struct Stdinfo
{
char name[30];
int score;
};
struct Stdinfo function();
int main(int argc, char **argv)
{
//Number of students ~stdnum
int stdnum, i;
puts("Input numbers of student(s) :");
scanf("%i", &stdnum);
stdnum--;
struct Stdinfo student[stdnum];
//Filling array of structure
for (i = 0; i < stdnum; i++)
{
student[i] = function();
}
return 0;
}
struct Stdinfo function()
{
struct Stdinfo student;
puts("Input the name of the student : ");
fgets(student.name, sizeof(student.name), stdin);
puts("Input his(her) score:");
scanf("%i", &student.score);
return student;
}
Now searching is not my main problem and I appreciate any help by which I can solve "Structure's members filing" problem.
You decrease the size of the array incorrectly. When doing so, you ignore the last student. Discard that decrement.
Now think how the input comes. The user enters the number of students and then his Enter!
scanf("%i" ,&stdnum); will consume the number, but not the newline (from Enter).
As a result, fgets() will read that newline and stop there. In others words, it thinks that this is what the input actually is. To alleviate this problem, simply consume this newline, e.g. with getchar(), before fgets() gets called.
#include <stdio.h>
#include <string.h>
struct Stdinfo{
char name[30];
int score;
};
struct Stdinfo function() ;
int main(void)
{
//Number of students ~stdnum
int stdnum , i;
puts("Input numbers of student(s) :") ;
scanf("%i" ,&stdnum);
// Do not do that, it makes your program ignore the last student
//stdnum--;
struct Stdinfo student[stdnum] ;
//Filling array of structure
for(i=0 ; i < stdnum ; i++)
{
student[i] = function() ;
}
// print your array
for(i=0 ; i < stdnum ; i++)
{
printf("%s\n", student[i].name);
printf("%d\n", student[i].score);
}
return 0;
}
struct Stdinfo function() {
struct Stdinfo student;
getchar(); // Consume newline from previous input
puts("Input the name of the student : ") ;
fgets(student.name , sizeof (student.name) , stdin);
// remove trailing newline from 'fgets()'
student.name[strcspn(student.name, "\n")] = 0;
puts("Input his(her) score:") ;
scanf("%i" ,&student.score) ;
return student ;
}
PS: You may want to Removing trailing newline character from fgets() input (my example does this).

C: Structure usage [duplicate]

This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 5 years ago.
the lines after printf("enter nation\n"); to printf("enter m or f\n"); not executing like the codeblock can't see it as it prints the two line together with no chance for me to enter anything in between.. this problem always happen when i use struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int salary;
int age;
char name[10];
char m:1;
char nation:1;
};
int main()
{
int i,k;
char g,n;
char name2[10];
struct emp x[3];
for(i=0;i<3;i++)
{
printf("enter name\n");
scanf("%s",&name2);
strcpy(x[i].name,name2);
printf("enter salary\n");
scanf("%d",&x[i].salary);
printf("enter age\n");
scanf("%d",&x[i].age);
printf("enter nation\n");
cant see the next 3 lines and jump to enter m or f
scanf("%c",&n);
if(n=='e'){x[i].nation==0;}
else {x[i].nation==1;}
printf("enter m or f\n");
cant see those lines too
scanf("%c",&g);
if(g=='f'){x[i].m=0;}
else if(g=='m'){&x[i]==1;}
}
for(k=0;k<3;k++)
{
if(x[i].nation=='e')
{
puts(x[i].name);
printf("%d\n",x[i].salary);
printf("%d\n",x[i].age);
if(x[i].m==0)
printf("female\n");
else {printf("male\n");}
printf("egyptian");
}
}
return 0;
}
You have to change
scanf("%c",&n);
to
scanf(" %c",&n); // Skip leading whitespaces
Arrays in C are passed by pointer, so for
char name2[10];
You dont have to use reference operator
scanf("%s",&name2);
just simply do
scanf("%s",name2); // Will pass an arrays address
Also it would be nice to limit input, or buffer overflow may occur and it would lead to undefined behavior
scanf("%9s",name2);

Saving strings to structs using gets()? [duplicate]

This question already has answers here:
C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf [duplicate]
(7 answers)
Closed 6 years ago.
I'm running into some problems in saving a string - a name, for example - into a struct field. I've used gets() and fgets() both, but fgets() isn't working properly either.
I never get the chance to input the first employee name; it skips straight to the employee code and then skips the address too. For some reason, when inputting the second employee, I get to input both the name and code, and then it skips the address again.
Anyone know what I'm doing wrong?
#include <stdio.h>
typedef struct {
char name[150];
int code;
char add[300];
} tEmployee;
int main()
{
printf("How many employees would you like to register?\n");
int n;
scanf("%i", &n);
tEmployee employee[n];
for (int i = 0; i < n; i++)
{
printf("Name: ");
gets(employee[i].name);
printf("Code: ");
scanf("%i", &employee[i].code);
printf("Address: ");
gets(employee[i].add);
printf("%s\n", employee[i].name);
printf("%i\n", employee[i].code);
printf("%s\n", employee[i].add);
}
return 0;
}
The C library input routines aren't consistent about the way they handle newline (\n). Some read it as part of the input, some don't. Since scanf() gets what it needs before the newline, it has no reason to read it in so we have to so explicitly to clear it out of the buffer before our next input. There are different techniques but just calling getchar() works for this example.
Also, since gets() is considered unsafe, and leaves a newline dangling on the end of your input, I've added a custom my_gets() wrapper that fixes both issues:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[150];
int code;
char add[300];
} tEmployee;
char *my_gets(char *str, int size)
{
char *pos;
char *result = fgets(str, size, stdin);
if (result != NULL && (pos = strchr(str, '\n')) != NULL)
*pos = '\0';
return result;
}
int main()
{
int n;
printf("How many employees would you like to register?\n");
scanf("%i", &n);
getchar(); // eat newline \n
tEmployee employee[n];
for (int i = 0; i < n; i++)
{
printf("Name: ");
my_gets(employee[i].name, 150);
printf("Code: ");
scanf("%i", &employee[i].code);
getchar(); // eat newline \n
printf("Address: ");
my_gets(employee[i].add, 300);
printf("%s\n", employee[i].name);
printf("%i\n", employee[i].code);
printf("%s\n", employee[i].add);
}
return 0;
}
You could make a similar wrapper function for your specific use of scanf() that eats the extra newline for you so you don't have to worry about it everytime you call that function for input.
It's your mixed use of gets and scanf. I've faced similar problem in C++, when I mixed the use of std::cin and >> operator and std::getline function.
Also, gets is deprecated, don't use it...
Anyway, if you really want to use the both, then you should "flush" stdin each time you use scanf, or the next time you read stdin you will read the rest of it till the end of line (the \n).
One way to do it, is to read till the end of line after each scanf:
/* define the function */
void flush()
{
while (getchar() != '\n');
}
Then use it in your code as follow:
printf("How many employees would you like to register?\n");
int n;
scanf("%i", &n);
flush();
tEmployee employee[n];
for (int i = 0; i < n; i++)
{
printf("Name: ");
gets(employee[i].name);
printf("Code: ");
scanf("%i", &employee[i].code);
flush();
printf("Address: ");
gets(employee[i].add);
printf("%s\n", employee[i].name);
printf("%i\n", employee[i].code);
printf("%s\n", employee[i].add);
}
return 0;
Try this:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[150];
int32_t code;
char add[300];
} tEmployee;
typedef uint_fast8_t bool_t;
/*****************************************************************************
* flush stdin... this should be standard but somewhy you need to reinvent
* it all the time...
*****************************************************************************/
static inline void flush_stdin()
{
char ch;
do {
ch = getchar();
} while ((ch != '\n') && (ch != EOF));
}
/*****************************************************************************
* reads a line of text from a stream.
*****************************************************************************/
static inline bool_t xio_fgetline(FILE *stream, char *linebuf, size_t szline)
{
fgets(linebuf, szline, stream);
// find last character.
char *lc = linebuf + strlen(linebuf) - 1;
// the only case when lc is a null is if the program memory
// has been altered. In this case, it should crash anyway.
// therefore I skip a nullcheck before chomping.
// chomp linebuf.
if (*lc == '\n') {
*lc = 0;
}
// string is {0} after chomping.
if (strlen(linebuf) == 0) {
return 0;
}
return 1;
}
/*****************************************************************************
* reads a line of text from stdin.
*****************************************************************************/
static inline bool_t xio_getline(char *linebuf, size_t szline)
{
return (xio_fgetline(stdin, linebuf, szline));
}
int main(int argc, char **argv)
{
int32_t n;
tEmployee *employee = (tEmployee *)0;
printf("How many employees would you like to register?\n");
scanf("%i", &n);
flush_stdin();
employee = (tEmployee *)malloc(n * sizeof(tEmployee));
for (int32_t i = 0; i < n; ++i) {
printf("Name: ");
xio_getline(employee[i].name, sizeof(employee[i].name));
printf("Code: ");
scanf("%i", &employee[i].code);
flush_stdin();
printf("Address: ");
xio_getline(employee[i].add, sizeof(employee[i].add));
printf("%s\n", employee[i].name);
printf("%i\n", employee[i].code);
printf("%s\n", employee[i].add);
}
free(employee);
return 0;
}

Resources