RajanPERT 0 Report post Posted October 4, 2005 Hi, I want some help! I am facing a problem in a program of C++. I am using VC++ 6. The coding of program is: #include <iostream.h> int main() { double result,total; double num1=1; double num2=3; result=num1/num2; cout<<num1<<" / "<<num2<<" ? "; cin>>total; if(total==result) cout<<"Right ans"; else cout<<"Wrong ans"; return 0; } The problem: At run time, when I enter the value of total (i.e. 0.333333, the answer of 1/3). I always get message of “Wrong ans”. Whether the 0.333333 is right ans, because when I print the value of result, I get same value (i.e. 0.333333). How can I debug this problem? Please help! Thanks and Take Care! Share this post Link to post Share on other sites
abhay 0 Report post Posted October 4, 2005 (edited) well did c++ years back so correct me if im wrong the code used for R&D (i used turboc c compiler) #include <iostream.h>#include <dos.h> //for R&D purpose #include <conio.h> //for R&D purpose int main() { clrscr(); // clear the screen (for R&D purpose) double result,total; double num1=1; double num2=3; int a = 1; result=num1/num2; while(a == 1){ cout<< "\n" <<num1<<" / "<<num2<<" ? "; cin>>total; total = (double)total; double abb = 0.123456789; // just for R&D purpose cout << abb; // just for R&D purpose if(result == total){ cout<<"Right ans"; a = 0; } else{ cout<<"Wrong ans" << "\n"; cout << total << "\n" ; //for R&D purpose cout << result << "\n"; //for R&D purpose cout << num1 / num2; //for R&D purpose a = 1; //for R&D purpose } } sleep(5); //for R&D purpose return 0; } as far as i found out you are inserting value "0.333333" which i think you might have found out frm a code like this cout << num1 / num2; but after R&D i found out that "cout << " just gives the first six characters of the double value eg if i = 0.123456789; then cout << i; will give 0.123456; the first six characters are only shown so going with doubles capacity of decimal digits ie 15 decimal digits we should get 0.333333333333333 i e != 0.333333; thats y it is going to wrong answer block; in my PC it was goin till 0.33333333333333333333 ( i e 20 decimal digits) you pc may have diffrent arch so the capacity of double can differ (thats the magic of java ! all pcs same capacity of value types) Edited October 4, 2005 by abhay Share this post Link to post Share on other sites
ashoksoft 83 Report post Posted October 6, 2005 try to enter 0.3333333333 coz the precision of double is architecture dependent... but I guess there was a way to mask to specific places of decimal ( we can do a formatted output thro setprecision() and setw() from iomanip ... if I am not wrong) used to code in Cpp ages ago... Cheers Ashok Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 7, 2005 Finally i debug the problem. The last coding is here #include <iostream.h>#include <math.h> int main() { double result,total; double num1=1; double num2=3; double a=0.1234567890; result=num1/num2; result= floor(result*100+0.5)/100; cout<<a<<endl; cout<<result<<endl; cout<<num1<<" / "<<num2<<" ? "; cin>>total; if(total==result) cout<<"Right ans"; else cout<<"Wrong ans"; return 0; } BTW thanks for help and i think it will architecture independent. if someone have different architecture please try this and share your experience with us. Thanks and Take Care! Share this post Link to post Share on other sites
abhay 0 Report post Posted October 9, 2005 result= floor(result*100+0.5)/100; y in th world did you have to add this line suppose result = 1/3 = 0.333333333333333333333 then result = 0.333333333333333333333 * 100 ie 33.3333333333333333333 -> 33.3333333333333333333 + 0.5 = 33.8333333333333333333 -> floor(33.8333333333333333333) = 33 // floor returns a integer when you had to convert it into a integer then why to use a double value in first place the code should have been like this then #include <iostream.h> int main() { int result,total; int num1=1; int num2=3; result=num1/num2; cout<<num1<<" / "<<num2<<" ? "; cin>>total; if(total==result) cout<<"Right ans"; else cout<<"Wrong ans"; return 0; } would have given you the same answer when you are using double you mean that precision dose matters so converting the result into a integer(by the use of floor()) makes the use of double meaningless Share this post Link to post Share on other sites
ashoksoft 83 Report post Posted October 9, 2005 Very true abhay. The only possible way to ensure both presecion as well as correct (according to the 3 decimal place) output, is to have a formatted output to a file, read back, and compare. Formatted output can be acheived with setprecision() and setw(), both from iomanip.h. Ages since I sat on C/C++, so not really in a position to comment around now ... Cheers Ashok Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 10, 2005 @Abhay, Ashok! You both Guys are seems to be ringht. But i am trying to develope a program where we give two different numbers to user and user provide the ans. The program checked the ans and give results accordingly. So i apply the floor() for help. After applying the floor() statement, we will get the result 0.33 for 1/3, (i checked it with different datas. If any body have a 64bit machine, please also check it in your system, is this right?). Which is equal to the ans given by a general user for same Que. I didnot want to print the result. So there is no use of setprecision or setw or other iomanip.h file's function. Thanks and Take Care! Share this post Link to post Share on other sites
ashoksoft 83 Report post Posted October 10, 2005 hmm ... but then why use double ? when precesion is not a pre-requisite? Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 10, 2005 Ohh...Its my mistake. No need to use double. Thanks for help. Thanks and Take Care! Share this post Link to post Share on other sites
abhay 0 Report post Posted October 12, 2005 (edited) @Abhay, Ashok!You both Guys are seems to be ringht. But i am trying to develope a program where we give two different numbers to user and user provide the ans. The program checked the ans and give results accordingly. So i apply the floor() for help. After applying the floor() statement, we will get the result 0.33 for 1/3, (i checked it with different datas. If any body have a 64bit machine, please also check it in your system, is this right?). Which is equal to the ans given by a general user for same Que. I didnot want to print the result. So there is no use of setprecision or setw or other iomanip.h file's function. Thanks and Take Care! 43540[/snapback] dude if you want to check the answer from the user then you have to use double because if you use a integer you dont get the benifit of presecion for eg:-- while using a integer 1/3 = 0 and 2/3 = 0 you got my point !!! if its a integer then 1/3 == 2/3 but if you take double then 1/3 != 2/3 **** but in double the presecion is different for different machines, so taking the user input is almost impossible in this case**** but if you floor the double value, which you did in your previous program theres no use of using double value because floor will convert it into a integer for eg if 1/3 = 0.33 floor(1/3) = 0; // floor gives you the largest integer which is not greater than the argument used PS :-- the answer of using floor is same as using the integer moral of the story :- it is almost impossible to use to use a double value in ur senario. Edited October 12, 2005 by abhay Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 13, 2005 @Abhay! First, i do not want any precision, because my program will worked for only two decimal places (i.e. 0.33). So i now change to Float. For other information, regarding Double problem, Please run the program (Described in Post 4) and checked it with different value. It will always give right ans. Thanks and Take Care! Share this post Link to post Share on other sites
abhay 0 Report post Posted October 16, 2005 @Abhay!First, i do not want any precision, because my program will worked for only two decimal places (i.e. 0.33). So i now change to Float. For other information, regarding Double problem, Please run the program (Described in Post 4) and checked it with different value. It will always give right ans. Thanks and Take Care! 43885[/snapback] the senirio here is totally changed from the orignal senirio where you needed more decimal points(which only double can support) dude if you need to go with the program (Described in Post 4) then use #include <iostream.h> #include <math.h> int main() { float result,total; int num1=1; int num2=3; int a=0.1234567890; result=num1/num2; result= floor(result*100+0.5)/100; cout<<a<<endl; cout<<result<<endl; cout<<num1<<" / "<<num2<<" ? "; cin>>total; if(total==result) cout<<"Right ans"; else cout<<"Wrong ans"; return 0; } much more memory efficeint due to the use of int rather than double (well its a small prog , so efficency dosent matters here) Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 17, 2005 @Abhay! Sorry, But i didnot understand what u want to say. Because which program u use here in your post no 12 is logically wrong. If we devide 1/3, the ans will always zero, Not 0.33, which i required. The Right program is giving below: #include <iostream.h>#include <math.h> int main() { float result,total; float num1=1; float num2=3; result=num1/num2; result= floor(result*100+0.5)/100; cout<<endl<<result<<endl<<endl; cout<<num1<<" / "<<num2<<" ? "; cin>>total; if(total==result) cout<<"Right ans"; else cout<<"Wrong ans"; return 0; } Please check it. Thanks and Take Care! Share this post Link to post Share on other sites
SexyGurl 0 Report post Posted October 18, 2005 Very true abhay. The only possible way to ensure both presecion as well as correct (according to the 3 decimal place) output, is to have a formatted output to a file, read back, and compare. Formatted output can be acheived with setprecision() and setw(), both from iomanip.h. Ages since I sat on C/C++, so not really in a position to comment around now ... Cheers Ashok 43483[/snapback] I like this approach, (or equivalently we could read the result into a String variable) ... and the cin input should also be read as a string. Comparison should on the basis of the length of the cin input. That way, user could input 0.33 or 0.333 or 0.33333333 and all the times get a match to the result computed by computer. I could have easily implemented this in VBA ... but am learning c++, do have to find out the syntax to implement String inputs, comparison etc. I am going nuts with operator overloading in C++. How hard do you experienced guys think is it, to learn the C++ STL. How long would it take an average guy to learn STL. Can you suggest a good STL tutorial from the web .... (not those childish C++ tutorials starting with hello world programs). A bit higher level than that. Share this post Link to post Share on other sites
abhay 0 Report post Posted October 18, 2005 I am going nuts with operator overloading in C++. ask me i had gone nuts with object oriented concept itself atleast you have crossed that stage took me almost 3 years to complete c++ (oo wasnt my cup of tea that time) i did c++ from 3 books may b they could help :- let us c++ - yashwant kanetkar (starters) object oriented programming in c++ :- robert lafore (somewhat advanced + has some STL in it) object-oriented programming with c++ :- balaguru swamy ( very basic book) FYI i should let you know i never did understand oo until i actully completed c++ and started playing with java Share this post Link to post Share on other sites
adwait 0 Report post Posted October 18, 2005 Not on the net, but Balguruswamy is an excellent book to start with STL, but just the very basic stuff so that you understand what the STL is about. So don't buy that book, boorrow it if u can, because its a pretty basic book with just 1 chapter on the template library. Share this post Link to post Share on other sites
SexyGurl 0 Report post Posted October 18, 2005 (edited) I don't know I understood the OO concept or not ! I think I do .... how do I make sure, that I understand? Is there a test, for that? (I mean not a certificate test .... what should I be able to do, if I understand .... and what should be sufficient to prove that I don't understand it, really) Edited October 18, 2005 by bhutes Share this post Link to post Share on other sites
abhay 0 Report post Posted October 18, 2005 no specific test that i know about but its basic use is actully making programming more real Share this post Link to post Share on other sites
RajanPERT 0 Report post Posted October 18, 2005 Hi Bhutes, You can choose one from these two books (for Beginner to Advance user). 1. C++ How to program Author: Deitel and Deitel 2. C++ Primer Author: Stanley B. Lippman, Josee Lajoie, Barbara E. Moo These books have complete details of STL with Basic. Try to search these two on NET or This Sunday go to Books Market at Lalkila, New Delhi and search these books their. U may find these books very cheaply (About 40 Rs. only). Thanks and Take Care! Share this post Link to post Share on other sites
cracker 0 Report post Posted December 17, 2005 well i think i have that book in pdf format Share this post Link to post Share on other sites
ashoksoft 83 Report post Posted December 18, 2005 so what are u waiting for... upload it mate maybe someone who is searching around for it ... might get a chance to read it . Cheers Ashok Share this post Link to post Share on other sites
cracker 0 Report post Posted December 18, 2005 (edited) Hi Bhutes,You can choose one from these two books (for Beginner to Advance user). 2. C++ Primer Author: Stanley B. Lippman, Josee Lajoie, Barbara E. Moo These books have complete details of STL with Basic. Try to search these two on NET or This Sunday go to Books Market at Lalkila, New Delhi and search these books their. U may find these books very cheaply (About 40 Rs. only). Thanks and Take Care! Essential C++, by Stanley B.Lippman Description: QUOTE Essential C++ presents the basics of C++ in the context of procedural, generic, object-based, and object-oriented programming. It is organized around a series of increasingly complex programming problems, and language features are introduced as solutions to these problems. In this way you will not only learn about the functions and structure of C++, but will understand their purpose and rationale. http://groups.google.co.in/group/desi-tek/...1bfaf0e845e73f3 Edited December 18, 2005 by cracker Share this post Link to post Share on other sites