I have this C code:
#include "stdio.h"
main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
printf("Enter details of first book\n");
gets(book1.name);
gets(book1.author);
scanf("%d%d",&book1.year,&book1.copies);
printf("Enter details for second book\n");
gets(book2.name);
gets(book2.author);
scanf("%d%d",&book2.year,&book2.copies);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
}
What is happening here is that it only scans till the author name of the second book. After that it directly prints the output.
Here is my input:(The first two lines are the initial printf statements)
Enter details of first book
warning: this program uses gets(), which is unsafe.
the c programmign laguagne
dfadsda
3432
23
Enter details for second book
ruby on rails
mark hammers
After which it directly prints the output:
the c programmign laguagne
dfadsda
3432
23
ruby on rails
0
0
What is wrong here? Also we can see that the name of the second book is assinged to the author.
I'm using gcc as the compiler on Mac OS X ML.
Use fflush(stdin) before each input statement. This method will clear the input buffer.
After the modification your code will be-
#include "stdio.h"
int main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
printf("Enter details of first book\n");
gets(book1.name);
fflush(stdin);
gets(book1.author);
fflush(stdin);
scanf("%d%d",&book1.year,&book1.copies);
fflush(stdin);
printf("Enter details for second book\n");
gets(book2.name);
fflush(stdin);
gets(book2.author);
fflush(stdin);
scanf("%d%d",&book2.year,&book2.copies);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
return 0;
}
You can see the details about fflush() here.
UPDATED :
Here after the scanf() statement you need to flush the input buffer. The fflush() method is not useful here because it is defined only for output streams. You can consume the rest of a partially-read line with a single line code after each scanf() line, like -
while((c = getchar()) != '\n' && c != EOF);
Than your code will be:
#include "stdio.h"
int main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
char c;
printf("Enter details of first book\n");
gets(book1.name);
gets(book1.author);
scanf("%d%d",&book1.year,&book1.copies);
while((c = getchar()) != '\n' && c != EOF);
printf("Enter details for second book\n");
gets(book2.name);
gets(book2.author);
scanf("%d%d",&book2.year,&book2.copies);
while((c = getchar()) != '\n' && c != EOF);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
return 0;
}
OUTPUT :
Enter details of first book
warning: this program uses gets(), which is unsafe.
sadsadas
asa
12
34
Enter details for second book
zxczxc
sds
23
22
sadsadas
asa
12
34
zxczxc
sds
23
22
In your source code,
scanf("%d%d",&book1.year,&book1.copies);
does not read the "\n" after "23" because this just reads two integers.
One solution for this problem is to do gets() before reading the second book, like:
#include "stdio.h"
main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
printf("Enter details of first book\n");
gets(book1.name);
gets(book1.author);
scanf(" %d %d",&book1.year,&book1.copies);
char a[100];
gets(a);
printf("Enter details for second book\n");
gets(book2.name);
gets(book2.author);
scanf(" %d %d",&book2.year,&book2.copies);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
}
For this reason, reading integer using gets and using atoi after that is simpler method.
#include "stdio.h"
main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
printf("Enter details of first book\n");
gets(book1.name);
gets(book1.author);
char buff[100];
gets(buff);
book1.year = atoi(buff);
gets(buff);
book1.copies = atoi(buff);
printf("Enter details for second book\n");
gets(book2.name);
gets(book2.author);
gets(buff);
book2.year = atoi(buff);
gets(buff);
book2.copies = atoi(buff);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
}
Solution:
#include <stdio.h> /* Using fgets(), scanf(), printf() in this program */
#include <string.h> /* Using strlen() in this program */
int main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
char c;
char read_new_line;
printf("Enter details of first book\n");
if (fgets(book1.name, sizeof(book1.name), stdin) == NULL)
{
fprintf(stderr, "error reading name of book 1\n");
return -1;
}
/* Strip out \n character added by fgets */
book1.name[strlen(book1.name) - 1] ='\0';
if (fgets(book1.author, sizeof(book1.author), stdin) == NULL)
{
fprintf(stderr, "error reading author of book 1\n");
return -1;
}
/* Strip out \n character added by fgets */
book1.author[strlen(book1.author) - 1] ='\0';
scanf("%d %d",&book1.year,&book1.copies);
/* Strip out \n character left out in input stream */
while ((read_new_line = getchar()) != EOF && read_new_line != '\n')
;
printf("Enter details for second book\n");
if (fgets(book2.name, sizeof(book2.name), stdin) == NULL)
{
fprintf(stderr, "error reading name of book 2\n");
return -1;
}
/* Strip out \n character added by fgets */
book2.name[strlen(book2.name) -1 ] = '\0';
if (fgets(book2.author, sizeof(book2.author), stdin) == NULL)
{
fprintf(stderr, "error reading author of book 2\n");
return -1;
}
/* Strip out \n character added by fgets */
book2.author[strlen(book2.author) - 1] ='\0';
scanf("%d %d",&book2.year,&book2.copies);
/* Strip out \n character left out in input stream */
while((c = getchar()) != '\n' && c != EOF)
;
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
return 0;
}
Observation on code posted in the question:
Lets try to understand why your code is not working:
After the call to scanf from below statement
scanf("%d%d",&book1.year,&book1.copies);
Your input is
3432\n
23\n
scanf reads in 3432 and stores in &book1.year, following \n gets left out in input
stream. Then, second %d discards leading whitespaces (whitespaces in this context includes spaces, tabs, new line, etc.) and reads in 23 and stores that in &book1.copies, following \n gets left out in input stream.
when gets(book2.name) is called \n left out in input stream matches gets() criteria and hence 'empty string' is assigned to book2.name and whatever is meant and user input provided for book2.name is stored in book2.author.
Followed by whatever string meant for book2.author and typed as user input is assigned to book2.year%d conversion is done to that and it fails, as no proper integer entered and scanf() returns failed.
Note :
Use of gets() is by itself real bad. As you use %s, you need to be sure to guard against buffer overflows, which you can't do with gets(). Read: Why does everyone say not to use gets()?
Some of the posted answers seems to suggest fflush() for clearing
stdin stream. Read, Why Shouldn't I use fflush(stdin)?
Just a small note, you should probably use fgets() instead of gets() since it's now deprecated due to buffer safety issues.
And it's due to the fact that scanf() will eat that last \n before it gets to read the data for the next entry.
try this instead
#include <stdio.h>
#include <string.h>
int main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1 = { 0 },book2 = { 0 }; // initialize to 0
printf("Enter details of first book\n");
printf( "name>" );
fgets(book1.name, sizeof(book1.name), stdin);
// remove \n
book1.name[strlen(book1.name)-1] = '\0';
printf( "author>");
fgets(book1.author, sizeof(book1.author), stdin);
book1.author[strlen(book1.author)-1] = '\0'; // remove \n
printf( "year copies>");
scanf("%d %d",&book1.year,&book1.copies);
fflush(stdin); // remove any garbage remaining like \n
printf("Enter details for second book\n");
printf( "name>" );
fgets(book2.name, sizeof(book2.name), stdin);
book2.name[strlen(book2.name)-1] = '\0';
printf( "author>");
fgets(book2.author, sizeof(book2.author), stdin);
book2.author[strlen(book2.author)-1] = '\0';
printf( "year copies>");
scanf("%d %d",&book2.year,&book2.copies);
printf("%s\n%s\n%d\n%d\n",
book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",
book2.name,book2.author,book2.year,book2.copies);
return 0;
}
Related
When I use the function fgets, the program skips the user input, effecting the rest of the program. An example program with this effect is:
#include <stdio.h>
int main() {
char firstDigit[2];
char secondDigit[2];
printf("Enter your first digit: ");
fgets(firstDigit, 1, stdin);
printf("\nEnter your second digit: ");
fgets(secondDigit, 1, stdin);
printf("\n\nYour first digit is %s and your second digit is %s.\n", firstDigit, secondDigit);
}
I then thought that maybe the problem was that fgets might be writing the newline, so I changed the code to account for that:
#include <stdio.h>
int main() {
char firstDigit[3];
char secondDigit[3];
printf("Enter your first digit: ");
fgets(firstDigit, 2, stdin);
printf("\nEnter your second digit: ");
fgets(secondDigit, 2, stdin);
printf("\n\nYour first digit is %c and your second digit is %c.\n", firstDigit[0], secondDigit[0]);
}
This time, the first input works properly, but the second input is skipped.
What am I doing incorrectly?
char firstDigit[2] and char secondDigit[2] are not large enough to hold a digit, a newline character, and a null-terminator:
char firstDigit[3];
char secondDigit[3];
Then, the calls to fgets() need to specify the size of the buffer arrays:
fgets(firstDigit, sizeof firstDigit, stdin);
/* ... */
fgets(secondDigit, sizeof secondDigit, stdin);
When instead fgets(firstDigit, 2, stdin); is used, fgets() stores at most two characters, including the \0 character, in firstDigit[]. This means that the \n character is still in the input stream, and this interferes with the second call to fgets().
In answer to OP's comment, How would you remove the unread characters from the input stream?, a good start would be to use more generous allocations for firstDigit[] and secondDigit[]. For example, char firstDigit[100], or even char firstDigit[1000] will be large enough that any expected input will be taken in by fgets(), leaving no characters behind in the input stream. To be more certain that the input stream is empty, a portable solution is to use the idiomatic loop:
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
Note here that it is necessary to check for EOF, since getchar() may return this value if the user signals end-of-file from the keyboard, or if stdin has been redirected, or in the unlikely event of an input error. But also note that this loop should only be used if there is at least a \n character still in the input stream. Before attempting to clear the input stream with this method, the input buffer should be checked for a newline; if it is present in the buffer, the input stream is empty and the loop should not be executed. In the code below, strchr() is used to check for the newline character. This function returns a null pointer if the sought-for character is not found in the input string.
#include <stdio.h>
#include <string.h> // for strchr()
int main(void)
{
char firstDigit[3]; // more generous allocations would also be good
char secondDigit[3]; // e.g., char firstDigit[1000];
printf("Enter your first digit: ");
fgets(firstDigit, sizeof firstDigit, stdin);
/* Clear input stream if not empty */
if (strchr(firstDigit, '\n') == NULL) {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
putchar('\n');
printf("Enter your second digit: ");
fgets(secondDigit, sizeof secondDigit, stdin);
/* Clear input stream if not empty */
if (strchr(secondDigit, '\n') == NULL) {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
puts("\n");
printf("Your first digit is %c and your second digit is %c.\n",
firstDigit[0], secondDigit[0]);
return 0;
}
It may be even better to use a single buffer[] to store lines of input, and then to store individual characters in chars. You could also write a function to clear the input stream, instead of rewriting the same loop each time it is needed:
#include <stdio.h>
#include <string.h> // for strchr()
void clear_stdin(void);
int main(void)
{
char buffer[1000];
char firstDigit;
char secondDigit;
printf("Enter your first digit: ");
fgets(buffer, sizeof buffer, stdin);
firstDigit = buffer[0];
/* Clear input stream if not empty */
if (strchr(buffer, '\n') == NULL) {
clear_stdin();
}
putchar('\n');
printf("Enter your second digit: ");
fgets(buffer, sizeof buffer, stdin);
secondDigit = buffer[0];
/* Clear input stream if not empty */
if (strchr(buffer, '\n') == NULL) {
clear_stdin();
}
puts("\n");
printf("Your first digit is %c and your second digit is %c.\n",
firstDigit, secondDigit);
return 0;
}
void clear_stdin(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
For the first case, fgets(firstDigit, 1, stdin); cannot read anything from the input because the buffer has a size of only 1 byte, and fgets() must store a null terminator into the destination.
For the second case: fgets(firstDigit, 2, stdin); reads 1 byte from stdin, the digit that you typed, and cannot read the newline because the destination array is already full, allowing for the null terminator. The second fgets() reads the pending newline from the first entry and returns immediately for the same reason, not letting you type the second input.
You must allow fgets() to read at least 2 bytes by providing a buffer size of at least 3:
#include <stdio.h>
int main(void) {
char firstDigit[3];
char secondDigit[3];
printf("Enter your first digit: ");
if (!fgets(firstDigit, sizeof firstDigit, stdin))
return 1;
printf("\nEnter your second digit: ");
if (!fgets(secondDigit, sizeof secondDigit, stdin))
return 1;
printf("\n\nYour first digit is %s and your second digit is %s.\n",
firstDigit, secondDigit);
return 0;
}
Note that if you type more than a single character before the enter key, the program will still behave in an unexpected way.
This is a buffer problem. When you press enter, don't know why it is saved in the stdin buffer.
After you perform an fgets(...) you must type fflush(stdin); on all circumstances.
Something like this:
printf("Enter your first digit: ");
fgets(firstDigit, 1, stdin);
fflush(stdin);
I've made a user-defined function for reading input and replacing newline character '\n' with '\0' so when I use printf statement for printing the string it won't add newline at the end.
char xgets(char *line, int size, FILE *stdn)
{
//READS THE LINE
fgets(line, size, stdn);
//REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0'
line[strcspn(line, "\n")] = '\0';
return line;
}
When I call xgets inside main() function it works properly, but when it is called in other user-defined function it does not wait for user-input.
I'm using Visual Studio 2015 for debugging my code.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
//USER-DEFINED FUNCTION
char xgets(char *line, int size, FILE *stdn);
void sortm_hgrade();
void sortm_rcharge();
void header(void);
void header(void)
{
printf("*-*-*-*-*HOTEL_INFO*-*-*-*-*");
printf("\n\n");
}
char xgets(char *line, int size, FILE *stdn)
{
//READS THE LINE
fgets(line, size, stdn);
//REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' END LINE CHARACTER
line[strcspn(line, "\n")] = '\0';
return line;
}
#define MAX 1000
//PROGRAMS STARTS HERE
int main(void)
{
//VARIABLE-DECLARATION
int i = 0, j = 0, n = 0;
char line[MAX] = { 0 };
char o = { 0 };
char h[10] = { 0 };
//FUCNTION CALL-OUT
header();
printf("Type anything : ");
xgets(h, sizeof(h), stdin);
printf("Enter one option from the following : \n\n");
printf("(a) To Print out Hotels of a given Grade in order of charges. \n");
printf("(b) To Print out Hotels with Room Charges less than a given Value. \n");
printf("Please type a proper option. \n");
while (n == 0){
scanf_s(" %c", &o);
switch (o){
case 'a':
sortm_hgrade();
n = 1;
break;
case 'b':
sortm_rcharge();
n = 1;
break;
default:
printf("Option INVALID \n");
printf("Please type a proper option \n");
n = 0;
break;
}
}
//TERMINAL-PAUSE
system("pause");
}
void sortm_hgrade()
{
//FOR SORTING BY GRADE
char g[10] = { 0 };
printf("Enter the Grade : ");
xgets(g, sizeof(g), stdin);
printf("\n");
}
void sortm_rcharge()
{
printf("----");
}
You should change
scanf(" %c", &o);
to
scanf("%c ", &o);
This force scanf to consume trailing chars, like '\n'
In your code '\n' of user input for scanf %c is not consumed and it is consumed by fgets in your xgets function that exit immediately with an empty buffer.
BTW that solution can wok only if a single char is input by user.
Best code would be
char c;
while (n == 0)
{
o = getchar();
while ((c = getchar()) != EOF && c != '\n') ;
EDIT
With the second solution code is waiting, and discarding, chars until a '\n' is triggered or end of file. In your specific case (using stdin as console) EOF is not mandatory. It will be mandatory in case of input is being read from a "real file".
You need to skip the \n character after you take in a character. you can command scanf for that. fgets reads that newline character up first and then hence it terminates. use this
scanf(" %c *[^\n]", &o);
This should do the trick
I am learning file handling in C.I have this code but it is not accepting string as an input to write it to a file.Any help will be appreciated.
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main(void)
{
FILE * fp1;
fp1 = fopen("abc.txt","a+");
if(fp1==NULL)
{printf("An error occurred");
}
printf("Delete file?\n");
int a,c;
char name [20];
int flag=1;
int ch=1;
while(flag!=0)
{
printf("Enter id input \n");
scanf("%d",&a);
fprintf(fp1,"\n%d\t",a);
printf("Enter Name");
gets(name);
fputs(name, fp1);
printf("Enter No \n");
scanf("%d",&c);
fprintf(fp1,"\t%d\t",c);
printf("Write more then press 0 else 1");
scanf("%d",&ch);
if(ch==1)
{
flag=0;
}
}
fclose(fp1);
}
On running this code the code does not take an input after Enter Name and directly skips to Enter No.I want the output to be in a tabular form.
Use a getchar() after entering id because the \n of 1st scanf stays in buffer.
printf("Enter id input \n");
scanf("%d",&a);
getchar();
When you enter a number for scanf("%d",&a);, you type in a number and press the Enter key. The scanf consumes the number and leaves the newline character ('\n') in the standard input stream (stdin). When the execution of the program reaches gets(name);, gets sees the newline character and consumes it, storing it in name.
Firstly, never use gets as it is dangerous as it doesn't prevent buffer overflows. Use fgets instead:
fgets(name, sizeof(name), stdin);
Secondly, you have to get rid of the newline character. You can do this by flushing the stdin. Or you can simply scan and discard the newline character just after reading the number from scanf by changing
scanf("%d",&a);
to
scanf("%d%*c",&a);
%*c scans and discards a character.
gets() is deprecated, don't use it. you can still use scanf()...
as for the tabulation...think it through.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE* fp1;
fp1 = fopen("abc.txt", "a+");
if (fp1 == NULL) {
printf("An error occurred");
}
int a, c;
char name [20];
int flag = 1;
int ch = 1;
while (flag != 0) {
printf("Enter id input:\n");
scanf("%d", &a);
fprintf(fp1, "%d\t", a);
printf("Enter Name:\n");
scanf("%s", name);
fprintf(fp1, "%s\t", name);
printf("Enter No:\n");
scanf("%d", &c);
fprintf(fp1, "%d\n", c);
printf("Again (0) or Exit(1) ?:\n");
scanf("%d", &ch);
if (ch == 1) {
flag = 0;
}
}
fclose(fp1);
return 0;
}
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 8 years ago.
I've tried looking around and I can't seem to find where the error lies. I know it must have something to do with the way I used fgets but I can't figure out for the life of me what it is. I've read that mixing fgets and scanf can produce errors so I've even changed my second scanf to fgets and it still skips the rest of my inputs and only prints the first.
int addstudents = 1;
char name[20];
char morestudents[4];
for (students = 0; students<addstudents; students++)
{
printf("Please input student name\n");
fgets(name, 20, stdin);
printf("%s\n", name);
printf("Do you have more students to input?\n");
scanf("%s", morestudents);
if (strcmp(morestudents, "yes")==0)
{
addstudents++;
}
}
My inputs are Joe, yes, Bill, yes, John, no. All goes according to plan if I utilize scanf in lieu of the first fgets but I would like be able to use full names with spaces included. Where am I going wrong?
When the program displays Do you have more students to input? and you input yes and then hit enter on console, then \n will be stored in input stream.
You need to remove the \n from the input stream. To do that simply call getchar() function.
It will be good if you don't mix scanf and fgets. scanf has lots of problems, better use fgets.
Why does everyone say not to use scanf? What should I use instead?
Try this example:
#include <stdio.h>
#include <string.h>
int main (void)
{
int addstudents = 1;
char name[20];
char morestudents[4];
int students, c;
char *p;
for (students = 0; students<addstudents; students++)
{
printf("Please input student name\n");
fgets(name, 20, stdin);
//Remove `\n` from the name.
if ((p=strchr(name, '\n')) != NULL)
*p = '\0';
printf("%s\n", name);
printf("Do you have more students to input?\n");
scanf(" %s", morestudents);
if (strcmp(morestudents, "yes")==0)
{
addstudents++;
}
//Remove the \n from input stream
while ( (c = getchar()) != '\n' && c != EOF );
}
return 0;
}//end main
I need to read user input characters including whitespace and store them in linked list. If i use scanf("%c", &charas) it runs loop 2 times and allows to input only 1 time. If i use scanf(" %c", &charas) it does not read whitespace.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct sarasas {
char zenklas;
struct sarasas *kitas;
};
typedef struct sarasas Sarasas;
typedef Sarasas *SarasasPtr;
int tarpas(struct sarasas* sar, int index)
{
struct sarasas* dabartinis = sar;
int i;
for (i=1; i<=index; i++)
{
if (dabartinis->zenklas == 32)
return(i);
dabartinis=dabartinis->kitas;
}
}
int main()
{
int i, n, tarpo_vieta;
char charas;
SarasasPtr sar;
sar=(SarasasPtr) malloc(sizeof(Sarasas));
SarasasPtr pradzia=sar;
printf("Iveskite skaiciu n\n");
scanf("%d", &n);
printf("Veskite elementus: \n");
for(i=1;i<=n;i++)
{
printf("%d\n", i);
scanf("%c\n", &charas);
sar->zenklas=charas;
sar->kitas=(SarasasPtr) malloc(sizeof(Sarasas));
sar=sar->kitas;
}
sar->kitas=NULL;
//tarpo_vieta=tarpas(pradzia, n);
printf("%d\n", tarpo_vieta);
for (i=1;i<=n;i++)
{
printf("%c\n", pradzia->zenklas);
pradzia=pradzia->kitas;
}
}
Also i cannot use arrays.
use fgets
fgets(comment, sizeof comment, stdin);
Or
use
scanf("%[^\n]",word);
Use this one, some time our scanf and and also gets will not work properly, these happen so to read the value using different read function,
scanf("%[^\n]s",word);
this scanf will read your value upto new line means till you will not key enter this will read your input(size of string is a limit).
scanf("%[^*]",word);
this will read up to your '*' char if you pressed.
Try using getchar() to read one character instead of scanf():
charas = getchar();
First try running this:
char c;
do {
c = getchar();
putchar(c);
} while (c != '.');
While the code apparently deals with a single character per loop, what the program actually does it it takes input of any length from the user and then prints it character by character until it encounters a period.
You can use a similar loop, just instead of putchar in my code, write the code to add c to the linked list.
The problem lies with scanf("%d", &n); as although is does read in int n, it leaves the '\n' or Enter in stdin. This is scanned in with the next scanf("%c\n", &charas);.
Also a problem: scanf("%c\n", &charas); which does not return until a char, optional white-space (like \n, all which is consumed) and a non-white-space is entered. (That non-white-space is returned to stdin though)
As #miushock says, check scanf() family results.
Rather than:
scanf("%d", &n);
for(i=1;i<=n;i++) {
scanf("%c\n", &charas);
sar->zenklas=charas;
}
Do
char buf[40];
if (fgets(buf, sizeof buf, stdin) == NULL) Handle_EOForIOError();
if (sscanf(buf, "%d", &n) != 1) Handle_InputError();
for (i=1; i<=n; i++) {
if (fgets(buf, sizeof buf, stdin) == NULL) Handle_EOForIOError();
if (strlen(buf) != 2) Handle_InputError();
sar->zenklas = buf[0];
}