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';
Related
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]) ...
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
//code for mvt. in this ch did not scan by compiler. ch is used as a condition for loop . Its initial value is 'y' , and if it goes to 'n' , loop breaks. In this code i am asking user if he/she want to continue then press y else n . but don't know why gcc do not wait for scan it. It is a simple code but i can not get the mistake.
int main(){
char ch;
temp=tm;
ch='y';
for(i=0;ch=='y';i++){
printf("enter memory size for process %d",i+1);
scanf("%d",&m);
if(m<temp)
{printf("memory allocated\n"); ms[n]=m; n++;
temp-=ms[i];
}
else
{printf("memory not allocated\n"); }
https://stackoverflow.com/users/10404087/rishabh-sharma
printf("do you want to continue");
scanf("%c",&ch);
}
printf("total memory : %d\n",tm);
printf("process \t occupied \n");
for(i=0;i<n;i++)
printf("%7d \t %8d \n",i+1,ms[i]);
printf("total externel fregment : %d \n",temp);
return 0;
}
You have to use flushall() function that clears all buffers associated with input streams, and writes any buffers associated with output streams.
Added:flushall() isn't C, but a vendor specific extension.
OR
Other alternative is to use space before %c
Example
char ch;
scanf(" %c", &ch);
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 5 years ago.
Improve this question
I am trying to develop a small program which is available below:
When I run the program, even after the right input, it gives me the output described in else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
You mean
scanf(" %c", &course);
But also,
Strings in c MUST be wrapped with double qoutes, the expression 'TOEFL' must be generating a warning about multi character constant, do not ignore it.
Strings in c, are compared one character at a time, so you need to use a function called strcmp() for that.
To read a string, you need an array to store it in, and yes, the "%s" specifier
char cours[100];
scanf("%99s", course);
if (strcmp(course, "TOEFL") == 0) ...
course is a char variable, so it only can contain a single character.
Try changing its declaration to:
char course[10];
See my code comments for explaination //xxxxxx below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course; //this decl stores only single character not string
printf("Enter Your Course Name: \n");
scanf(" %s", &course); //passing string argument
if (course == 'TOEFL') {//can't compare strings with == use strcmp
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
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 7 years ago.
Improve this question
Sorry this has been confusing me for hours and nothing comes up on google about this. I'm programming in C, trying to convert decimal to binary, and I'm confused as to why this keeps giving me the right amount as input but the integer value is always 0.
even if I properly cast input as an int, it doesn't work (ie: int integer = (int) input;
main() {
double input;
scanf("%d", &input);
printf("input: %d \n", input); // this will print fine
int integer = input; // stores nums before decimal
printf("integer:%i\n", integer); // this always prints 0
Because you are using the wrong specifier to read -> scanf() or print -> printf() the double, do this
if (scanf("%lf", &input) == 1)
{
printf("input: %f\n", input);
printf("integer: %d\n", (int) input);
}
You need To Correct Format Specifiers.
main(){
double input;
scanf("%lf",&input);
printf("input: %lf \n", input);
int integer = input;
printf("integer:%i\n", integer); }
Some Format Specifiers
Or you can read from here
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.