Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 7:50 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 4 posts ] 
Author Question
 Question subject: code problem
PostPosted: Mon Oct 11, 2010 6:10 pm 
Offline
Newbie
User avatar

Joined: Mon Oct 11, 2010 12:28 pm
Posts: 2
Has thanked: 1 time
Have thanks: 0 time

pls can anyone help me ?? i have a problem with the following code...The code is ..
Code:
main()
{
   int a,b;
   scanf("%d%d",&a,&b);
   char x;
   scanf("%c",&c);
   switch(x);
   {
     case'A':
     printf("good");
     break:
     default:
     printf("bad");
}

The output of following code in C is "bad" but problem is that during execution,it only asks for the values of a &b.It doesnt ask for the value of x whose datatype is char.Why So?
The Same program if executed in C++ using cout and cin works fine n even asks for the value of x.Why So that the above code works for C++ and not in C?

pls help me...


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: code problem
PostPosted: Mon Oct 11, 2010 10:33 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2272
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
Code:
scanf("%c",&c);

is think it should be
Code:
scanf("%c",&x);

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: code problem
PostPosted: Wed Oct 13, 2010 12:53 pm 
Offline
Newbie
User avatar

Joined: Wed Oct 13, 2010 12:36 pm
Posts: 1
Has thanked: 0 time
Have thanks: 1 time
use \n before














use \n before %c in scanf statement as shown below......
main()
{
int a,b;
scanf("%d%d",&a,&b);
char x;
scanf("\n%c",&c);
switch(x);
{
case'A':
printf("good");
break:
default:
printf("bad");
}



For this message the author Shikshakanwar has received gratitude : jestino23
TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: code problem
PostPosted: Sat Oct 16, 2010 8:28 am 
Offline
Newbie
User avatar

Joined: Mon Oct 11, 2010 12:28 pm
Posts: 2
Has thanked: 1 time
Have thanks: 0 time
ya thank u but can u pls tell me whats the significance of '\n' here in the program....pls


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 4 posts ] 
Quick reply


  

 Similar topics
 Read your gmail using Java code
 Code to open multiple links.
 I need help with checkers code
 java code for listing folder contents from remote folder
 code for online payment system in java
 JavaScript fade out problem
 apriori algorithm java code
 playfair cipher assembly code
 code
 connecting PC with mobile using bluetooth code in java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team