#include <stdio.h>
int main(int argc, const char * argv[]) {
//run...
printf("----------------CALC--------------\n");
printf("(1) Type in a letter to get a secret message: ");
int validSecret = 0;
char secretLetter;
while(validSecret == 0){
scanf("%c",&secretLetter);
if(secretLetter == 'b'){
printf("B\n");
}
else if (secretLetter == 'r'){
printf("R\n");
}
else if (secretLetter == 'k'){
printf("K\n");
}
else {
printf("Game over\n");
validSecret = 1;
}
}
return 0;
}
If I enter a b the if statement executes correctly and prints a B and a new line but also prints game over. It's running the if and the else... This makes no sense.
The newline you've entered (corresponding to a '\n' character) doesn't match any of the other conditions; of course your code will take the else branch because of it.
Is it possible that stdin could reach EOF? Your code ignores the return value, and so wouldn't be able to detect such scenarios... It should probably look more like:
while (scanf("%c", &secretLetter) == 1) {
if (secretLetter == 'b') {
printf("B\n");
}
else if (secretLetter == 'r'){
printf("R\n");
}
else if (secretLetter == 'k'){
printf("K\n");
}
else if (secretLetter != '\n') {
printf("Game over\n");
break;
}
}
Notice how I converted your else to an else if (secretLetter != '\n')? This should prevent "Game over" from being printed when secretLetter is \n, solving the first problem...
Also notice how I made the terminal condition of the loop match the failure of scanf? This should prevent the loop from continuing when stdin reaches EOF. You might want to move the printf("Game over\n"); to outside of the loop to account for this.
You don't need an intermediate variable in place of break here; you should avoid unnecessary clutter at all cost.
After you read input, you need to flush the input buffer, like so
void flush()
{
while (getchar() != '\n')
;
}
This is so that all characters up to, and including, the newline are consumed. The reason is that scanf stops reading as soon as it sees the '\n', so it leaves it in the inpu buffer. Also, never flush wih fflush(stdin), because that isn't portable.
This is because the '\n' input by pressing Enter is also consumed by scanf(). You can use
scanf(" %c", &secretLetter);
to discard these whitespaces (including ' ', '\t', '\n' and something else).
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 am trying to use getchar() to remove characters from the input buffer. In the following code, the user is asked to input a choice to select, and then depending on the choice, another input is required, either type int or type char (string).
In the int case, getcar() is not needed and scanf takes in input correctly. But in the char case, scanf fails to get input without using getchar() beforehand. Is there a reason why that is?
printf("Available Ciphers:\n1) Caesar Cipher\n2) Vigenere Cipher\nSelected Cipher: ");
if(scanf("%d", &choice) != 1){
printf("Error: Bad selection!\n");
exit(EXIT_SUCCESS);
} else if (choice != 1 && choice != 2){
printf("Error: Bad Selection!\n");
exit(EXIT_SUCCESS);
//If the choice entered is correct, then run the following.
} else {
if(choice == 1){
printf("Input key as nuumber: ");
if(scanf("%d", &caesarkey) != 1){ //Why is getchar() not needed here?
printf("Error: Bad Key!\n");
exit(EXIT_SUCCESS);
}
//morecode here
} else if (choice == 2){
printf("Input key as string: ");
while(getchar() != '\n'); //Why is this needed here?
/*Uses scanf and not fgets, since we do not want the
key to contain the newline character '\n'. This is
due to the fact that the newline character is not
considered in the function that encrypts and decrypts
plaintext and ciphertext.*/
if(scanf("%[^\n]s", vigencipherkey) != 1){
printf("Error, Cannot read inputted key!\n");
exit(EXIT_SUCCESS);
}
//More code here..
}
}
It seems that you are scanning for a string rather than an int, and as such, you are passing in an int rather than the address of an int.
Change this line
if(scanf("%[^\n]s", vigencipherkey) != 1){
To
if (scanf("%d", &vigencipherkey) != 1) {
In order to read the remainder of the line input by the user, you can use this function:
int flush_line(void) {
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
return c;
}
Notes:
c must be defined as int to accommodate for all values of the type unsigned char and the special negative value EOF.
you should test for '\n' and EOF otherwise you will have an endless loop on premature end of file without a trailing newline, such as would occur if you redirect the input of your program from an empty file.
you can test for end of file by comparing the return value of flush_line() with EOF.
In class I need to use scanf to get integers to work with. Problem is I do not know to end the while loop. I wait for '\n' in the code, but it is passing all tests. The program has to complete for grading.
How to make code work when input includes several '\n' in input and spacebars at the end of input.
All numbers are given with spacebar between.
# include <stdio.h>
int main()
{
int numbers;
char ch;
int stop = 0;
while(scanf("%d%c", &numbers, &ch))
{
if((ch == '\n') stop++;
#my_code
if (stop == 1) break;
}
while(scanf("%d%c", &numbers, &ch)) { if((ch == '\n') .... has a couple of problems.
If the line of input has only white-space like "\n" or " \n", scanf() does not return until non-white-space is entered as all leading white-spaces are consumed by "%d".
If space occurs after the int, the "\n" is not detected as in "123 \n".
Non-white-space after the int is discarded as in "123-456\n" or "123x456\n".
how to end loop?
Look for the '\n'. Do not let "%d" quietly consume it.
Usually using fgets() to read a line affords the more robust code, yet sticking with scanf() the goal is to examine leading white-space for the '\n'
#include <ctype.h>
#include <stdio.h>
// Get one `int`, as able from a partial line.
// Return status:
// 1: Success.
// 0: Unexpected non-numeric character encountered. It remains unread.
// EOF: end of file or input error occurred.
// '\n': End of line.
// Note: no guards against overflow.
int get_int(int *dest) {
int ch;
while (isspace((ch = fgetc(stdin)))) {
if (ch == '\n') return '\n';
}
if (ch == EOF) return EOF;
ungetc(ch, stdin);
int scan_count = scanf("%d", dest);
return scan_count;
}
Test code
int main(void) {
unsigned int_count = 0;
int scan_count;
int value;
while ((scan_count = get_int(&value)) == 1) {
printf("%u: %d\n", ++int_count, value);
}
switch (scan_count) {
case '\n': printf("Normal end of line.\n"); break;
case EOF: printf("Normal EOF.\n"); break;
case 0: printf("Offending character code %d encountered.\n", fgetc(stdin)); break;
}
}
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.
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;
}