The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C programing help
Discuss C programing help in the C Programming forum on Dev Shed. C programing help 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.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 14th, 2013, 05:57 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 29 m 6 sec
Reputation Power: 0
|
|
C programing help
help hello
im moni,
im a real beginner in c programing,
and im trying to sum two numbers and im doin it with switch
although i dont know how to use it.
i know i can do it some other way,
and when im runing this prog it showing some wrong num,
and i cant give data to scanf("%d",&b);
but just wandering so plzzz help
thanks in advance
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,sum;
char ch;
clrscr();
printf("\t\t\t this is calculator program \n\n\n\n");
printf("enter 1st num: \n");
scanf("%d",&a);
printf("enter any operator: \n");
scanf("%c \n\n",&ch);
printf("enter 2nd num: \n");
scanf("%d",&b);
switch(ch)
{
case '+':
sum=a+b;
break;
default:
} ;
printf("\n\n\n answer= %d", sum);
getch();
}
|

January 14th, 2013, 09:29 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
scanf("%c \n\n",&ch);
says to read the very next character in the input buffer, which would be the newline from the Enter key when you entered the first number. If you rewrote the default case to output the ASCII code of ch (ie, printf with "%d") then you would see what ch's value is.
You want that scanf to ignore all white space that precedes the character you want it to read, so you need to tell it that thus (note the space before the %):
scanf(" %c \n\n",&ch);
|

January 14th, 2013, 10:06 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 29 m 6 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by dwise1_aol scanf("%c \n\n",&ch);
says to read the very next character in the input buffer, which would be the newline from the Enter key when you entered the first number. If you rewrote the default case to output the ASCII code of ch (ie, printf with "%d") then you would see what ch's value is.
You want that scanf to ignore all white space that precedes the character you want it to read, so you need to tell it that thus (note the space before the %):
scanf(" %c \n\n",&ch); |
wow nice i gave space before %c and it working...
thanksuuu very much..
can u explain why it happend?
|

January 14th, 2013, 12:25 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by mo6361 can u explain why it happend? |
I already did explain why it happened.
Review scanf format strings. Any literal characters are what scanf is supposed to expect to see and to skip.
Review the concept of white space.
Review how scanf works.
Read the "commonly asked questions" thread, where this subject is undoubtedly covered. Your mistake shows up here with extreme regularity.
If you then still do not understand something, come back and ask that specific question.
Last edited by dwise1_aol : January 14th, 2013 at 01:34 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|