Brainteaser codes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 44
Reputation: arunpawar is an unknown quantity at this point 
Solved Threads: 0
arunpawar arunpawar is offline Offline
Light Poster

Brainteaser codes

 
0
  #1
May 7th, 2008
It's not homework it is just a play.If you have any simmilar tragic like this one with int and character or float then please discuss it here or explain how things happen.Let this thread be informative.

I'm playing with int,floats and type casts but i have found that in following program when i enter the value of first integer as float,it skips out the cout statement that asks for float value,instead it takes the float value frm the decimal number we entered frm the first first input we get from cin.What is going on? i dnt get it if i entered float value then it skips next statement?How and why?How can i get my chance of inputing another float number in that case? Anyone like to explain what happened here?

  1. //IDE: Codeblocks 8.02
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int a;
  7. float b;
  8. cout<<"Enter the integer number"<<endl;
  9. cin>>a;
  10. cout<<"\n Enter float number"<<endl;
  11. cin>>b;
  12. cout<<endl;
  13. cout<<a<<endl;
  14. cout<<b<<endl;
  15. system("pause");
  16. exit(0);
  17. }


I have one more Big Question in this case is that "If i want to find out what value is input by user here,before printing it.How can i find out that it's integer or float?"

You can pass your teasing questions here or answer mine,i'm looking for simmilar situations with the code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Brainteaser codes

 
0
  #2
May 7th, 2008
Originally Posted by arunpawar View Post
I have one more Big Question in this case is that "If i want to find out what value is input by user here,before printing it.How can i find out that it's integer or float?"
You could store the user input in a string (instead of int or float), then check it for '.' character. If it has '.' then it's a float. Then use the string with a stringstream to put the number into float or int varable as desired.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 44
Reputation: arunpawar is an unknown quantity at this point 
Solved Threads: 0
arunpawar arunpawar is offline Offline
Light Poster

Re: Brainteaser codes

 
0
  #3
May 7th, 2008
Thnxfor big question.Do u have anything Teasing..Do post it in here...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Brainteaser codes

 
0
  #4
May 7th, 2008
Teasing questions right.
What is the output of this.

#include<stdio.h>

int main ()
{
printf ( "%f", 10 ) ;
return 0 ;
}
Last edited by Prabakar; May 7th, 2008 at 7:38 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 44
Reputation: arunpawar is an unknown quantity at this point 
Solved Threads: 0
arunpawar arunpawar is offline Offline
Light Poster

Re: Brainteaser codes

 
0
  #5
May 8th, 2008
That was teasing one...it prints 0.00000 on my console......
can u explain why? and next teasing code guys,,,keep then coming...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Brainteaser codes

 
0
  #6
May 8th, 2008
Give your mind a thought first.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 44
Reputation: arunpawar is an unknown quantity at this point 
Solved Threads: 0
arunpawar arunpawar is offline Offline
Light Poster

Re: Brainteaser codes

 
0
  #7
May 9th, 2008
i got it regarding the Format specifier..Hehe...Next question...Lets start with something teasy with strings...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Brainteaser codes

 
0
  #8
May 9th, 2008
  1. #include <iostream>
  2. #define ENIUQ(TEMPLATE) std::cout << TEMPLATE << "(" << #TEMPLATE << ");}";
  3.  
  4. int main()
  5. {ENIUQ("#include <iostream>\n#define ENIUQ(TEMPLATE) std::cout << TEMPLATE << \"(\" << #TEMPLATE << \");}\";\n\nint main()\n{ENIUQ");}

Here's one for you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC