If I only want the user to enter one char, how would I go about doing that in the c language. My attempt at this is below but it failed horribly. From what i read online I heard you could use the function gets or fgets to accomplish this, but I could not figure out how.
do
{
geussNumber += 1;
printf("Enter guess number %d\n", geussNumber);
scanf(" %c", &geussLetter);
scanf ("%c", &inputViolation);
if (isalpha(geussLetter) == 0)
{
printf("You did not enter a letter\n");
}
else if (inputViolation == true)
{
printf("You eneterd more than one letter\n");
}
else
{
inputLoopEnd = 1;
}
}
while( inputLoopEnd == false );
You could use the getc family of functions.
Have a look at http://quiz.geeksforgeeks.org/difference-getchar-getch-getc-getche/ for example.
It seems you want to read input one line at a time (i.e. the user types a one-letter guess and then <enter>), and you want to verify that the guess indeed contains only a single letter. Couching the problem in those terms perhaps makes it clearer how fgets() could be applied, as that function's purpose is to read one line at a time. Having read a whole line -- or at least as much as the buffer can accommodate -- you can validate it and extract the guess.
scanf() is hard to use properly, so I do recommend the fgets() approach. If you insist on using scanf(), however, then you might do it like this:
// consumes leading whitespace, inputs one character, then consumes any
// immediately-following run of spaces and tabs:
int result = scanf(" %c%*[ \t]", &guessLetter);
if (result == EOF) {
// handle end-of-file ...
} else {
assert(result == 1); // optional; requires assert.h
int nextChar = getchar();
if ((nextChar == '\n') || (nextChar == EOF)) {
// handle multiple-character guess ...
} else if (!isalpha(guessLetter)) {
// handle non-alphabetic guess ...
} else {
// it's valid ...
}
}
Do not use things like fgets() or fputs() etc... They are falling out of use.
As you can see from the description here... this function is designed to handle objects of type str, and you are more focused on using chars at the moment so why not just handle only chars to make life easier.
You can't do this the way you think you can...
scanf(" %c", &geussLetter);
scanf ("%c", &inputViolation);
This can't work because even if the user enters in only one char the way they are supposed to, it's still going to trigger your inputViolation scheme.
Edit: 12:14pm 7/20/2016
I really like the elegance of MOHAMAD's solution on the community wiki.
So I edited to fit your situation and it works well here too. Same idea...
#include <stdio.h>
#include <stdlib.h>
int clean_stdin()
{
while (getchar() != '\n');
return 1;
}
int main(void)
{
int first_time_around = 0;
char theguess = 0;
char c;
do
{
if (first_time_around == 0)
first_time_around++;
else
printf("Wrong input \n");
printf("Enter guess number: \n");
} while (((scanf("%c%c", &theguess, &c) != 2 || c != '\n')
&& clean_stdin()) || !isalpha(theguess));
return 0;
}
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;
}
}
If the user inputs "abc" as the input, the Do-While Loop loops around 3 times, one time for each letter. However, I want it to loop only once.
int main(void) {
do {
printf("Enter how much money you can contribute: ");
numArgsRead = scanf(" %lf", &availableFundsToContribute);
scanf("%c", &finalValOnLine);
} while (!((availableFundsToContribute > 0) && (numArgsRead == 1) && (finalValOnLine == '\n')));
return 0;
}
Here's what my output looks like:
Enter how much money you can contribute: abc
Enter how much money you can contribute: Enter how much money you can contribute: Enter how much money you can contribute:
Here's what I want it to look like:
Enter how much money you can contribute: abc
Enter how much money you can contribute:
There are two main options — continuing with scanf() and switching to fgets() and sscanf().
Continuing with scanf()
As noted in comments, you need to track when scanf() fails to convert the input without encountering EOF, and deal with the extraneous input. The normal way to deal with it is to read up to and including the end of line.
static inline int gobble_to_eol(void)
{
int c;
while ((c = getchar()) != EOF && c != '\n')
;
return c;
}
int main(void)
{
int numArgsRead;
double availableFundsToContribute = 0.0;
char finalValueOnLine = '\0';
do
{
printf("Enter how much money you can contribute: ");
if ((numArgsRead = scanf("%lf", &availableFundsToContribute)) == EOF)
break;
if (numArgsRead == 0)
{
if (gobble_to_eol() == EOF)
break;
}
else
{
if (scanf("%c", &finalValOnLine) == EOF)
break;
if (c != '\n')
{
/* Input might be 3.14abc */
if (gobble_to_eol() == EOF)
break;
}
} while (!(numArgsRead == 1 && availableFundsToContribute > 0.0 && finalValOnLine == '\n'));
return 0;
}
Point of detail: all formats except three (%c, %[…] (scan sets), and %n) skip leading white space, including newlines, automatically. There is no need include the blank in "%lf" (but there is also no harm in doing so at the start, but putting a blank at the end is a UI disaster).
Switching to fgets() and sscanf()
An alternative approach reads whole lines and then scans them with sscanf(). This was mentioned by user3386109 in a comment, but it is standard advice and often the best way to deal with the input.
The code might look like this:
int main(void)
{
char buffer[4096]; // Make it bigger if you prefer!
double availableFundsToContribute = 0.0;
char finalValOnLine;
do
{
printf("Enter how much money you can contribute: ");
if (fgets(buffer, sizeof(buffer), stdin) == NULL)
break;
if ((numArgsRead = sscanf(buffer, "%lf%c", &availableFundsToContribute, &finalValOnLine)) != 2)
{
buffer[strcspn(buffer, "\n")] = '\0'; // Zap newline
fprintf(stderr, "Unrecognized input [%s] - try again.\n", buffer);
}
} while (!(numArgsRead == 2 && availableFundsToContribute > 0 && finalValOnLine == '\n'));
return 0;
}
In both cases, I would probably use a top-controlled loop (for or while) but I'm keeping close to the original code. There probably should be error reporting associated with the call to gobble_to_eol() in the first code, so that the user knows what the program thinks is wrong. Note that using scanf() directly makes it hard to report on what caused the problem; reading a line and then parsing it means you can report the erroneous input accurately. Note, too, that using fgets() means that empty lines will be reported as an error, rather than silently ignored waiting for new input.
Warning: no compiler was consulted about the accuracy of the code. Treat with due caution.
I am trying to read a numeric only value that will then execute a specific function, depending on its value using the switch statement.
I know I could just use while(option < 0 || option >3 || option != 99), but is there a workaround?
Personal work:
do
{
printf("Please choose an option :\n0. Create Database\n1.Add New Student \n2. Show All Students \n3.Delete Student\n99.Exit\n");
scanf("%d", &option);
} while (!isdigit(option));
Which does not work for some reason.
Your major problem there is that, if scanf fails to read any digits for some reason (such as if you enter x), it will return zero (the number of items successfully scanned), not populate option, and (this is the killer) leave the input pointer in the same place as before you started.
That unfortunately means, when you go back to get another number, it will just attempt reread the problematic data and probably end up in an infinite loop without allowing more input.
The scanf family is meant for formatted data and there's precious little that's more unformatted than user input :-)
Your best bet would be to use a rock-solid input function to get in a line, then just check that line to see if it's valid. Such a function can be found here. Incorporating that into your needs would give you something like:
#include <stdio.h>
#include <string.h>
#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf ("%s", prmpt);
fflush (stdout);
}
if (fgets (buff, sz, stdin) == NULL)
return NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '\0';
return OK;
}
#include <ctype.h> // only needed for isdigit, not for above code.
int main(void) {
int option;
do {
// Only allowed up to two characters (plus '\0').
char buff[3];
int result = getLine(
"Menu\n"
" 0. Create Database\n"
" 1. Add New Student\n"
" 2. Show All Students\n"
" 3. Delete Student\n"
" 99. Exit\n"
"Please choose option: ",
buff, sizeof(buff));
// No point continuing on EOF, input stream is closed.
if (result == NO_INPUT) {
puts("\n*** End of input");
return 1;
}
// All faults just restart operation: too long, too short,
// non-digits.
if (result == TOO_LONG) continue;
if (! isdigit(buff[0])) continue;
if (buff[1] != '\0' && ! isdigit(buff[1])) continue;
// Now get the integer representation and continue unless valid.
sscanf(buff, "%d", &option);
} while (option != 99 && (option < 0 || option > 3));
printf("You chose %d\n", option);
return 0;
}
And, yes, I know I said you should check the return value from scanf but it's not necessary in the case of my posted code since you've already established at that point that the string you're scanning is a valid one- or two-digit number.
Like Carl said:
isdigit is just for check single character, and should NOT be used here to the int value obtained from scanf .
To archive your requirement, we can use scanf to obtain a string, then use strtol convert to int, check the endptr to confirm if there is any invalid character.
Code may like this:
int option;
char str[8];
char *endp;
do{
printf("Please choose an option :\n0. Create Database\n1.Add New Student \n2. Show All Students \n3.Delete Student\n99.Exit\n");
scanf("%7s", str);
option = strtol(str,&endp,10);
} while (*endp);
This is the most complete and safest solution I can come up with using scanf:
int option;
int status;
char tailchar;
status = 2;
tailchar = '\n';
do {
if(tailchar != '\n' || status != 2){
for (int c=getchar(); c!='\n' && c!=EOF; c=getchar());
}
printf("Please choose an option:\n"
" 0. Create Database\n"
" 1. Add New Student \n"
" 2. Show All Students \n"
" 3. Delete Student\n"
"99. Exit\n");
status = scanf(" %d%c", &option, &tailchar);
} while (status != 2 || option < 0 || (option >3 && option != 99));
if(tailchar != '\n'){
for (int c=getchar(); c!='\n' && c!=EOF; c=getchar());
}
It only accepts valid options, and cleans any bad input by the user before asking to choose again.
I'm facing a problem with my code of a simple login program. The problem I'm facing is when I use a switch case or if statement for the option of logging in as an Admin or a User, the input for username is skipped and goes directly to password, and no matter what I type it gives me my error message. Instead, I want it to receive my username first then the password. It works fine on its own if there is only code for either Admin OR User, only one but not when there are more than one. Please help. Note: I'm using the same functions for both admin and user just to check if it works. The picture shows the output.I'm a C newbie, so minimal jargon perhaps? Code as follows:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char username[18];
char pass[16];
void arequest()
{
printf("\nPlease Enter username:");
fflush(stdin);
gets(username);
printf("\nPlease Enter Password:");
fflush(stdin);
gets(pass);
}
void averify()
{
if (strcmp(username, "admin") == 0)
{
if (strcmp(pass, "apass") == 0)
{
printf("Successful Login");
_getch();
}
else
{
printf("Invalid Password");
_getch;
}
}
else
{
printf("Invalid Username");
_getch();
}
}
int choice;
int main()
{
printf("Welcome to Railway Reservation System");
printf("\n1.Admin \n2.User");
printf("\nPlease Enter your selection:");
scanf_s("%d", &choice);
if (choice == 1)
{
arequest();
averify();
}
else if (choice == 2)
{
arequest();
averify();
}
else
{
printf("Invalid Choice");
_getch();
return main;
}
return 1;
}
output
You are flushing the input stream with fflush(). fflush(stdin) is undefined behavior in most cases, and is at best implementation-dependent. To clear the extra characters from the input stream, consider writing a little function like this:
void clear_stream(void)
{
int c;
while ((c = _getch()) != '\n' && c != EOF)
continue;
}
Remove the calls to fflush(). You do not need to clear the stream after gets(username) since gets() discards the newline. Add a call to clear_stream() after this line in main():
scanf_s("%d", &choice);
There may be extra characters, including a newline, left in the input stream after the call to scanf_s(), and these need to be removed before trying to read user input again. In some cases scanf()_s (and scanf()) will skip over initial whitespaces in reading input, but _getch() and getchar() will not. This illustrates one of the dangers of using scanf().
printf("\nPlease Enter your selection:");
scanf("%d", &choice);
clear_stream();
Also, gets() is considered so dangerous that there is never a reason to use it for anything at all. Use fgets() instead. fgets() does keep the newline, where gets() discards it, so I often write my own version of gets() using fgets() that is safe:
char * s_gets(char *st, int n)
{
char *ret;
int ch;
ret = fgets(st, n, stdin);
if (ret) {
while (*st != '\n' && *st != '\0')
++st;
if (*st)
*st = '\0';
else {
while ((ch = getchar()) != '\n' && ch != EOF)
continue; // discard extra characters
}
}
return ret;
}
The library conio.h is nonstandard, as are the functions _getch() and scanf_s(). You should use the stdio.h functions getchar() and scanf(). The value returned by scanf() is the number of successful assignments, and you should check this to be sure that the input is as expected. In your program, if the user enters a letter at the selection prompt, no assignment is made, and the value of choice remains uninitialized. The code continues without handling this problem. choice could be initialized to some reasonable value, such as int choice = -1;. Alternatively, you can check the return value from scanf() to see if an assignment was made, and proceed accordingly.
I noticed that you are returning 1 from main(). You should return 0 unless there is an error. And, I see that you return main in the event of an invalid choice. Maybe you meant to return 1 here? And it appears that you have forgotten to #include <string.h> for the strcmp() function.
Finally, I don't understand why username, pass, and choice are global variables. This is a bad practice. These should be declared in main() and passed to functions as needed. It would be a good idea to #define the global constants MAXNAME and MAXPASS instead of hard-coding the array dimensions.
I didn't intend this to be a full-scale code review when I started, but that is what it turned into. Here is a revised version of your program that implements the suggested changes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 18
#define MAXPASS 16
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
continue;
}
char * s_gets(char *st, int n)
{
char *ret;
int ch;
ret = fgets(st, n, stdin);
if (ret) {
while (*st != '\n' && *st != '\0')
++st;
if (*st)
*st = '\0';
else {
while ((ch = getchar()) != '\n' && ch != EOF)
continue; // discard extra characters
}
}
return ret;
}
void arequest(char username[MAXNAME], char pass[MAXPASS])
{
printf("\nPlease Enter username:");
s_gets(username, MAXNAME);
printf("\nPlease Enter Password:");
s_gets(pass, MAXPASS);
}
void averify(char username[MAXNAME], char pass[MAXPASS])
{
if (strcmp(username, "admin") == 0)
{
if (strcmp(pass, "apass") == 0)
{
printf("Successful Login");
getchar();
}
else
{
printf("Invalid Password");
getchar();
}
}
else
{
printf("Invalid Username");
getchar();
}
}
int main(void)
{
char username[MAXNAME];
char pass[MAXPASS];
int choice;
printf("Welcome to Railway Reservation System");
printf("\n1.Admin \n2.User");
printf("\nPlease Enter your selection: ");
if (scanf("%d", &choice) == 1) {
clear_stream();
if (choice == 1)
{
arequest(username, pass);
averify(username, pass);
}
else if (choice == 2)
{
arequest(username, pass);
averify(username, pass);
}
else
{
printf("Invalid Choice: %d\n", choice);
getchar();
return 1;
}
} else {
clear_stream(); // stream has not yet been cleared
printf("Nonnumeric input");
getchar();
}
return 0;
}
EDIT
The OP mentioned in the comments that scanf() was causing problems in Visual Studio. Apparently Visual Studio tries to force the use of scanf_s(). The issue with this function is not that it is inherently bad, just that it is nonstandard. One solution might be to use the s_gets() function already added to the code to read the user selection into a character buffer, and then to use sscanf() to extract input. This has an advantage in that there is no need to call the clear_stream() function after s_gets(), because s_gets() cleans up after itself, so the clear_stream() function could now be removed altogether from the program. This can be accomplished with only a small change in main():
char choice_buffer[10];
int choice;
...
if (s_gets(choice_buffer, sizeof(choice_buffer)) &&
sscanf(choice_buffer, "%d", &choice) == 1) {
if (choice == 1)
...
} else {
printf("Nonnumeric input");
getchar();
}
s_gets() reads up to the first 9 characters (in this case) of a line of user input into choice_buffer, which is an array that will hold chars (there is more space in choice_buffer than is needed to hold a single digit choice and a '\0'). If there is an error, s_gets() returns a NULL pointer, otherwise a pointer to the first char of choice_buffer is returned. If the return value of s_gets() was non-NULL, then sscanf() assigns the first int stored in the buffer to choice. If no int is found in the string, sscanf() returns a value of 0, failing the conditional test.
I created a program to make a diamond out of *'s. I am looking for a way to check if the type of input is an integer in the C language. If the input is not an integer I would like it to print a message.
This is what I have thus far:
if(scanf("%i", &n) != 1)
printf("must enter integer");
However it does not display the message if it's not an integer. Any help/guidance with this issue would be greatly appreciated!
you can scan your input in a string then check its characters one by one, this example displays result :
0 if it's not digit
1 if it is digit
you can play with it to make your desired output
char n[10];
int i=0;
scanf("%s", n);
while(n[i] != '\0')
{
printf("%d", isdigit(n[i]));
i++;
}
Example:
#include <stdio.h>
#include <string.h>
main()
{
char n[10];
int i=0, flag=1;
scanf("%s", n);
while(n[i] != '\0'){
flag = isdigit(n[i]);
if (!flag) break;
i++;
}
if(flag)
{
i=atoi(n);
printf("%d", i);
}
else
{
printf("it's not integer");
}
}
Use fgets() followed by strtol() or sscanf(..."%d"...).
Robust code needs to handle IO and parsing issues. IMO, these are best done separately.
char buf[50];
fgets(buf, sizeof buf, stdin);
int n;
int end = 0; // use to note end of scanning and catch trailing junk
if (sscanf(buf, "%d %n", &n, &end) != 1 || buf[end] != '\0') {
printf("must enter integer");
}
else {
good_input(n);
}
Note:
strtol() is a better approach, but a few more steps are needed. Example
Additional error checks include testing the result of fgets() and insuring the range of n is reasonable for the code.
Note:
Avoid mixing fgets() and scanf() in the same code.
{ I said scanf() here and not sscanf(). }
Recommend not to use scanf() at all.
strtol
The returned endPtr will point past the last character used in the conversion.
Though this does require using something like fgets to retrieve the input string.
Personal preference is that scanf is for machine generated input not human generated.
Try adding
fflush(stdout);
after the printf. Alternatively, have the printf output a string ending in \n.
Assuming this has been done, the code you've posted actually would display the message if and only if an integer was not entered. You don't need to replace this line with fgets or anything.
If it really seems to be not working as you expect, the problem must be elsewhere. For example, perhaps there are characters left in the buffer from input prior to this line. Please post a complete program that shows the problem, along with the input you gave.
Try:
#include <stdio.h>
#define MAX_LEN 64
int main(void)
{ bool act = true;
char input_string[MAX_LEN]; /* character array to store the string */
int i;
printf("Enter a string:\n");
fgets(input_string,sizeof(input_string),stdin); /* read the string */
/* print the string by printing each element of the array */
for(i=0; input_string[i] != 10; i++) // \0 = 10 = new line feed
{ //the number in each digits can be only 0-9.[ASCII 48-57]
if (input_string[i] >= 48 and input_string[i] <= 57)
continue;
else //must include newline feed
{ act = false; //0
break;
}
}
if (act == false)
printf("\nTHIS IS NOT INTEGER!");
else
printf("\nTHIS IS INTEGER");
return 0;
}
[===>] First we received input using fgets.Then it's will start pulling each digits out from input(starting from digits 0) to check whether it's number 0-9 or not[ASCII 48-57],if it successful looping and non is characters -- boolean variable 'act' still remain true.Thus returning it's integer.