I'm new to c programming and I'm facing this problem with my program
I have a loop that gets a char form the input buffer
while(c = getchar()){
if(c == '\n') break;
if(c == '1') Add();
if(c == '2') getInput(); // this is where the headache starts
....
}
here is the getInput() function
void getInput()
{
char ch = getchar();
if(ch == '1') doSomething();
....
}
but when calling getchar() from the getInput() function it only gets characters that were left in the input buffer from the last call of getchar(). and what i want it to do is to get newly typed characters.
I've been googling for two hours for a decent way to clear the input buffer but nothing helped. So a link to a tutorial or an article or something is very appreciated and if there's another way to implement this then please tell me.
First of all there will be == comparison operator rather than = assignment operator in the if condition in this code.
while(c = getchar()){
if(c = '\n') break;
if(c = '1') Add();
if(c = '2') getInput(); // this is where the headache starts
....
}
And for stop taking input try EOF which from keyboard can be given by prssing CTRL+D.
EDIT : The problem is with the \n which is actually taken as input when you press ENTER key on the key board. So change just one line of code.
if (c ==\n) break; to if (c == EOF ) break; and as I said EOF is the end of input.
Then your code will work fine.
Flow of code :
step 1: suppose `2` is input
step 2: getInput() is called
step 3: suppose `1` as input // in getInput
step 4: doSomething() is called // from getInput
step 5: After completion of doSomething again come back to while loop ,
but in your case you have already given `\n` character as an input
when you pressed `1` and `ENTER`.And thus loop terminates.
but after changing the code as I said , this should work.
NOTE: To understand code flow and for debugging purposes it's best practice to put printf() in various places in functions and see the output as which lines are executing and which are not.
This should work: (Example of clearing input buffer)
#include <stdio.h>
int main(void)
{
int ch;
char buf[BUFSIZ];
puts("Flushing input");
while ((ch = getchar()) != '\n' && ch != EOF);
printf ("Enter some text: ");
if (fgets(buf, sizeof(buf), stdin))
{
printf ("You entered: %s", buf);
}
return 0;
}
/*
* Program output:
*
Flushing input
blah blah blah blah
Enter some text: hello there
You entered: hello there
*
*/
Related
I want to break this loop when the user press enters twice. Meaning, if the user does not enter a character the second time, but only presses enter again, the loop must break.
char ch;
while(1) {
scanf("%c",&ch);
if(ch=='') { // I don't know what needs to be in this condition
break;
}
}
It is not possible to detect keypresses directly in C, as the standard I/O functions are meant for use in a terminal, instead of responding to the keyboard directly. Instead, you may use a library such as ncurses.
However, sticking to plain C, we can detect newline characters. If we keep track of the last two read characters, we can achieve similar behavior which may be good enough for your use-case:
#include <stdio.h>
int main(void)
{
int currentChar;
int previousChar = '\0';
while ((currentChar = getchar()) != EOF)
{
if (previousChar == '\n' && currentChar == '\n')
{
printf("Two newlines. Exit.\n");
break;
}
if (currentChar != '\n')
printf("Current char: %c\n", currentChar);
previousChar = currentChar;
}
}
Edit: It appears that the goal is not so much to detect two enters, but to have the user:
enter a value followed by a return, or
enter return without entering a value, after which the program should exit.
A more general solution, which can also e.g. read integers, can be constructed as follows:
#include <stdio.h>
#define BUFFER_SIZE 64U
int main(void)
{
char lineBuffer[BUFFER_SIZE];
while (fgets(lineBuffer, BUFFER_SIZE, stdin) != NULL)
{
if (lineBuffer[0] == '\n')
{
printf("Exit.\n");
break;
}
int n;
if (sscanf(lineBuffer, "%d", &n) == 1)
printf("Read integer: %d\n", n);
else
printf("Did not read an integer\n");
}
}
Note that there is now a maximum line length. This is OK for reading a single integer, but may not work for parsing longer input.
Credits: chux - Reinstate Monica for suggesting the use of int types and checking for EOF in the first code snippet.
You can store the previous character and compare it with the current character and enter, like this:
char ch = 'a', prevch = '\n';
while(1){
scanf("%c",&ch);
if((ch=='\n') && (ch == prevch)){// don't know what needs to be in this condition
break;
}
prevch = c;
}
Note that the previous character by default is enter, because we want the program to stop if the user hits enter at the very start as well.
Working like charm now
char ch[10];
while(1){
fgets(ch, sizeof ch, stdin);
if(ch[0]=='\n'){
break;
}
}
I have these two blocks of code, the aim of both of which is to get some user inputs, to make a cryptography script.
int keyPrompt(){
/* this prompts the user for the Caesar encryption key */
int c;
printf("Enter key value between 1-9: ");
while((c=getchar()) != EOF && c != '\n'){
return c;
}
}
int cryptPrompt(){
/* this asks the user whether they want to encrypt or decrypt */
int d;
printf("Do you want to encrypt or decrypt?(E/D): ");
while((d=getchar()) != EOF && d != '\n'){
/*
if(d == 'E'){
return 1;
}
else if (d == 'D' ){
return -1;
}
*/
return d;
}
}
The problem I'm having is that when I run the file, the first while loop behaves as I expect it to: I enter a value, hit enter and it goes to the next line. With the second while loop, when it executes the function, it skips asking the user for an input and simply goes straight to the next line of good, not storing any return value.
Any idea why it's doing this?
Consider what this code does:
int keyPrompt(){
/* this prompts the user for the Caesar encryption key */
int c;
printf("Enter key value between 1-9: ");
while((c=getchar()) != EOF && c != '\n'){
return c;
}
}
After printing the prompt, it reads an input character. If that character is an EOF or a newline, it exits the loop and falls off the end of the function (without a return, which leads to undefined behavior if the returned value is not ignored). If its NOT an EOF or newline, it returns the character.
In no case will the while loop ever actually loop.
Consider what happens if you call this code and enter a key+newline. The key will be read and returned, and the newline will be left in the input buffer. Now consider what happens if you call another function like this -- the first thing it will read is the newline left over from the keyPrompt function...
Suppose I have the following which allows the entering in of text:
printf("Enter in your text\n");
while ((c=getchar()) != EOF) {
putchar(c);
if (last_char) // the last character before the user presses enter.
do_something();
}
Is there a way for me to detect if the character is the last one in the text input (without getting out of the loop)? Or is that not possible the current organization of things above?
Something list this?
printf("Enter in your text\n");
int c,last_char;
do
{
c = getchar();
if (c == '\n')
{
// it's the last char... do something with last_char
printf("\nThe last char was: %c\n", last_char);
}
last_char = c;
} while (c != EOF);
How about-
int c = 0;
while((c = getchar()) != EOF)
{
putchar(c);
// assuming that ';' is the character. you can change
// this to any acceptable character here
if(c == ';')
{
printf("\nYou entered ';' character\n");
// do something here
}
}
Is there a way for me to detect if the character is the last one in the text input (without getting out of the loop)?
No.
stdin is best thought of as a stream of data. Code cannot know beforehand if end-of-file or enter ('\n') will arrive
How to detect last character in a C string when within a getchar() loop
Instead detect when end-of-file occurs and operate on the previous character after the loop.
printf("Enter in your text\n");
int previous = EOF;
int c;
while ((c=getchar()) != EOF) {
previous = c;
}
do_something(previous);
If one needs the character before enter, while ((c=getchar()) != '\n' && c != EOF) { would suffice.
I have a problem with the input for a postfixed notation calculator in C.
My teacher asked me to use scanf("%s",token) to get the input. To stop reading tokens, I check if the value returned by scanf is EOF. It works if I use input redirection when testing, but if I write the expression on the Windows cmd I fall into an endless loop. How can I stop scanf when I simply press enter without entering a string?
Here's the code:
#include <stdio.h>
#include <ctype.h>
#include "stack.h"
int main(){
int a,b,t,stop;
char token[10],c;
do{
stop = scanf("%s",token);
if (stop == EOF){
break;
}
if (isdigit(token[0])){
t = atoi(token);
push(t);
}else{
a = top();
pop();
b = top();
pop();
c = token[0];
switch(c){
case '+': t = a + b;
break;
case '-': t = a - b;
break;
case '*': t = a * b;
break;
case '/': t = a / b;
break;
}
push(t);
}
} while(1);
printf("Result: %d\n",top());
}
The problem is in the if block after the stop variable inizialisation, I think. Sorry for my English, I'm an Italian student, I tried to be as neat as I can.
"How can I stop scanf when I simply press enter without entering a string?"
Using scanf() to read '\n' (Enter) is challenging. fgets() or fgetc() is a better approach. But the following shows how to nearly do it with scanf().
The %s in scanf("%s",token); first consumes leading white-space including '\n'. Code must first detect any potential '\n' before calling scanf("%s",token);.
char ws[2];
// Consume white-space, but not \n
scanf("%*[ \f\r\t\v]"); // Depending on locale, this may not be a complete list
if (1 == scanf("%1[\n]", ws) {
break;
}
stop = scanf("%s",token);
if (stop == EOF){
break;
}
A cleaner way to do this.
int ch;
while (isspace(ch = fgetc(stdin)) && ch != '\n');
if (ch == '\n') {
break;
}
ungetc(ch, stdin);
stop = scanf("%s",token);
if (stop == EOF){
break;
}
Only one more question: to stop the program, I had to type ctrl+z
twice. Can you tell me why?
Usually the C standard input in line buffered text mode is implemented by calling a system specific function which reads a line and returns the number of characters in it (including the newline character); when the number 0 is returned, this is considered EOF. Now, when you enter "3 5 +(crtl+z)(ctrl+z)", the first Ctrl-Z ends the input line and causes the reading function to return 5, but this does not indicate EOF to the standard input functions; only after the second Ctrl-Z ist pressed without intervening input, 0 is returned and recognized as EOF.
I'm Writing a program for Billing System. I'm using do-while loop in my program. And the program is executed according to user input. If the user want to continue the execution, the program will be continue. But I Got a prob in Execution. I was trying my logic in simple do-while loop. The same Problem arises for simple do-while loop also.
Problem is: If the input is yes, the program does not get the further input from user.
That simple do-while loop is:
#include <stdio.h>
main()
{
int c;
char ch;
do
{
printf("enter the no less then 4:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("In 1\n");
break;
case 2:
printf("In 2\n");
break;
case 3:
printf("In 3\n");
break;
}
printf("do u want to continue?:");
ch=getchar();
}while(ch=='y');
}
If i put while(ch != 'n') instead of while(ch=='y') the program working fine. I couldn't understand the problem behind this. Please Help me to rectify this. And Explain about this problem.Thank u in advance.
first run, 3 is printed, user types "y" and presses return
getchar() reads 'y' and program loops
second time, getchar() reads newline character from the previous key press
newline is not 'y' so program does not loop
Several problems:
getchar returns an int, not a char, so ch must be an int just like c.
scanf needs a pointer to go with the %d, so it should be scanf("%d", &c);
The while should rather test for EOF, as in while ((ch = getchar()) != EOF)
Note that the input will contain the newlines, which you should deal with (e.g. ignore).
This should be quite robust:
#include <stdio.h>
int main(void)
{
int c, ch;
for (;;) {
printf ("Enter a number (1, 2 or 3):");
fflush (stdout);
if (scanf ("%d", &c) == 1) {
switch (c) {
case 1:
printf ("In 1\n");
break;
case 2:
printf ("In 2\n");
break;
case 3:
printf ("In 3\n");
break;
}
printf ("Do you want to continue? [y/n]:");
fflush (stdout);
while ((ch = getchar ())) {
if (ch == 'y')
break;
else if (ch == 'n' || ch == EOF)
return 0;
}
} else {
printf ("That was not a number. Exiting.\n");
return 0;
}
}
}
While(ch=='y') or the character whatever in while() it will sent to case 3 as per your coding....y is pressing ,it wil sent to case 3 otherwise it wont work
Instead of reading the answer using getchar, use fgets.
As others explained, the second getchar call gives you the newline, which was typed after the first y.
With fgets, you'll get everything the user typed. Then you can check if it's y (just check the first character, or use strcmp).