Reading character input with scanf

  • Thread starter Thread starter TranscendArcu
  • Start date Start date
  • Tags Tags
    Input Reading
Click For Summary
The discussion revolves around a coding issue where a program incorrectly interprets user input for sorting preference as 'a' instead of 'd' when 'd' is entered. The variable `pref` is of type char and initially unassigned, which may lead to unexpected behavior if not properly handled. The `flushInputBuffer` function is intended to clear any leftover input, but there is confusion regarding its effectiveness, especially when the return key is pressed without entering a character. Despite attempts to troubleshoot, the user finds that they need to press Enter twice to confirm an input of 'a' when they intended to enter 'd'. The problem may stem from how the input buffer is managed and the sequence of function calls in the program.
TranscendArcu
Messages
277
Reaction score
0
Code:
printf("\nSpecify ascending ['a'] or descending 'd' >> ");scanf("%c",&pref);flushInputBuffer();
	extract_array(s,n,crit,array1);extract_array(s,n,2,array2);
	if (pref == '\n')pref = 'a';
	printf("\n%c\n",pref);
So the above is a snippet of code from one of the programs I am writing. My problem is that, even if I enter 'd' for my input, the program seems to decide ALWAYS that I mean to enter 'a'. I only want the computer to interpret my input as an 'a' if I just press the return key when prompted for input. Can anyone help me see the problem here?
 
Physics news on Phys.org
It depends on what comes before.

What is the type of pref?
What is the initial value of pref?
What is the last thing you've read from input successfully?
Is there still input lying around in the input buffer (a new line perhaps)?
 
I like Serena said:
It depends on what comes before.

What is the type of pref?
What is the initial value of pref?
What is the last thing you've read from input successfully?
Is there still input lying around in the input buffer (a new line perhaps)?

pref is type char, which matches with the %c. Pref is initially unassigned I just have
Code:
char pref;
The last successfully read character could have been almost anything, since the output of the program, a piece of which is above, has multiple functions depending on the last read character. However, there should not be any input lying around in the buffer -- the flushInputBuffer function should ensure that. In particular, I have
Code:
void flushInputBuffer()
{
	char c;
	do {
		scanf("%c",&c);
	} while (c != '\n');
}
Since I cleared the buffer at the last character scan, too, I'm not sure what the problem is.
 
So what happens if you copy and paste these couple of lines to the beginning of your main() function?
You should see that if you type 'd' followed by Enter, that your programs reads 'd', after which the new line is flushed.

Just for fun, copy those lines a second time after the first, and see if you can type 'd' successfully again...
 
EDIT: Let me try adding the conditional if statement. I'll let you know if it changes something.
Code:
#include <stdio.h>
void flushInputBuffer();

int main(void)
{
	char pref;
	int j;
	for (j=0;j<10;j++)
	{
		printf("\nSpecify ascending ['a'] or descending 'd' >> ");
		scanf("%c",&pref);flushInputBuffer();
		printf("\nYou entered character: %c\n",pref);
	}
	return (0);
}void flushInputBuffer()
{
	char c;
	do {
		scanf("%c",&c);
	} while (c != '\n');
}

Output:
Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

So it seems to be working, which is why I am confused.
 
So as it turns out, it still works with the if (pref == '\n')pref = 'a';

However, I have to press the return key twice in order to get it to tell me that I secretly entered the letter 'a'. It is not immediate.
Output:Specify ascending ['a'] or descending 'd' >> You entered character: a

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> d

You entered character: d

Specify ascending ['a'] or descending 'd' >> a

You entered character: a

Specify ascending ['a'] or descending 'd' >> You entered character: a

Specify ascending ['a'] or descending 'd' >> You entered character: a

Specify ascending ['a'] or descending 'd' >>
 
Here's the code again:

Code:
/*
 * test.c
 *
 *  Created on: 25. apr. 2012
 *      Author: James
 */

#include <stdio.h>
void flushInputBuffer();

int main(void)
{
	char pref;
	int j;
	for (j=0;j<10;j++)
	{
		printf("\nSpecify ascending ['a'] or descending 'd' >> ");
		scanf("%c",&pref);flushInputBuffer();
		if (pref == '\n')pref = 'a';
		printf("\nYou entered character: %c\n",pref);
	}
	return (0);
}void flushInputBuffer()
{
	char c;
	do {
		scanf("%c",&c);
	} while (c != '\n');
}
 
So either you have old input floating around, or your program does decide to pick choice 'd', but you're mistaken in your conclusion that it picked 'a'.

You did say that your program "seemed" to always pick 'a'...

TranscendArcu said:
So as it turns out, it still works with the if (pref == '\n')pref = 'a';

However, I have to press the return key twice in order to get it to tell me that I secretly entered the letter 'a'. It is not immediate.

I do not understand what you mean.
Where and how do you need to press Enter twice?

Btw, does your extract_array() function do anything that may interfere?
 
Oh, if you press Enter instead of a letter, your flushInputBuffer() function will force you to press Enter a second time.
 
  • #10
I like Serena said:
So either you have old input floating around, or your program does decide to pick choice 'd', but you're mistaken in your conclusion that it picked 'a'.

You did say that your program "seemed" to always pick 'a'...



I do not understand what you mean.
Where and how do you need to press Enter twice?

Btw, does your extract_array() function do anything that may interfere?

This is what the extract_array function is:
Code:
void extract_array(student_t s[], int n, int crit, aux_t array[])
{
	int j;
	char temp[STRSZ];

	for (j=0; j<n; j++) {
		switch (crit) {
			case 1: strcpy(temp, s[j].first);
				break;
			case 2: strcpy(temp, s[j].last);
				break;
			case 3: strcpy(temp, s[j].food);
		}
		strcpy(array[j].string, temp);
	}
}
I don't think this should be interfering.

I am confident that it is picking 'a' because I asked it to print out what it thought pref was after it had entered the if statement. It always returned 'a', even when I had entered 'd' as my input.
 
  • #11
Here's some sample output to show the point (just to specify, I changed the prompt output slightly, which is why it's not the same as before; the user input part is still all the same though): Choose to sort in ascending ['a'] or descending 'd' order >> d

a
Where do you want to print file?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K