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 January 14th, 2013, 05:57 AM
mo6361 mo6361 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 mo6361 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 6 sec
Reputation Power: 0
Red face 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();
}

Reply With Quote
  #2  
Old January 14th, 2013, 09:29 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
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 16 h 5 m 8 sec
Reputation Power: 1949
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);

Reply With Quote
  #3  
Old January 14th, 2013, 10:06 AM
mo6361 mo6361 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 mo6361 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 6 sec
Reputation Power: 0
Smile

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?

Reply With Quote
  #4  
Old January 14th, 2013, 12:25 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
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 16 h 5 m 8 sec
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C programing help

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