Discuss Whats wrong with this code in the C Programming forum on Dev Shed. Whats wrong with this code C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 3
Time spent in forums: 19 m
Reputation Power: 0
Whats wrong with this code
WHAT IS WRONG WITH THIS CODE? IT SCAN num1 IT SCAN num2 THEN IT SHOW ENTER OPERATOR AND THEN WITHOUT SCANING OPERATOR IT SHOW WRONG OPERATOR IT DOES NOT SCAN op WHY I DONT KNOW PLEASE HELP ME GUYx
Code:
#include <stdio.h>
main ()
{
int num1, num2;
char op;
printf("Enter 1st Number: ");
scanf("%d", &num1);
printf("Enter 2nd Number: ");
scanf("%d", &num2);
printf("Enter Operator");
scanf("%c", &op);
switch (op)
{
case '+':
printf("Sum is %d", num1+num2);
break;
case '-':
printf("Difference is %d", num1-num2);
break;
case '*':
printf("Multiplication is %d", num1*num2);
break;
case '/':
printf("Division is %d", num1/num2);
break;
default:
printf("Wrong Operation");
}
}
Posts: 6,123
Time spent in forums: 2 Months 2 Weeks 3 Days 15 h 33 m 14 sec
Reputation Power: 1949
Even if you are getting frustrated, that is never an excuse to yell at us!
The problem is that your program is doing exactly what you are telling it to do. Even though that's not what you want it to do. Remember, computers never do what we want them to do, but only what we tell them to do.
When scanf converts a value from the input stream (stdin), it stops and leaves whatever follows in the input buffer, including the characters for the Enter key that you pressed when you entered that value. So when you read the decimal value for num2, scanf skips white space (spaces, newlines, tabs, etc) until it finds the next printable characters which it then tries to convert into an integer value and as soon as it hits a non-digit it stops. Then you tell scanf to read in the next character, regardless of what it is, even if it is nothing but white space, which it does. You should output the ASCII code for what op has read in; that should be included in your default case's printf("Wrong Operation");. You will most likely find it to be ASCII code 13 or 10, though it could be 32 if you had added a space after that second number. And that is exactly what you are telling scanf to do.
What you want is for scanf to ignore all white space that occurs before a printable character. This is how you tell it to do that: scanf(" %c", &op);
Please note the space that precedes the percent sign. That tells scanf to expect and to ignore any and all white space that occurs before the printable character that you want it to read in.
Posts: 4,806
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by uzairali001
they closed my thread without helping me thats why i posted problem here
On Stackoverflow you can edit your posts to make them more acceptable to the community and thus avoid down-voting and closure. It is well to take comment advice as soon as possible to improve its quality. You were asked not to SHOUT on Stackoverflow, and you persisted in exactly that when you posted here.
Moreover the Stackoverflow community work to avoid duplication - it is a hybrid forum/wiki format and duplicates will get quickly closed and deleted, and the misapplication of scanf() is covered extensively already - you should have searched first. The idea on Stackoverflow is that becomes a massive searchable Q&A database of high quality questions and answers, whereas in the forum format here, old questions are quickly forgotten and hard to find. You question and any answers have to be of sufficiently high quality to survive on Stackoverflow. That may seem somewhat elitist, but it makes it a far more useful resource.
In your case I think it was perhaps somewhat harsh, it could have been edited for you, or marked it as a duplicate of an existing question. It appears that at least one user did attempt an edit, but gave up. I am not clear why, perhaps he lacked the time or inclination - no body has to help.
Posts: 4,806
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by clifford
[...] it could have been edited for you [...]
Because I would not want your first experience on the excellent Stackoverflow to discourage you from using it again, I have "fixed" your post there, you should look at the changes to understand where you went wrong. I can't fix your question here. The changes include:
Normal typography (no shouting).
Conventional code formatting
ISO compliant code.
Accurate English (though not having English as a first language is not a something anyone will criticise you for).
Depersonalised (no pleading "help me", no addressing the "guys" or anyone in particular, just the facts - that's how Stackoverflow likes it).
More relevant tags.
That said, the issue is dealt with all over the Internet, it is one of the most common issues a novice encounters and is even mentioned in the sticky-thread of this very forum. Consequently I did not vote to reopen it, but it may stop attracting down-votes. However to be honest, if you have solved the issue, to protect your SO rep further I would simply delete the question yourself ; that way it cannot be further down-voted. Even closed question can be voted on on SO.
Posts: 4,806
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by uzairali001
and i m not shouting
You misunderstand; in electronic textual communication, writing in ALL CAPITALS is regarded as shouting. It is not considered polite. It is also hard to read any moderately long piece of text written in that way.
Where I am from, addressing someone as "dear" is likely to be considered rude too. Unless you wereMicheal Winner of course, who made a career from being rude in any case.
Last edited by clifford : March 2nd, 2013 at 02:52 PM.
Posts: 1
Time spent in forums: 24 m 23 sec
Reputation Power: 0
Nee to make minor adjustment
Before
Code:
printf("Enter Operator");
line add
Code:
fflush(stdin)
Quote:
Originally Posted by uzairali001
WHAT IS WRONG WITH THIS CODE? IT SCAN num1 IT SCAN num2 THEN IT SHOW ENTER OPERATOR AND THEN WITHOUT SCANING OPERATOR IT SHOW WRONG OPERATOR IT DOES NOT SCAN op WHY I DONT KNOW PLEASE HELP ME GUYx
Code:
#include <stdio.h>
main ()
{
int num1, num2;
char op;
printf("Enter 1st Number: ");
scanf("%d", &num1);
printf("Enter 2nd Number: ");
scanf("%d", &num2);
fflush(stdin);
printf("Enter Operator");
scanf("%c", &op);
switch (op)
{
case '+':
printf("Sum is %d", num1+num2);
break;
case '-':
printf("Difference is %d", num1-num2);
break;
case '*':
printf("Multiplication is %d", num1*num2);
break;
case '/':
printf("Division is %d", num1/num2);
break;
default:
printf("Wrong Operation");
}
}