943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 873
  • C++ RSS
May 7th, 2008
0

Brainteaser codes

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
arunpawar is offline Offline
54 posts
since Dec 2007
May 7th, 2008
0

Re: Brainteaser codes

Click to Expand / Collapse  Quote originally posted by arunpawar ...
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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
May 7th, 2008
0

Re: Brainteaser codes

Thnxfor big question.Do u have anything Teasing..Do post it in here...
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
arunpawar is offline Offline
54 posts
since Dec 2007
May 7th, 2008
0

Re: Brainteaser codes

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.
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008
May 8th, 2008
0

Re: Brainteaser codes

That was teasing one...it prints 0.00000 on my console......
can u explain why? and next teasing code guys,,,keep then coming...
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
arunpawar is offline Offline
54 posts
since Dec 2007
May 8th, 2008
0

Re: Brainteaser codes

Give your mind a thought first.
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008
May 9th, 2008
0

Re: Brainteaser codes

i got it regarding the Format specifier..Hehe...Next question...Lets start with something teasy with strings...
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
arunpawar is offline Offline
54 posts
since Dec 2007
May 9th, 2008
0

Re: Brainteaser codes

CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i need someone to explain how this works...
Next Thread in C++ Forum Timeline: cash register help..!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC