This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Using scanf() function two times: works in one case but not in other case [duplicate]
(4 answers)
Closed 3 months ago.
I am new to C programming language and I was trying to run this simple C program , I was expecting the program to go take two char inputs separately and some output based on some if condition , but the program just takes one input and terminates without going through second input , I don't know what I am doing wrong ,please help . this is the output I am getting , below is the code I was trying to run
#include <stdio.h>
int main()
{
char c,d;
printf("Enter your first character : ");
scanf("%c",&c);
if (c=='y')
{
/* code */
printf("y");
printf("\nEntered character is %c\n" ,c);
}
else
{
printf("unknown character");
}
printf("Enter your second character : ");
scanf("%c",&d);
if (c=='a')
{
printf("\nEntered character is %c\n" ,d);
}
else
{
printf("Unknown character");
}
}
Related
This question already has answers here:
Scanf reads only one word from sentence
(4 answers)
Closed 1 year ago.
#include<stdio.h>
#include<string.h>
int main(){
char sen[100];
int i=0,len;
printf("Enter a senetence\n");
scanf("%s",sen);
len=strlen(sen);
char ch=sen[i];
while(i<len){
if(ch==' '){
printf("\n");
i++;
ch=sen[i];
continue;
} //i dont know what is the flaw with the logic.
printf("%c",ch);
i++;
ch=sen[i];
}
return 0;
}
The aim of the program is to print the words of a sentence separately. However, this program is successful in printing only the first word. Please tell me where I am going wrong.
You get just one word as input. You can scanf the input like:
scanf("%[^\n]s",sen);
This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 5 years ago.
#include<stdio.h>
int main()
{
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
}
find output of program. It is not taking 2 characters.
When you enter the first character;
The character Ascii value is placed in the variable A .
Then you press enter and the ascii value of enter thats 13 is placed in the second variable.
So B should be 13.
In order to run this program use flushall() library function..
int main() {
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
flushall();
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
return 0;
}
You could also use the standard Library function scanf()
Write something like:
Scanf(" %c",&variablename);
The space before the %c tells the compiler to skip any whitespace (if any).
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
In following c code scanf is not working.
When program is run, it execute upto the second printf line and after it skips the scanf("%c",&sex); and directly execute next printf().
Why this so happen?
I run this code on different c compilers, but the output is same.
#include<stdio.h>
void main()
{
char mar,sex;
int age,flag=0;
printf("Married [Y/N]:");
scanf("%c",&mar);
printf("Sex [M/F] :");
scanf("%c",&sex); //**This not working**
printf("Age :"); //**execution directly jumped here**
scanf("%d",&age);
if(mar=='y')
flag=1;
else if(sex=='m'&& age>=30)
flag=1;
else if(sex=='f'&& age>=25)
flag=1;
else
{
}
if(flag)
{
printf("Congratulations!!!! You are Egligible..");
else
printf("Sorry... You are not egligible..");
getch();
}
//Output
Married [Y/N]:y
Sex [M/F] :Age :23
Congratulations!!!! You are Egligible..
The problem is with the new line character that you press after you enter values (Y/N) for the previous scanf. The new line character is taken as an input and the program proceeds with the next one. Try to use flushall(); before next read (that is scanf) this will solve your problem. You can also use space before the format specifier to solve this, that will escape the newline character.
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 6 years ago.
I'm a newbie learning to program in C, and I am currently working on creating a program that takes a name input from a user then prints that name back on to the screen. When the program prints out the name in the else if block, what I get is $ ". I want to know why it happens and how I might correct this problem. My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int main() {
char * ans; // yes/no answer
char * name; //name variable
printf("Welcome to the program. Would you like to begin? (y/n)\n");
scanf("%s", ans);
if (strcmp(ans, "y") != 0) {
printf("Have a good day!\n");
return 0;
}
else if (strcmp(ans, "y") == 0)
printf(" %s\n", ans);
printf("Okay, input your name:\n");
scanf("%s", name);
printf(" %s", name);// I get $ " rather than the name.
return 0;
}
You are using scanf() to read characters from the user, but haven't allocated any memory to hold those characters. This gives undefined behavior, as "anything can happen" when you break the rules like that.
Make those uninitialized pointers into arrays instead:
char ans[128];
char name[128];
This gives you 128 characters of space for each string (one of which will be used by the terminator).
This question already has answers here:
i want to getchar twice but i cant
(2 answers)
Closed 7 years ago.
I have been tasked to create a simple lower to upper case program. However, while testing if my input prints correctly, I have noticed that every time I enter a character, it always loops again even when its not supposed to.
#include <stdio.h>
int main() {
char input;
//enter a character
//set the given input char to variable
printf("Enter a character in lower case: \n");
input = getchar();
//Sentinel value is Q
while (input != 'Q'){
printf("you entered %c.\n",input);
printf("\n");
printf("Enter a character in lower case: \n");
input = getchar(); //gets input from inside the loop
}
printf("Goodbye \n");
return 0;
}
test output (I input the character 'g' and pressed enter once):
Enter a character in lower case:
g
you entered g.
Enter a character in lower case:
you entered
.
I'm not seeing the problem.
The problem here is caused by the newline character entred into the stdin by pressing the ENTER key. Before proceeding for the next input, you need to clear the input buffer , like adding
while (getchar() != '\n');
before asking for the next input.
That said, getchar() returns an int. A char may not be enough to hold the return value always, Change
char input;
to
int input = 0;