Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

error in C programming language : gets() after scanf()

Status
Not open for further replies.

ghasem_008

Full Member level 4
Joined
Feb 9, 2012
Messages
221
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Activity points
2,990
error in c language : gets() after scanf()

hi.
As you know,there is a problem in c programming language,when gets() comes after scanf().
in this case, first gets() after scanf() doesnt work.in fact the first gets() see Enter ("\r") and skip.

i used from an extra gets() to ignore this prblem.
for example:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main()
{
    int n;
    printf("enter an integer number:");
    scanf("%d" , & n);
    char s2[]={'\0'};
    gets(s2);
    char s[10];
    printf("enter a string:");
    gets(s);
    return 0;
}



in the above, I used from [char s2[]={'\0'}; gets(s2);] to ignore scanf effect on gets().

is there any better solution (with basic commands only.I dont want to use from librarian functions)?
 

Re: error in c language : gets() after scanf()

This question was asked on stackexchange and it appears there are a couple of possible work arounds.

The option that looks like it worked for the OP in that thread was...
Code:
...
scanf("%d", &a);
getc(stdin);
...
This would be in place of the gets lines.


Note: I'm not a C programmer so I have no clue if this is the "right" way to do this.
 

Re: error in c language : gets() after scanf()

is there any better solution (with basic commands only.I dont want to use from librarian functions)?

In C/C++ I never liked to use APIs for reading streamming inputs, like strings.
At this case, I preferred manage a getc(), allocating each byte one-by-one at the variable.

In addition, for writing strings I preferred the sprintf(), checking the returned value.
 

Re: error in c language : gets() after scanf()

My compiler manual suggests
Code:
char s[10];
gets(s);
sscanf(s, "%d" , & n);

At this case, I preferred manage a getc(), allocating each byte one-by-one at the variable
.
When reading from stdin stream (e.g. in a WIndows terminal window), getc() would usually not return until a character is available.
 

Re: error in c language : gets() after scanf()


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main()
{
    int n;
    printf("enter an integer number:");
    scanf("%d" , & n);
    printf("enter a string:");
    char s[10];
    scanf("%s",s);
    return 0;
}


You could (and should) check the scanf return values and you may get memory overflow errors (no char string size limits during reading).
 

Re: error in c language : gets() after scanf()

thank you very much!
I'm agree with andre_teprom. gets() is not a very good function.

"Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. "
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top