Reputation: 527
A simple C problem, I want to read integers from standard input(user input), each line of standard input contains an integer, and an empty line denotes the end of input. I tried to use gets() and scanf(), but it's not working.
while (1){
char a;
scanf("%c",&a);
printf("%c",a);
if (a=='\r'||a=='\n'){
break;
}
}
when i use scanf(), It terminates every time i press an enter;
while(1){
char a[10];
gets(a);
if (a=='\r'||a=='\n'){
break;
}
}
and when i use gets(), '\r' or '\n' can not be read into buffer, so it never break.
Could anyone help me with that, thanks in advance!
Upvotes: 0
Views: 3872
Reputation: 32596
I want to read integers from standard input
Doing
while (1){ char a; scanf("%c",&a); printf("%c",a); if (a=='\r'||a=='\n'){ break; } }
you read a line containing any characters, not only a number representation
Doing
while(1){ char a[10]; gets(a); if (a=='\r'||a=='\n'){ break; } }
a is a char*
so in a=='\r'||a=='\n'
you wrongly compare a pointer with characters, you want *a=='\r'||*a=='\n'
Additional remarks :
fgets(a, sizeof(a), stdin)
I want to read integers from standard input(user input), each line of standard input contains an integer, and an empty line denotes the end of input.
A proposal, a line containing only space and tabs before the end of line is considered empty, and the spaces and tabs before/after the number are bypassed without being considered invalid :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
int main()
{
while(1) {
puts("enter number");
char a[10];
if (fgets(a, sizeof(a), stdin) == NULL) {
puts("EOF");
break;
}
errno = 0;
char * endptr;
long n = strtol(a, &endptr, 10);
if (errno != 0) {
puts("not a valid long");
}
else {
/* check possible unexpected characters */
char * p = endptr;
for (;;) {
if (*p == 0) {
if (endptr == a) {
puts("done");
return 0;
}
printf("the number is %ld\n", n);
break;
}
if (!isspace(*p++)) {
puts("not (only) a valid long");
break;
}
}
}
}
}
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra n.c
pi@raspberrypi:/tmp $ ./a.out
enter number
123
the number is 123
enter number
123
the number is 123
enter number
12a
not (only) a valid long
enter number
a12
not (only) a valid long
enter number
a
not (only) a valid long
enter number
done
Upvotes: 2
Reputation: 34583
Is this an XY problem? You can use fgets
and sscanf
to do the job.
#include <stdio.h>
int main(void) {
char buffer[100];
int number;
while(fgets(buffer, sizeof buffer, stdin) != NULL) {
if(sscanf(buffer, "%d", &number) != 1) { // or strtol perhaps
break;
}
printf("Number: %d\n", number);
}
return 0;
}
Upvotes: 4
Reputation: 1
I think using getline might be a more general solution, here is some example code that you could modify for your purposes, "buf" would contain the input.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *buf = NULL;
int size;
unsigned int length;
size = getline(&buf, &length, stdin);
if (size != -1)
/* do anything you want with the stuff that was entered here */
/* for the example I just write it back to stdout! */
puts(buf);
else
/* this would be your "end" condition */
printf("Nothing read!\n");
printf("Size: %d\n Length: %d\n", size, length);
free(buf);
return 0;
}
Upvotes: 0