Part of the code is as follows but not work
printf("Enter a line of text (Enter to stop): ");
fflush(stdin);
fgets(input, 256, stdin);
if (input == '\0') {
exit(0);
}
.....
.....
i want if the user only press enter, the program will be terminated. how to do it?
thanks
Use if(*input == '\n') - if fgets reads a newline, it's stored in the buffer.
Related
i have been trying to input characters and the character should be put in the file unless users change the value of opt to 'n'.
#include<stdio.h>
void main()
{
char ch,opt='y';
FILE *fp;
fp=fopen("myfile.txt","w");
while(opt=='y' || opt =='Y')
{ scanf("%c",&ch);
fputc(ch,fp);
printf("want to enter more characters(y or n):");
scanf("%c",&opt);
}
fclose(fp);
}
So I want to give the inputs until opt value changes to 'n'.
If you are using Windows OS then you can use following code to read characters without pressing Enter. See here and here to know about reading characters without pressing Enter on Linux plateforms.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch,opt='y';
FILE *fp;
fp=fopen("myfile.txt","w");
while(opt=='y' || opt =='Y')
{
ch = getch();
fputc(ch,fp);
printf("%c\nWant to enter more characters(y or n):", ch);
opt = getch();
printf("%c\n", opt);
}
fclose(fp);
}
Note: You can modify the code if you don't need to print the last entered character. See here to know other variants of getch() function.
Let's say you type a and press Enter.
When you do that, there are two characters in the input stream: 'a' and '\n'. The first scanf reads the 'a' into ch and the second scanf reads the '\n' into opt. That is the source of your problem.
You'll have to write code to read and discard the newline character. Here's one way to do it.
while(opt=='y' || opt =='Y')
{
scanf("%c",&ch);
fputc(ch,fp);
// Assuming that no more than one characte is entered per line,
// read the discard the newline.
fgetc(stdin);
printf("want to enter more characters(y or n):");
scanf("%c", &opt);
// Read and discard the newline again.
fgetc(stdin);
}
If you want to be a bit more flexible about your input, you can use:
// Make it as large as you need it to be.
#define LINE_LENGTH 200
int main()
{
char line[LINE_LENGTH];
FILE *fp;
fp=fopen("myfile.txt","w");
// Read a line of text.
while( fgets(line, LINE_LENGTH, stdin) != NULL )
{
// Print the line to the output file
fputs(line, fp);
printf("want to enter more characters(y or n):");
// Read the entire line again.
if( fgets(line, LINE_LENGTH, stdin) != NULL )
{
// If the entire line is just "n" or "N", break from the loop.
if ( strcmp(line, "n\n") == 0 || strcmp(line, "N\n") == 0 )
{
break;
}
}
}
fclose(fp);
}
scanf("%s", input);
if (strlen(input) > 1) {
errorNet();
}
if (input[0] == '\n') {
errorNet();
}
if (input[0] == '\0') {
errorNet();
}
When I hit enter, scanf goes to the next line and continues searching for input. How can I set that if enter is hit, the function errorNet is called?
Ex. If enter/blank line is inputed, call errorNet function.
If you use fgetsyou specify the size of the string you are reading. fgets will stop reading the input when this size is reached or the user pressed \n. Notice that this size counts the \0 in the end of the string.
char input[10];
fgets(input, 10, stdin);
printf("%s\n", input);
To detect if a user just pressed \n without writing anything, just check if the first character is a \n, for example:
if (input[0] == '\n') {
printf("just '\\n'\n");
}
Im using this code to allow the use to enter a numerical value for the variable:
float day;
printf("Day?: ");
scanf("%f",&day);
how can i make the program produce an error message if the user did not enter ANY value for "Day"??
Try this:
if(scanf("%f",&day) != 1)
printf("Error");
Use fgets()/sscanf()
char buf[100];
fgets(buf, sizeof(buf), stdin);
if (sscanf(buf, "%f", &day) != 1) {
if (buf[0] == '\n')
HandleNothingInput();
else
HandleBadInput();
}
}
fgets() typically reads until a Enter or '\n' is entered. By using sscanf() to parse the buffer, empty inputs ("user did not enter ANY value"), bad input ("abc") are readily detected as the result of scanf() will be 0.
Alternatives: If you want to insure no additional data is entered, one could use a sentinel:
int ch;
if (sscanf(buf, "%f %c", &day, &ch) != 1) {
atof() is another approach.
Here's a small program:
#include <stdio.h>
int main() {
char str[21], choice[21]; int size;
while(1){
printf("$ ");
fgets(str, 20, stdin);
printf("Entered string: %s", str);
if(str[0] == 'q') {
printf("You sure? (y/n) ");
scanf("%s", choice);
if(choice[0] == 'y' || choice[0] == 'Y')
break;
}
}
return 0;
}
It reads a string using fgets(). If the string starts with a q, it confirms if the user wants to quit, and exits if the user types y.
When I run it and type q, this happens:
$ q
Entered string: q
You sure? (y/n) n
$ Entered string:
$
Note the $ Entered string:. Clearly, fgets() got an empty character or something as input, even though I didn't type anything.
What's going on?
As described in other answer scanf call leaves the newline in the input buffer you can also use getchar() after scanf like this :
scanf("%20s", choice);// always remember( & good) to include field width
// in scanf while reading
Strings otherwise it will overwrite buffer in case of large strings `
getchar(); //this will eat up the newline
Besides , you should also use fgets like this :
fgets(str,sizeof str, stdin); //Its better
It because the scanf call reads a character, but leaves the newline in the buffer. So when you next time call fgets is finds that one newline character and reads it resulting in an empty line being read.
The solution is deceptively simple: Put a space after the format in the scanf call:
scanf("%s ", choice);
/* ^ */
/* | */
/* Note space */
This will cause scanf to read and discard all training whitespace, including newlines.
Use a 'char' of a specific size char choice [1]
OR
char c[1];
c = getchar();
if(c[0] == 'y' || c[1] == 'y'){
// DO SOMETHING
}
I want to end while loop by pressing the 'Enter' key, not any alphabet key. I could not figure out how I can do that.
I have done it such that, if the input is not the digit it will exit, but I want the input to be 'Enter' key.
void main (void)
{
float f;
float total=0.0;
printf("Enter numbers to be add: ");
while (scanf("%f",&f)==1)
{
total=total+f;
printf("Enter another # to be add: ");
scanf("%1.0f",&f);
}
printf("Addition Total = %1.0f",total);
}
Try fgets... should work.
http://www.codecogs.com/reference/computing/c/stdio.h/fgets.php
using your frame work you should read in a string and then check if it is blank. If the string is blank you can end execution. If the string is not blank you should scanf the string.
Use sscanf (http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/) to read the string after you have verified that it is not blank.
You can use fgets and sscanf instead:
char buffer[100];
while (fgets(buffer, sizeof(buffer), stdin))
{
/* On DOS/Windows an empty line might be 2 characters ("\r\n") */
if (strlen(buffer) == 1)
break; /* Only newline in buffer */
if (sscanf(buffer, "%f", &f) == 1)
{
total += f;
printf("Enter another number: ");
}
}
Try to use getch
c = null
while(c != 0x0D)
{
c = getch();
}
More info