Reputation: 1570
I'm trying to read a constant length string as an array of c for 8 times. Each time i override the content of the previously read array.
The code seems to be working at the first loop cycle, but then as you can see below, I get some weird output. What am I missing?
CODE:
#include <stdio.h>
#define MAX_READ_CYCLES 8
#define MAX_STRING_LENGTH 4
int main(){
int i, cycles;
char a[MAX_STRING_LENGTH+1];
/***************************************************
* BEGIN Read logic
***************************************************/
for(cycles=0; cycles < MAX_READ_CYCLES; cycles++){
i=0;
printf("\nEnter a string: ");
while(i < MAX_STRING_LENGTH){
a[i] = getc(stdin);
i++;
}
a[i] = '\0'; //string end character
fflush(stdin); //cleaning the buffer
printf("String you entered: %s\n", a);
}
/***************************************************
* END
***************************************************/
return 0;
}
OUTPUT:
Enter a string: cccc
String you entered: cccc
Enter a string: cccc
String you entered:
ccc
Enter a string: String you entered:
Enter a string:
Upvotes: 0
Views: 5618
Reputation: 17668
Consider a few issues:
a[i] = getc(stdin);
would take the previous Enter as an input, and so you'd loose one character.
You can try scanf( " %c", &a[i] );
instead, which would eliminate the buffered Enter character, and also the need for fflush()
function.
In your case, it might not be necessary, but usually I'd reset the character array before each read iteration using memset
:
memset( a, 0, sizeof( a ) );
// this requires <string.h>
Enter a string: cccc
String you entered: cccc
Enter a string: cccc
String you entered:
ccc # one `c` is missing because the input buffer looks like this `<Enter>cccc`, in which `<Enter>` is left over from previous input.
Enter a string:
String you entered: c # display `c` because input buffer contains `c<Enter><Enter><Enter>`, in which `c<Enter>` is left over from previous input.
Upvotes: 1
Reputation: 141586
fflush(stdin);
causes undefined behaviour. Replace that line with:
int ch;
while ( (ch = getchar()) != '\n' && ch != EOF ) {}
Your code has another logical issue. It always reads 4
characters , even if the person presses Enter first. So if someone types hi
and presses Enter then it keeps waiting until the next Enter press.
You may want to modify the while
loop to also break if the key '\n'
was just entered. In this scenario you would NOT go on to clean the input as described above.
Upvotes: 3
Reputation: 1427
Your code segment reads in exactly four characters including newlines. Use the scanf()
library function to input string characters into a
#include <stdio.h>
#define MAX_READ_CYCLES 8
#define MAX_STRING_LENGTH 4
int main(){
int i, cycles;
char a[MAX_STRING_LENGTH+1];
/***************************************************
* BEGIN Read logic
***************************************************/
for(cycles=0; cycles < MAX_READ_CYCLES; cycles++){
i=0;
printf("\nEnter a string: ");
scanf("%s", a);
fflush(stdin); //cleaning the buffer
printf("String you entered: %s\n", a);
}
/***************************************************
* END
***************************************************/
return 0;
}
Upvotes: 0