compare single char in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my code here
char temp;
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A') goto AGAIN;
why did it go back to AGAIN even though my input is 'A' ?
If I put this block of code to main() function, it works correctly.
But if I do the same thing in the subrountine, it failed.

This is because of the \n character read by the sacnf is next call.
Try this
scanf(" %c", &temp);
^ A space before %c can eat up any number of white-spaces

But for me, its work!. Are you given any other label as AGAIN in your program?
#include<stdio.h>
//./a.out
int main()
{
char temp;
calfunc(temp);
}
char calfunc(char temp)
{
printf("Inside Function");
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A')
goto AGAIN;
else
printf("Temp: %c\n",temp);
printf("Final: %c\n",temp);
return temp;
}
Result:
If type 'A', Temp:A Final:A
If type any other even 'a', Enter char: Enter char:. Then cursor places.

Related

Why use getchar in specific line? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why the following code is getting terminated after one round if the getchar() in the 5th line is not used?
char s;
while(1){
printf("Enter two integers: ");
scanf("%d%d",&a,&b);
getchar();
c = a + b;
printf("Addition of %d & %d is %d\n",a,b,c);
printf("Add more numbers(yes/no): ");
scanf("%c",&s);
if(s == 'y')
continue;
else
break;
}
And why using string is not working in the following code? I have included the <string.h> file.
char s[10];
while(1){
printf("Enter two integers: ");
scanf("%d%d",&a,&b);
getchar();
c = a + b;
printf("Addition of %d & %d is %d\n",a,b,c);
printf("Add more numbers(yes/no): ");
scanf("%s",s);
if(s == "yes")
continue;
else
break;
}
When you enter the input for the first scanf("%d%d",&a,&b) then you probably ended that with the Enter key.
That Enter key is added as a newline into the input buffer that scanf reads from.
If you don't have the getchar() call then that newline is what the next scanf("%c",&s) will read.
One solution is, as you've noted, to use getchar() to read and throw away the newline. Another solution is to ask scanf to do the same:
scanf(" %c",&s);
// ^
// Note the space here!
The leading space tells scanf to skip and ignore all leading white-space character, of which newline is one such character.
Note that most format specifiers for scanf automatically skip and ignore leading white-space. For example %s will do that, which means your second version with scanf("%s",s) doesn't need the getchar() call.
Also note that this should have been easily deduced by spending five minutes in a debugger to step through the code statement by statement while monitoring variables and their values.
Then for s == "yes". What happens here is that the array s will decay to a pointer to its first element, as will the literal string, and then you compare these pointers to see if they are the same, which they will most likely never be.
It's somewhat simplified equivalent to this:
char yes_literal[4] = "yes";
if (&s[0] == &yes_literal[0]) ...

scanf() running only once inside for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am a newbie to C programming. I am trying to solve a question using scanf in loop, but the problem is that scanf is only running once inside the loop. My code is:
#include <stdio.h>
#include <string.h>
int main()
{
int n;
int x=0;
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
char stat[3];
scanf ("%s", stat);
if (strcmp(stat, "X++")==0)
x++;
else if (strcmp(stat,"++X")==0)
x++;
else if (strcmp (stat, "--X")==0)
x--;
else if (strcmp(stat, "X--")==0)
x--;
}
printf ("%d", x);
return 0;
}
Why is the scanf running only once even when n is 2, 3 or anything else?
This may be because the value of the variable n is destroyed by out-of-bounds write.
Your buffer stat don't have insufficient size to store 3-character string because there are no room to store terminating null character.
Increase buffer size and limit number of characters to read for safety.
Checking if reading is successful will make it safer.
char stat[3];
scanf ("%s", stat);
should be
char stat[4];
if (scanf ("%3s", stat) != 1) return 1;

Why is my C program skipping over if statements? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am a new coder and have no idea what I am doing please help!
The code is reading and taking inputs just fine until it reaches the scanf(" %c", &i);then it skips to the Amount print seemingly ignoring my if statements.Is there somthing wrong with my use of scanF?
Here is the code:
#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D;
int t=1;
double b,d;
while (t=1){
printf("Enter the old balance:\n");
scanf(" %lf", &b);
printf(" %lf", b);
if(b>=0)
{
printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");
printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
scanf(" %c", &i);
if(i=F){
printf("Your ending balance is");
printf(" %lf", b);
printf("\n Program is ending!");
return 0;
}
if(i=W){
printf("Amount:");
scanf(" %f", &d);
b= b-d;
printf(" %f",b);}
}
if(b<0);
{
printf("The balance must be maintained above zero!\n");
}
}
return 0;
}
Because your comparison is wrong
if(i=F){ // this is assignment, not comparison
should be
if(i=='F'){ // note also it's comparison to character 'F'
In addition to wrong comparison operators, your way of comparing two chars is also wrong. If you want to check whether the input char i equals to 'F' , you should initialize the char variable F with character 'F', otherwise it would be just unintialized variable and comparison with that variable would be wrong.
You should add these lines just after you initialize F and W.
F = 'F';
W = 'W';
By the way, you can use another variable names like,
char var1 = 'F';
char var2 = 'W';

Program crashes when prompting user for input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I made a simple little program and it crashes before it gets to the part of the if statement.
#include <stdio.h>
int main()
{
char name[100];
printf("What is your name?");
scanf("%s", name);
printf("Hello, %s\n",name);
printf("You are trapped in a tunnel. Go through the right doors to get out\n");
int choice;
printf("Choose door 1 or door 2");
scanf("%d", choice);
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
return 0;
}
My program runs fine with no errors, it just crashes...
scanf("%d", &choice);
This is how it was meant to be. choice is a standalone int, so you need to pass its address to scanf to get correct results. What you do now is passing a value that is stored in choice to scanf, while it expects an address.
The C library function int scanf(const char *format, ...) reads
formatted input from stdin.
So it should be like this:
int choice;
printf("Choose door 1 or door 2");
scanf("%d", &choice); //& means the address-of
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
You miss & in scanf statement that why it doesn't working..
It should be
scanf("%d",&choice);

No Error but why we don't get input for second scanf() statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c", &i);/* i know this must be reversed for next input but I want to know the impact of this code if we use scanf type char for integer data type*/
scanf("%d", &ch);
printf("%c %d", ch, i);
return 0;
}
No Error but why don't get input for second scanf() statement
You declared i to be an int and ch to be a char
In your scanf you have %c for i and %d for ch while %c is for chars and %d for ints.
So just turn them around and you'll be fine.
With printf you have the correct format.

Resources