C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2013, 01:08 PM
uzairali001 uzairali001 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 uzairali001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m
Reputation Power: 0
Question 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");
    }


}
Comments on this post
requinix disagrees: DON'T YELL AT US

Reply With Quote
  #2  
Old March 1st, 2013, 01:46 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
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.

Code:
scanf("%d", &num2);
    printf("Enter Operator");
        scanf("%c", &op);


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.

Reply With Quote
  #3  
Old March 1st, 2013, 01:51 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by uzairali001
WHAT IS WRONG WITH THIS CODE?
What, apart from the bizarre, arbitrary and unconventional indentation you mean?
Comments on this post
dwise1_aol agrees: And the implied int return type for main while never actually returning anything?

Reply With Quote
  #4  
Old March 1st, 2013, 02:04 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 15 h 33 m 14 sec
Reputation Power: 1949
Also cross-posted: http://stackoverflow.com/questions/...r-in-c-language

Makes me regret having helped him.
Comments on this post
clifford agrees: But on Stackoverflow, you can down-vote, vote-to-close, and vote-to-delete, for questions not quite
there, you can edit them if you are feeling helpful/charritable. This one was closed in minutes and
has attracted a -9 score and a vote to delete.

Reply With Quote
  #5  
Old March 2nd, 2013, 05:55 AM
uzairali001 uzairali001 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 uzairali001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m
Reputation Power: 0
Thumbs up

Quote:
Originally Posted by dwise1_aol
Also cross-posted:

Makes me regret having helped him.


thanks for helping me
they closed my thread without helping me thats why i posted problem here

Reply With Quote
  #6  
Old March 2nd, 2013, 11:30 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #7  
Old March 2nd, 2013, 12:01 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #8  
Old March 2nd, 2013, 12:39 PM
uzairali001 uzairali001 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 uzairali001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m
Reputation Power: 0
thanks dear
actually i m not native speaker thats why i dont know how to post
and i m not shouting

Reply With Quote
  #9  
Old March 2nd, 2013, 02:47 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #10  
Old March 4th, 2013, 08:16 AM
Mom@786 Mom@786 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 1 Mom@786 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 24 m 23 sec
Reputation Power: 0
Smile 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");
    }


}
Comments on this post
ptr2void disagrees: No, you should never use fflush(stdin)!

Reply With Quote
  #11  
Old March 4th, 2013, 08:51 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,833 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 14 h 6 m 4 sec
Reputation Power: 1774
Flushing stdin isn't specified by the standard.
read this
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Whats wrong with this code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap